import { MANUAL_TOC, type ManualEntry } from './manual.generated'

export type { ManualEntry } from './manual.generated'

export type ManualChapter = {
  entry: ManualEntry
  body: string
}

export function getManualToc(): readonly ManualEntry[] {
  return MANUAL_TOC
}

function slugMatches(a: readonly string[], b: readonly string[]): boolean {
  if (a.length !== b.length) return false
  for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false
  return true
}

export function findManualEntry(slug: readonly string[]): ManualEntry | null {
  return MANUAL_TOC.find((e) => slugMatches(e.slug, slug)) ?? null
}

export function getManualChapter(slug: readonly string[]): ManualChapter | null {
  const entry = findManualEntry(slug)
  if (!entry) return null
  return { entry, body: entry.markdown }
}
