---
import "blume:theme";
import type { BlumeFavicon } from "../../core/data.ts";
import { EN_UI } from "../../core/i18n-ui.ts";
import type { UIStrings } from "../../core/i18n-ui.ts";
import type { FontHead } from "../../theme/fonts.ts";
import type { Navigation } from "../../core/types.ts";
import Analytics from "./Analytics.astro";
import WebMcp from "./WebMcp.astro";
import Banner from "./Banner.astro";
import Favicon from "./Favicon.astro";
import Fonts from "./Fonts.astro";
import { BANNER_INIT_SCRIPT, THEME_INIT_SCRIPT } from "./head-scripts.ts";
import Header from "./Header.astro";

// A minimal shell for the Scalar API/AsyncAPI reference: Blume's banner + navbar
// on top, then a full-height region the reference mounts into. Unlike RootLayout
// it has no docs sidebar/TOC/prose column, so the reference renders edge to edge.
//
// Deliberately NOT wired into the client router (`<ClientRouter />`): Scalar is
// a SPA whose mount script runs once per real page load, so a client-side swap
// back onto this page would leave the mount region empty (Astro never re-runs a
// script it has already executed). Without the router's marker meta, the docs
// pages' router falls back to a normal full load when navigating here — exactly
// what a fresh Scalar mount needs.
interface Props {
  site: { title: string; description?: string };
  logo?: {
    svg?: string;
    light?: string;
    dark?: string;
    alt: string;
    href: string;
    text?: string;
  } | null;
  favicon?: BlumeFavicon | null;
  appleIcon?: BlumeFavicon | null;
  banner?: {
    content: string;
    link?: { text: string; href: string };
    dismissible: boolean;
    key: string;
  } | null;
  analytics?: {
    posthog?: { host?: string; key: string };
    scripts?: {
      attributes?: Record<string, string>;
      content?: string;
      src?: string;
      strategy?: "async" | "defer";
    }[];
    vercel?: boolean;
  } | null;
  navigation: Navigation;
  route: string;
  themeMode: "system" | "light" | "dark";
  fontCssVars?: (string | FontHead)[];
  searchEnabled: boolean;
  pageTitle: string;
  /** Keep the reference route out of crawler indexes. */
  noindex?: boolean;
  /** Active locale code for `<html lang>` (defaults to `en`). */
  locale?: string;
  /** Text direction for `<html dir>` (defaults to `ltr`). */
  dir?: "ltr" | "rtl";
  /** Resolved UI dictionary; English baseline when omitted. */
  ui?: UIStrings;
}

const {
  site,
  logo,
  favicon,
  appleIcon,
  banner,
  analytics,
  navigation,
  route,
  themeMode,
  fontCssVars,
  searchEnabled,
  pageTitle,
  noindex = false,
  locale = "en",
  dir = "ltr",
  ui,
} = Astro.props;

const strings = ui ?? EN_UI;

const bannerKey = banner?.dismissible ? banner.key : null;
---

<!doctype html>
<html dir={dir} lang={locale}>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    {noindex && <meta name="robots" content="noindex" />}
    <title>{pageTitle}</title>
    <Favicon favicon={favicon} appleIcon={appleIcon} />
    <Fonts cssVars={fontCssVars ?? []} />
    {
      bannerKey && (
        <script data-key={bannerKey} is:inline set:html={BANNER_INIT_SCRIPT} />
      )
    }
    <script data-mode={themeMode} is:inline set:html={THEME_INIT_SCRIPT} />
    <Analytics analytics={analytics} />
  </head>
  <body class="bg-background font-sans text-foreground antialiased">
    <a
      class="absolute start-[-999px] top-0 z-[100] bg-accent px-4 py-2 text-accent-foreground focus:start-0"
      href="#blume-content">{strings.page.skipToContent}</a
    >
    <Banner banner={banner} strings={strings.banner} />
    <Header
      askStrings={strings.ask}
      hasDrawer={false}
      hasSidebar={false}
      logo={logo}
      navigation={navigation}
      route={route}
      searchEnabled={searchEnabled}
      site={site}
    />
    <div
      class="h-[calc(100dvh-4rem)] overflow-auto border-border border-t [&>[data-scalar-client]]:h-full"
      id="blume-content"
    >
      <slot />
    </div>
    <WebMcp />
  </body>
</html>
