import JsonToHtml from "@/components/JsonToHtml"
import { renderOptions } from "@/components/JsonToHtml/renderOptions"
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 SoonestStays from "@/components/MyPages/Blocks/Stays/Soonest"
import UpcomingStays from "@/components/MyPages/Blocks/Stays/Upcoming"

import PreviousStays from "../Blocks/Stays/Previous"

import {
  AccountPageContentProps,
  ContentProps,
} from "@/types/components/myPages/myPage/accountPage"
import {
  ContentEntries,
  DynamicContentComponents,
} from "@/types/requests/myPages/accountpage"

function DynamicComponent({ user, component, props }: AccountPageContentProps) {
  switch (component) {
    case DynamicContentComponents.membership_overview:
      return <Overview user={user} title={props.title} />
    case DynamicContentComponents.previous_stays:
      return <PreviousStays {...props} />
    case DynamicContentComponents.soonest_stays:
      return <SoonestStays {...props} />
    case DynamicContentComponents.upcoming_stays:
      return <UpcomingStays {...props} />
    case DynamicContentComponents.current_benefits:
      return <CurrentBenefitsBlock {...props} />
    case DynamicContentComponents.next_benefits:
      return <NextLevelBenefitsBlock {...props} />
    default:
      return null
  }
}

export default function Content({ user, lang, content }: ContentProps) {
  return (
    <>
      {content.map((item) => {
        switch (item.__typename) {
          case ContentEntries.AccountPageContentDynamicContent:
            const link = item.dynamic_content.link.linkConnection.edges.length
              ? {
                  href: item.dynamic_content.link.linkConnection.edges[0].node
                    .url,
                  text: item.dynamic_content.link.link_text,
                }
              : null

            const componentProps = {
              lang,
              title: item.dynamic_content.title,
              // TODO: rename preamble to subtitle in Contentstack
              subtitle: item.dynamic_content.preamble,
              ...(link && { link }),
            }
            return (
              <DynamicComponent
                component={item.dynamic_content.component}
                user={user}
                props={componentProps}
              />
            )
          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}
              />
            )
          case ContentEntries.AccountPageContentTextContent:
            return (
              <section>
                <JsonToHtml
                  embeds={[]}
                  nodes={item.text_content.content.json.children}
                  renderOptions={renderOptions}
                />
              </section>
            )
          default:
            return null
        }
      })}
    </>
  )
}
