import type { Dictionary } from "@/app/[lang]/dictionaries";

// Documentation sidebar navigation. `href` values are locale-less; the
// components prefix the active locale (e.g. /en/docs/quickstart). Titles are
// resolved from the dictionary so the sidebar is fully i18n.
export interface DocsNavLink {
  title: string;
  href: string;
}

export interface DocsNavGroup {
  title: string;
  links: DocsNavLink[];
}

type DocsLinks = Dictionary["docs"]["nav"]["links"];

// Structure (keys + hrefs) lives in code; labels come from the dictionary.
const STRUCTURE: {
  titleKey: keyof Omit<Dictionary["docs"]["nav"], "links">;
  links: { key: keyof DocsLinks; href: string }[];
}[] = [
  {
    titleKey: "guides",
    links: [
      { key: "introduction", href: "/docs" },
      { key: "quickstart", href: "/docs/quickstart" },
      { key: "mcp", href: "/docs/mcp" },
      { key: "authentication", href: "/docs/authentication" },
    ],
  },
  {
    titleKey: "coreConcepts",
    links: [
      { key: "keywords", href: "/docs/keywords" },
      { key: "backlinks", href: "/docs/backlinks" },
      { key: "content", href: "/docs/content" },
      { key: "webhooks", href: "/docs/webhooks" },
    ],
  },
];

// All doc page paths (locale-less), derived from STRUCTURE — the single source
// of truth. Used by the sitemap so new docs are indexed automatically.
export const docsPaths: string[] = STRUCTURE.flatMap((group) =>
  group.links.map((link) => link.href),
);

// Build the localized sidebar tree from the dictionary.
export function getDocsNavigation(dict: Dictionary): DocsNavGroup[] {
  const nav = dict.docs.nav;
  return STRUCTURE.map((group) => ({
    title: nav[group.titleKey],
    links: group.links.map((link) => ({
      title: nav.links[link.key],
      href: link.href,
    })),
  }));
}

// Flatten the tree to a single ordered list (used for prev/next pagination).
export function getDocsFlatLinks(dict: Dictionary): DocsNavLink[] {
  return getDocsNavigation(dict).flatMap((group) => group.links);
}
