import Overview from "@/components/Blocks/DynamicContent/Overview"
import EarnAndBurn from "@/components/Blocks/DynamicContent/Points/EarnAndBurn"
import PointsOverview from "@/components/Blocks/DynamicContent/Points/Overview"
import CurrentRewardsBlock from "@/components/Blocks/DynamicContent/Rewards/CurrentLevel"
import NextLevelRewardsBlock from "@/components/Blocks/DynamicContent/Rewards/NextLevel"
import ShortcutsList from "@/components/Blocks/ShortcutsList"
import JsonToHtml from "@/components/JsonToHtml"
import { getLang } from "@/i18n/serverContext"
import { modWebviewLink } from "@/utils/webviews"

import type {
  AccountPageContentProps,
  ContentProps,
} from "@/types/components/myPages/myPage/accountPage"
import { BlocksEnums } from "@/types/enums/blocks"
import { DynamicContentEnum } from "@/types/enums/dynamicContent"

function DynamicComponent({ dynamic_content }: AccountPageContentProps) {
  const dynamicContent = {
    ...dynamic_content,
    link: dynamic_content.link
      ? {
          ...dynamic_content.link,
          href: modWebviewLink(dynamic_content.link.href, getLang()),
        }
      : undefined,
  }
  switch (dynamic_content.component) {
    case DynamicContentEnum.Blocks.components.membership_overview:
      return <Overview {...dynamicContent} />
    case DynamicContentEnum.Blocks.components.points_overview:
      return <PointsOverview {...dynamicContent} />
    case DynamicContentEnum.Blocks.components.current_benefits:
      return <CurrentRewardsBlock {...dynamicContent} />
    case DynamicContentEnum.Blocks.components.next_benefits:
      return <NextLevelRewardsBlock {...dynamicContent} />
    case DynamicContentEnum.Blocks.components.expiring_points:
      // TODO: Add once available
      // return <ExpiringPoints />
      return null
    case DynamicContentEnum.Blocks.components.earn_and_burn:
      return <EarnAndBurn {...dynamicContent} />
    default:
      return null
  }
}

export default function Content({ content }: ContentProps) {
  return (
    <>
      {content.map((item, idx) => {
        switch (item.typename) {
          case BlocksEnums.block.DynamicContent:
            return (
              <DynamicComponent
                key={`${item.dynamic_content.title}-${idx}`}
                dynamic_content={item.dynamic_content}
              />
            )
          case BlocksEnums.block.Shortcuts:
            const shortcuts = item.shortcuts.shortcuts.map((shortcut) => {
              return {
                ...shortcut,
                url: modWebviewLink(shortcut.url, getLang()),
              }
            })
            return (
              <ShortcutsList
                key={`${item.shortcuts.title}-${idx}`}
                shortcuts={shortcuts}
                subtitle={item.shortcuts.subtitle}
                title={item.shortcuts.title}
                hasTwoColumns={item.shortcuts.hasTwoColumns}
              />
            )
          case BlocksEnums.block.TextContent:
            return (
              <section key={`${item.__typename}-${idx}`}>
                <JsonToHtml
                  embeds={
                    item.text_content.content.embedded_itemsConnection.edges
                  }
                  nodes={item.text_content.content.json.children}
                />
              </section>
            )
          default:
            return null
        }
      })}
    </>
  )
}
