/**
 * Frontmatter schema for canonical site markdown pages under
 * `docs/site/pages/<locale>/*.md`.
 *
 * Site pages use the same YAML frontmatter parser as `/docs` but carry
 * route-oriented fields (path, locale, shell, component) instead of
 * category/subcategory fields.
 *
 * Determinism: resolution is a pure function of (relPath, source, meta.json).
 */
import { splitFrontmatter, parseFlatYaml, extractFirstH1 } from "./doc_frontmatter.js";
/**
 * Resolve site page frontmatter from a markdown source and optional meta.json.
 *
 * @param relPath  Path relative to site pages root, e.g. `en/faq.md`.
 * @param source   Raw markdown source (frontmatter + body).
 * @param metaJson Optional parsed meta.json object (from sibling file).
 */
export function resolveSitePageFrontmatter(relPath, source, metaJson) {
    const { yaml, body } = splitFrontmatter(source);
    const raw = yaml ? parseFlatYaml(yaml) : {};
    const localePart = relPath.split("/")[0];
    const locale = typeof raw.locale === "string" && (raw.locale === "en" || raw.locale === "es")
        ? raw.locale
        : typeof metaJson?.locale === "string" &&
            (metaJson.locale === "en" || metaJson.locale === "es")
            ? metaJson.locale
            : localePart === "es"
                ? "es"
                : "en";
    const pathVal = typeof raw.path === "string" && raw.path.trim()
        ? raw.path.trim()
        : typeof metaJson?.path === "string" && metaJson.path.trim()
            ? metaJson.path.trim()
            : inferPathFromRelPath(relPath);
    const h1 = extractFirstH1(body);
    const page_title = typeof raw.page_title === "string" && raw.page_title.trim()
        ? raw.page_title.trim()
        : typeof metaJson?.page_title === "string" && metaJson.page_title.trim()
            ? metaJson.page_title.trim()
            : (h1 ?? inferTitleFromPath(pathVal));
    const shell = raw.shell === "bare" || raw.shell === "detail"
        ? raw.shell
        : metaJson?.shell === "bare"
            ? "bare"
            : "detail";
    const translation_of = typeof raw.translation_of === "string"
        ? raw.translation_of || null
        : typeof metaJson?.translation_of === "string"
            ? metaJson.translation_of || null
            : null;
    const translation_status = typeof raw.translation_status === "string" && raw.translation_status.trim()
        ? raw.translation_status.trim()
        : typeof metaJson?.translation_status === "string"
            ? metaJson.translation_status
            : "canonical";
    const component = typeof raw.component === "string" && raw.component.trim()
        ? raw.component.trim()
        : typeof metaJson?.component === "string"
            ? metaJson.component
            : null;
    const nav_group = typeof raw.nav_group === "string" && raw.nav_group.trim()
        ? raw.nav_group.trim()
        : typeof metaJson?.nav_group === "string"
            ? metaJson.nav_group
            : null;
    const nav_order = typeof raw.nav_order === "number" && Number.isFinite(raw.nav_order)
        ? raw.nav_order
        : typeof metaJson?.nav_order === "number"
            ? metaJson.nav_order
            : null;
    const description = typeof raw.description === "string" && raw.description.trim()
        ? raw.description.trim()
        : typeof metaJson?.description === "string"
            ? metaJson.description
            : null;
    const visibility = raw.visibility === "internal" ? "internal" : "public";
    return {
        path: pathVal,
        locale,
        page_title,
        shell,
        translation_of,
        translation_status,
        component,
        nav_group,
        nav_order,
        description,
        visibility,
    };
}
function inferPathFromRelPath(relPath) {
    const withoutLocale = relPath.replace(/^(en|es)\//, "");
    const withoutExt = withoutLocale.replace(/\.(md|mdx)$/, "");
    if (withoutExt === "home")
        return "/";
    return "/" + withoutExt;
}
function inferTitleFromPath(routePath) {
    const last = routePath.split("/").pop() ?? routePath;
    return (last
        .replace(/^\//, "")
        .replace(/[-_]+/g, " ")
        .replace(/\b([a-z])/g, (_, c) => c.toUpperCase()) || "Home");
}
//# sourceMappingURL=site_page_frontmatter.js.map