import JsonToHtml from "@/components/JsonToHtml"
import DynamicContentBlock from "@/components/Loyalty/Blocks/DynamicContent"

import CardGrid from "./CardGrid"

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

export function Blocks({ blocks }: BlocksProps) {
  return blocks.map((block) => {
    switch (block.__typename) {
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid:
        return <CardGrid card_grid={block.card_grid} />
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
        return (
          <section>
            <JsonToHtml
              nodes={block.content.content.json.children}
              embeds={block.content.content.embedded_itemsConnection.edges}
            />
          </section>
        )
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent:
        return <DynamicContentBlock dynamicContent={block.dynamic_content} />
      default:
        return null
    }
  })
}
