/**
 * Express router that serves bundled docs at `/docs` (index) and
 * `/docs/:slug` (page) with content negotiation.
 *
 * - `Accept: text/html` (default for browsers) → full HTML page with
 *   `noindex,nofollow` and a canonical link to the marketing SPA.
 * - `Accept: text/markdown` (or `?format=md`) → raw markdown body.
 * - `Accept: application/json` (or `?format=json`) → `{ frontmatter, body, ... }`.
 *
 * The router is mounted from `src/actions.ts`.
 */
import express from "express";
import { wantsHtml, wantsMarkdown } from "../root_landing/index.js";
import { loadAllInstalledDocs, loadInstalledDoc, } from "./loader.js";
import { renderDocAsHtml, renderDocAsJson, renderDocIndexHtml, renderDocIndexJson, } from "./renderer.js";
const SLUG_RE = /^[a-z][a-z0-9-]*$/;
function wantsJson(req) {
    if ((req.query.format ?? "") === "json")
        return true;
    const accept = req.header("accept");
    if (!accept)
        return false;
    if (wantsHtml(accept))
        return false;
    if (wantsMarkdown(accept))
        return false;
    return accept.toLowerCase().includes("application/json");
}
function wantsRawMarkdown(req) {
    if ((req.query.format ?? "") === "md")
        return true;
    if ((req.query.format ?? "") === "markdown")
        return true;
    return wantsMarkdown(req.header("accept"));
}
function buildRenderCtx(req, options) {
    return {
        publicDocsUrl: options.resolvePublicDocsUrl(req),
        inspectorUrl: options.resolveInspectorUrl(req),
    };
}
/**
 * Bucket D — historical deep paths from `docs/subsystems/*` and
 * `docs/proposals/*` redirect to their flat-slug Path W counterparts so the
 * install matches the marketing site's URL shape.
 */
const DEEP_PATH_REDIRECTS = {
    "/subsystems/agent_attribution_integration": "/docs/agent-attribution-integration",
    "/proposals/agent-trust-framework": "/docs/agent-trust-framework",
};
export function createDocsInstallRouter(options) {
    const router = express.Router();
    for (const [from, to] of Object.entries(DEEP_PATH_REDIRECTS)) {
        router.get(from, (_req, res) => {
            res.redirect(308, to);
        });
    }
    router.get("/", (req, res) => {
        const docs = loadAllInstalledDocs();
        const ctx = buildRenderCtx(req, options);
        if (wantsJson(req)) {
            res.json(renderDocIndexJson(docs, ctx));
            return;
        }
        if (wantsRawMarkdown(req)) {
            const lines = [
                "# Bundled product docs",
                "",
                `Canonical site: ${ctx.publicDocsUrl}`,
                "",
                ...docs.map((d) => `- [${d.frontmatter.title}](/docs/${d.slug}) — ${d.frontmatter.description}`),
                "",
            ];
            res
                .type("text/markdown; charset=utf-8")
                .set("X-Robots-Tag", "noindex, nofollow")
                .send(lines.join("\n"));
            return;
        }
        res
            .type("text/html; charset=utf-8")
            .set("X-Robots-Tag", "noindex, nofollow")
            .send(renderDocIndexHtml(docs, ctx));
    });
    router.get("/:slug", (req, res) => {
        const slug = req.params.slug || "";
        if (!SLUG_RE.test(slug)) {
            res.status(404).type("text/plain").send("doc not found");
            return;
        }
        const doc = loadInstalledDoc(slug);
        if (!doc) {
            res.status(404).type("text/plain").send(`doc not found: ${slug}`);
            return;
        }
        const ctx = buildRenderCtx(req, options);
        if (wantsJson(req)) {
            res
                .set("X-Robots-Tag", "noindex, nofollow")
                .json(renderDocAsJson(doc, ctx));
            return;
        }
        if (wantsRawMarkdown(req)) {
            res
                .type("text/markdown; charset=utf-8")
                .set("X-Robots-Tag", "noindex, nofollow")
                .send(doc.body);
            return;
        }
        res
            .type("text/html; charset=utf-8")
            .set("X-Robots-Tag", "noindex, nofollow")
            .send(renderDocAsHtml(doc, ctx));
    });
    return router;
}
//# sourceMappingURL=routes.js.map