import data from "@/data/clinical/atlas-pages.json";

export type AtlasBlock =
  | { type: "h1" | "h2" | "h3" | "p"; text: string }
  | { type: "ul" | "ol"; items: string[] }
  | { type: "table"; rows: string[][] };

export type AtlasSection = {
  /** Heading shown as the section title. null/undefined for the intro block. */
  title: string | null;
  blocks: AtlasBlock[];
};

export type AtlasPage = {
  title: string;
  sections: AtlasSection[];
};

export type AtlasPagesPayload = {
  generatedAt: string;
  methodology: AtlasPage;
  dataSources: AtlasPage;
};

const payload = data as unknown as AtlasPagesPayload;

export function getMethodologyPage(): AtlasPage {
  return payload.methodology;
}

export function getDataSourcesPage(): AtlasPage {
  return payload.dataSources;
}

export function getAtlasPagesGeneratedAt(): string {
  return payload.generatedAt;
}

/**
 * Produce a slug-friendly id for an h2 heading so the page can render a
 * table of contents with working jump links.
 */
export function sectionAnchor(title: string | null, index: number): string {
  if (!title) return `intro-${index}`;
  return title
    .toLowerCase()
    .normalize("NFD")
    .replace(/[\u0300-\u036f]/g, "")
    .replace(/[^a-z0-9\s-]/g, "")
    .trim()
    .replace(/\s+/g, "-")
    .replace(/-+/g, "-");
}
