---
import type { DocsSectionNav, DocsSidebarNav } from '../lib/docs';
import BaseLayout from './BaseLayout.astro';
import Footer from '../components/layout/Footer.astro';
import Header from '../components/layout/Header.astro';
import Sidebar from '../components/theme/Sidebar.astro';
import { getDefaultLocale, type NormalizedLocale } from '../lib/i18n';
import type { HeadConfig } from '../lib/page/head';
import type { JsonLdNode } from '../lib/page/schema';
import rawThemeConfig from 'virtual:prosefly/lotus/config';
import { isPagefindSearchEnabled } from '../lib/search';
import type { LotusThemeConfig } from '../lib/theme';

interface Heading {
  depth: number;
  slug: string;
  text: string;
}

interface Props {
  title: string;
  description?: string;
  head?: HeadConfig;
  jsonLd?: JsonLdNode | JsonLdNode[];
  locale?: NormalizedLocale;
  currentPath?: string;
  sectionTitle?: string;
  headings?: Heading[];
  pagefind?: boolean;
  subnav: DocsSectionNav[];
  sidebar: DocsSidebarNav;
  sidebars?: Record<string, DocsSidebarNav>;
}

const {
  title,
  description,
  head,
  jsonLd,
  locale,
  currentPath = Astro.url.pathname,
  sectionTitle,
  headings = [],
  pagefind = true,
  subnav,
  sidebar,
  sidebars = {},
} = Astro.props;
const themeConfig = rawThemeConfig as LotusThemeConfig;
const currentLocale = locale ?? getDefaultLocale(themeConfig);
const pagefindEnabled = isPagefindSearchEnabled(themeConfig);
const pagefindAttrs = pagefindEnabled
  ? pagefind
    ? { 'data-pagefind-body': true }
    : { 'data-pagefind-ignore': true }
  : {};
const pagefindIgnoreAttrs = pagefindEnabled ? { 'data-pagefind-ignore': true } : {};
---

<BaseLayout title={title} description={description} head={head} jsonLd={jsonLd} locale={currentLocale}>
  <Header
    currentPath={currentPath}
    headings={headings}
    sectionTitle={sectionTitle}
    sections={subnav}
    sidebar={sidebar}
    sidebars={sidebars}
    title={title}
  />

  <main class="pb-16">
    <div class="mx-auto grid max-w-screen-2xl grid-cols-1 gap-y-10 px-5 sm:px-6 lg:grid-cols-[var(--lotus-sidebar-width)_minmax(0,var(--lotus-content-width))] lg:gap-x-10 xl:grid-cols-[var(--lotus-sidebar-width)_minmax(0,var(--lotus-content-width))_var(--lotus-toc-width)] xl:gap-x-12">
      <aside class="hidden lg:block">
        <div class="sticky top-(--lotus-docs-chrome-height)">
          {(sidebar.links.length > 0 || sidebar.groups.length > 0) && (
            <Sidebar currentPath={currentPath} sidebar={sidebar} />
          )}
        </div>
      </aside>

      <article class="mx-auto w-full max-w-4xl min-w-0 pt-8 pb-10" {...pagefindAttrs}>
        <slot />
      </article>

      <aside class="hidden xl:block" {...pagefindIgnoreAttrs}>
        <div class="sticky top-(--lotus-docs-chrome-height) pt-6 pb-8">
          <slot name="aside" />
        </div>
      </aside>
    </div>
  </main>

  <Footer />
</BaseLayout>
