import { homeHrefs } from "@/constants/homeHrefs"
import { env } from "@/env/server"
import { serverClient } from "@/lib/trpc/server"

import { BookingWidget } from "@/components/BookingWidget"
import { getLang } from "@/i18n/serverContext"

import { MainMenu } from "./MainMenu"
import OfflineBanner from "./OfflineBanner"
import TopMenu from "./TopMenu"

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

export default async function Header({
  languageSwitcher,
  myPagesMobileDropdown,
}: {
  languageSwitcher: React.ReactNode
  myPagesMobileDropdown: React.ReactNode
}) {
  const data = await serverClient().contentstack.base.header({
    lang: getLang(),
  })

  const user = await serverClient().user.name()

  /**
   * ToDo: Create logic to get this info from ContentStack based on page
   * */
  const hideBookingWidget = true

  if (!data) {
    return null
  }

  const homeHref = homeHrefs[env.NODE_ENV][getLang()]
  const { frontpage_link_text, logo, menu, top_menu } = data

  const topMenuMobileLinks = top_menu.links
    .filter((link) => link.show_on_mobile)
    .sort((a, b) => (a.sort_order_mobile < b.sort_order_mobile ? 1 : -1))

  return (
    <header className={styles.header} role="banner">
      <OfflineBanner />
      <TopMenu
        frontpageLinkText={frontpage_link_text}
        homeHref={homeHref}
        links={top_menu.links}
        languageSwitcher={languageSwitcher}
      />
      <MainMenu
        frontpageLinkText={frontpage_link_text}
        homeHref={homeHref}
        links={menu.links}
        logo={logo}
        topMenuMobileLinks={topMenuMobileLinks}
        languageSwitcher={languageSwitcher}
        myPagesMobileDropdown={myPagesMobileDropdown}
        bookingHref={homeHref}
        user={user}
      />
      {hideBookingWidget ? null : <BookingWidget />}
    </header>
  )
}
