import { Lang } from "@/constants/languages"

import {
  AccountPageContentItem,
  ContentEntries,
  DynamicContent,
  DynamicContentComponents,
} from "@/types/requests/myPages/accountpage"

import CurrentBenefitsBlock from "@/components/MyPages/Blocks/Benefits/CurrentLevel"
import NextLevelBenefitsBlock from "@/components/MyPages/Blocks/Benefits/NextLevel"
import Overview from "@/components/MyPages/Blocks/Overview"
import Shortcuts from "@/components/MyPages/Blocks/Shortcuts"
import UpcomingStays from "@/components/MyPages/Blocks/Stays/Upcoming"
import { User } from "@/types/user"

function DynamicComponent({
  user,
  lang,
  content,
}: {
  content: DynamicContent
  lang: Lang
  user: User
}) {
  const componentProps = {
    title: content.title,
    preamble: content.preamble,
    link: content.link.linkConnection.edges.length
      ? {
          href: content.link.linkConnection.edges[0].node.url,
          text: content.link.link_text,
        }
      : null,
  }
  switch (content.component) {
    case DynamicContentComponents.membership_overview:
      return <Overview user={user} />
    case DynamicContentComponents.benefits:
      return null
    case DynamicContentComponents.previous_stays:
      return null
    case DynamicContentComponents.upcoming_stays:
      return <UpcomingStays lang={lang} {...componentProps} />
    case DynamicContentComponents.current_benefits:
      return (
        <CurrentBenefitsBlock
          title={content.title}
          preamble={content.preamble}
        />
      )
    case DynamicContentComponents.next_benefits:
      return (
        <NextLevelBenefitsBlock
          title={content.title}
          preamble={content.preamble}
        />
      )
    default:
      return null
  }
}

export default function Content({
  user,
  lang,
  content,
}: {
  user: User
  lang: Lang
  content: AccountPageContentItem[]
}) {
  return (
    <>
      {content.map((item) => {
        switch (item.__typename) {
          case ContentEntries.AccountPageContentDynamicContent:
            return (
              <DynamicComponent
                user={user}
                lang={lang}
                content={item.dynamic_content}
              />
            )
          case ContentEntries.AccountPageContentShortcuts:
            const shortcuts = item.shortcuts.shortcuts.map(
              (shortcut) => shortcut.linkConnection.edges[0].node
            )
            return (
              <Shortcuts
                shortcuts={shortcuts}
                subtitle={item.preamble}
                title={item.title}
              />
            )
          default:
            return null
        }
      })}
    </>
  )
}
