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 Script from "next/script"

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

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 { FontPreload } from "@/fonts/font-preloading"
import { getMessages } from "@/i18n"
import ClientIntlProvider from "@/i18n/Provider"
import { setLang } from "@/i18n/serverContext"

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

import type { Metadata } from "next"

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

export const metadata: Metadata = {
  title: "Webview",
}

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

  const { children } = props

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

  return (
    <html lang={params.lang}>
      <head>
        <FontPreload />
        <AdobeSDKScript />
        <GTMScript />
        {/* 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 ${styles.layout}`}>
        <ClientIntlProvider
          defaultLocale={Lang.en}
          locale={params.lang}
          messages={messages}
        >
          <TrpcProvider>
            <RouterTracking />
            {children}
            <ToastHandler />
          </TrpcProvider>
        </ClientIntlProvider>
      </body>
    </html>
  )
}
