import { JsonToHtml } from "@scandic-hotels/design-system/JsonToHtml"
import { BlocksEnums } from "@scandic-hotels/trpc/types/blocksEnum"

import CardsGrid from "@/components/Blocks/CardsGrid"
import CarouselCards from "@/components/Blocks/CarouselCards"
import DynamicContent from "@/components/Blocks/DynamicContent"
import ShortcutsList from "@/components/Blocks/ShortcutsList"
import TextCols from "@/components/Blocks/TextCols"
import UspGrid from "@/components/Blocks/UspGrid"

import AccordionSection from "./Accordion"
import CardGallery from "./CardGallery"
import Essentials from "./Essentials"
import HotelListing from "./HotelListing"
import Table from "./Table"

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

export default function Blocks({ blocks }: BlocksProps) {
  return blocks.map(async (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.CardsGrid:
        return (
          <CardsGrid
            cards_grid={block.cards_grid}
            key={`${block.cards_grid.title}-${idx}`}
          />
        )
      case BlocksEnums.block.Content:
        if (!block.content) return null
        return (
          <JsonToHtml
            key={`${block.typename}-${idx}`}
            nodes={block.content.json.children}
            embeds={block.content.embedded_itemsConnection.edges}
          />
        )
      case BlocksEnums.block.DynamicContent:
        return (
          <DynamicContent
            dynamic_content={block.dynamic_content}
            key={`${block.dynamic_content.title}-${idx}`}
          />
        )
      case BlocksEnums.block.CarouselCards:
        return (
          <CarouselCards
            carousel_cards={block.carousel_cards}
            key={`${block.carousel_cards.heading}-${idx}`}
          />
        )
      case BlocksEnums.block.CardGallery:
        return (
          <CardGallery
            card_gallery={block.card_gallery}
            key={`${block.card_gallery.heading}-${idx}`}
          />
        )
      case BlocksEnums.block.ContentPageHotelListing:
        const { heading, contentType, locationFilter, hotelsToInclude } =
          block.hotel_listing
        if (!locationFilter && !hotelsToInclude.length) {
          return null
        }

        return (
          <HotelListing
            heading={heading}
            locationFilter={locationFilter}
            hotelsToInclude={hotelsToInclude}
            contentType={contentType}
          />
        )
      case BlocksEnums.block.Shortcuts:
        return (
          <ShortcutsList
            key={`${block.shortcuts.title}-${idx}`}
            shortcuts={block.shortcuts.shortcuts}
            subtitle={block.shortcuts.subtitle}
            title={block.shortcuts.title}
            hasTwoColumns={block.shortcuts.hasTwoColumns}
          />
        )
      case BlocksEnums.block.Table:
        return <Table data={block.table} />
      case BlocksEnums.block.TextCols:
        return <TextCols text_cols={block.text_cols} />
      case BlocksEnums.block.TextContent:
        return (
          <JsonToHtml
            key={`${block.typename}-${idx}`}
            embeds={block.text_content.content.embedded_itemsConnection.edges}
            nodes={block.text_content.content.json.children}
          />
        )
      case BlocksEnums.block.UspGrid:
        return <UspGrid usp_grid={block.usp_grid} />
      case BlocksEnums.block.Essentials:
        return <Essentials content={block.essentials} />
      default:
        return null
    }
  })
}
