import JsonToHtml from "@/components/JsonToHtml"

import ShortcutsList from "../Blocks/ShortcutsList"
import Card from "../TempDesignSystem/Card"
import TeaserCard from "../TempDesignSystem/TeaserCard"
import JoinLoyaltyContact from "./JoinLoyalty"
import MyPagesNavigation from "./MyPagesNavigation"

import styles from "./sidebar.module.css"

import type { SidebarProps } from "@/types/components/sidebar"
import { DynamicContentEnum } from "@/types/enums/dynamicContent"
import { SidebarEnums } from "@/types/enums/sidebar"

export default function Sidebar({ blocks }: SidebarProps) {
  return (
    <aside className={styles.aside}>
      {blocks.map((block, idx) => {
        switch (block.typename) {
          case SidebarEnums.blocks.Content:
            return (
              <section
                className={styles.content}
                key={`${block.typename}-${idx}`}
              >
                <JsonToHtml
                  embeds={block.content.embedded_itemsConnection.edges}
                  nodes={block.content.json.children}
                />
              </section>
            )
          case SidebarEnums.blocks.DynamicContent:
            switch (block.dynamic_content.component) {
              case DynamicContentEnum.Sidebar.components.my_pages_navigation:
                return <MyPagesNavigation key={`${block.typename}-${idx}`} />
              default:
                return null
            }
          case SidebarEnums.blocks.JoinLoyaltyContact:
            return (
              <JoinLoyaltyContact
                block={block.join_loyalty_contact}
                key={`${block.typename}-${idx}`}
              />
            )
          case SidebarEnums.blocks.ScriptedCard:
            return (
              <Card
                key={block.scripted_card.system.uid}
                heading={block.scripted_card.heading}
                secondaryButton={block.scripted_card.secondaryButton}
                primaryButton={block.scripted_card.primaryButton}
                bodyText={block.scripted_card.body_text}
                scriptedTopTitle={block.scripted_card.scripted_top_title}
                theme={block.scripted_card.theme ?? "image"}
              />
            )
          case SidebarEnums.blocks.TeaserCard:
            return (
              <TeaserCard
                title={block.teaser_card.heading}
                description={block.teaser_card.body_text}
                intent={block.teaser_card.theme}
                key={block.teaser_card.system.uid}
                primaryButton={block.teaser_card.primaryButton}
                secondaryButton={block.teaser_card.secondaryButton}
                sidePeekButton={block.teaser_card.sidePeekButton}
                sidePeekContent={block.teaser_card.sidePeekContent}
                image={block.teaser_card.image}
              />
            )
          case SidebarEnums.blocks.QuickLinks:
            return <ShortcutsList {...block.shortcuts} hasTwoColumns={false} />

          default:
            return null
        }
      })}
    </aside>
  )
}
