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

import { auth } from "@/auth"

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

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

import { LangParams } from "@/types/params"

export default async function Header({
  lang,
  languageSwitcher,
}: LangParams & { languageSwitcher: React.ReactNode }) {
  const data = await serverClient().contentstack.config.header()
  const session = await auth()

  const homeHref = homeHrefs[env.NODE_ENV][lang]
  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}
        lang={lang}
      />
      <MainMenu
        frontpageLinkText={frontpage_link_text}
        homeHref={homeHref}
        links={menu.links}
        logo={logo}
        topMenuMobileLinks={topMenuMobileLinks}
        languageSwitcher={languageSwitcher}
        bookingHref={homeHref}
        isLoggedIn={!!session}
        lang={lang}
      />
    </header>
  )
}
