---
import {
  getDocsContext,
  getSidebarSectionTitle,
  type DocsNavigationHandoff,
} from '../lib/docs';
import type { NormalizedLocale } from '../lib/i18n';
import type { HeadConfig } from '../lib/page/head';
import type { JsonLdNode } from '../lib/page/schema';
import DocsLayout from './DocsLayout.astro';

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

interface Props {
  title: string;
  description?: string;
  head?: HeadConfig;
  jsonLd?: JsonLdNode | JsonLdNode[];
  locale?: NormalizedLocale;
  currentPath?: string;
  currentSection?: string;
  currentSlug?: string;
  docsNavigation?: DocsNavigationHandoff;
  headings?: Heading[];
  pagefind?: boolean;
}

const props = Astro.props;
const docsNavigation: DocsNavigationHandoff = props.docsNavigation
  ?? await getDocsContext(props.currentSection, props.locale?.key);
const currentSection = props.currentSection ?? docsNavigation.currentSection;
const sidebars = docsNavigation.sidebars;
const sidebar = currentSection ? sidebars[currentSection] ?? { links: [], groups: [] } : { links: [], groups: [] };
const sectionTitle = props.currentSlug ? getSidebarSectionTitle(sidebar, props.currentSlug) : undefined;
---

<DocsLayout
  title={props.title}
  description={props.description}
  head={props.head}
  jsonLd={props.jsonLd}
  locale={props.locale}
  currentPath={props.currentPath}
  sectionTitle={sectionTitle}
  headings={props.headings}
  pagefind={props.pagefind}
  sidebar={sidebar}
  sidebars={sidebars}
  subnav={docsNavigation.sections}
>
  <slot />

  <Fragment slot="aside">
    <slot name="aside" />
  </Fragment>
</DocsLayout>
