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, idx) => {
    const firstItem = idx === 0
    switch (block.__typename) {
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
        return (
          <section key={`${block.__typename}-${idx}`}>
            <JsonToHtml
              nodes={block.content.content.json.children}
              embeds={block.content.content.embedded_itemsConnection.edges}
            />
          </section>
        )
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent:
        return (
          <DynamicContentBlock
            dynamicContent={block.dynamic_content}
            firstItem={firstItem}
            key={`${block.dynamic_content.title}-${idx}`}
          />
        )
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts:
        return (
          <Shortcuts
            firstItem={firstItem}
            key={`${block.shortcuts.title}-${idx}`}
            shortcuts={block.shortcuts.shortcuts}
            subtitle={block.shortcuts.preamble}
            title={block.shortcuts.title}
          />
        )
      case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardsGrid:
        return (
          <CardsGrid
            cards_grid={block.cards_grid}
            key={`${block.cards_grid.title}-${idx}`}
          />
        )
      default:
        return null
    }
  })
}
