"use client"

import { useDestinationDataStore } from "@/stores/destination-data"

import AccordionSection from "@/components/Blocks/Accordion"
import JsonToHtml from "@/components/JsonToHtml"

import type { BlocksProps } from "@/types/components/blocks"
import { BlocksEnums } from "@/types/enums/blocks"

export default function Blocks({ blocks }: BlocksProps) {
  const { activeFilters } = useDestinationDataStore((state) => ({
    activeFilters: state.activeFilters,
  }))

  if (activeFilters.length) {
    return null
  }

  return blocks.map((block, idx) => {
    switch (block.typename) {
      case BlocksEnums.block.Accordion:
        return (
          <AccordionSection
            accordion={block.accordion.accordions}
            title={block.accordion.title}
            key={`${block.typename}-${idx}`}
          />
        )
      case BlocksEnums.block.Content:
        return (
          <JsonToHtml
            key={`${block.typename}-${idx}`}
            nodes={block.content.json.children}
            embeds={block.content.embedded_itemsConnection.edges}
          />
        )
      default:
        return null
    }
  })
}
