import "@scandic-hotels/design-system/fonts.css"
import "@/app/globals.css"
import "@/public/_static/css/design-system-new-deprecated.css"
import "@scandic-hotels/design-system/style.css"

import { ReactQueryDevtools } from "@tanstack/react-query-devtools"
import Script from "next/script"
import { SessionProvider } from "next-auth/react"

import { Lang } from "@/constants/languages"
import TrpcProvider from "@/lib/trpc/Provider"

import { SessionRefresher } from "@/components/Auth/TokenRefresher"
import CookieBotConsent from "@/components/CookieBot"
import Footer from "@/components/Footer"
import Header from "@/components/Header"
import StorageCleaner from "@/components/HotelReservation/EnterDetails/StorageCleaner"
import { RACRouterProvider } from "@/components/RACRouterProvider"
import SitewideAlert from "@/components/SitewideAlert"
import { ToastHandler } from "@/components/TempDesignSystem/Toasts"
import AdobeSDKScript from "@/components/TrackingSDK/AdobeSDKScript"
import GTMScript from "@/components/TrackingSDK/GTMScript"
import RouterTracking from "@/components/TrackingSDK/RouterTracking"
import { getMessages } from "@/i18n"
import ClientIntlProvider from "@/i18n/Provider"
import { setLang } from "@/i18n/serverContext"

import type { LangParams, LayoutArgs } from "@/types/params"

export default async function RootLayout(
  props: React.PropsWithChildren<
    LayoutArgs<LangParams> & {
      bookingwidget: React.ReactNode
    }
  >
) {
  const params = await props.params;

  const {
    bookingwidget,
    children
  } = props;

  setLang(params.lang)
  const messages = await getMessages(params.lang)

  return (
    <html lang={params.lang}>
      <head>
        <AdobeSDKScript />
        <GTMScript />
        <Script
          strategy="beforeInteractive"
          data-blockingmode="auto"
          data-cbid="6d539de8-3e67-4f0f-a0df-8cef9070f712"
          data-culture={params.lang}
          id="Cookiebot"
          src="https://consent.cookiebot.com/uc.js"
        />
        {/* eslint-disable-next-line formatjs/no-literal-string-in-jsx */}
        <Script id="ensure-adobeDataLayer">{`
          window.adobeDataLayer = window.adobeDataLayer || []
          window.dataLayer = window.dataLayer || []
        `}</Script>
      </head>
      <body className="scandic">
        <div className="root">
          <SessionProvider basePath="/api/web/auth">
            <ClientIntlProvider
              defaultLocale={Lang.en}
              locale={params.lang}
              messages={messages}
            >
              <TrpcProvider>
                <RACRouterProvider>
                  <RouterTracking />
                  <SitewideAlert />
                  <Header />
                  {bookingwidget}
                  {children}
                  <Footer />
                  <ToastHandler />
                  <SessionRefresher />
                  <StorageCleaner />
                  <CookieBotConsent />
                  <ReactQueryDevtools initialIsOpen={false} />
                </RACRouterProvider>
              </TrpcProvider>
            </ClientIntlProvider>
          </SessionProvider>
        </div>
      </body>
    </html>
  )
}
