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

import CardsGrid from "./CardsGrid"

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.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} />
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts:
        return (
          <Shortcuts
            shortcuts={block.shortcuts.shortcuts}
            title={block.shortcuts.title}
            subtitle={block.shortcuts.preamble}
          />
        )
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardsGrid:
        return <CardsGrid cards_grid={block.cards_grid} />
      default:
        return null
    }
  })
}
