import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { resolve, join } from "node:path";
function listSpecialists(dir) {
    if (!existsSync(dir))
        return [];
    const out = [];
    for (const entry of readdirSync(dir)) {
        const full = join(dir, entry);
        const st = statSync(full);
        if (st.isFile() && entry.endsWith(".md"))
            out.push(entry.replace(/\.md$/, ""));
        else if (st.isDirectory() && existsSync(join(full, "IDENTITY.md")))
            out.push(entry);
    }
    return out;
}
function slugify(s) {
    return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
}
export function runCatalogueCensus(input) {
    const platformSpecs = listSpecialists(resolve(input.platformRoot, "templates", "specialists", "agents"));
    const premiumSpecs = [];
    for (const bundle of input.enabledBundles) {
        const dir = resolve(input.premiumPluginsRoot, bundle, "agents");
        premiumSpecs.push(...listSpecialists(dir));
    }
    const disk = [...platformSpecs, ...premiumSpecs];
    const body = existsSync(input.shippedSkillPath)
        ? readFileSync(input.shippedSkillPath, "utf8")
        : "";
    const bodySlugs = new Set();
    for (const match of body.matchAll(/^\|\s*([^|]+)\s*\|/gm)) {
        const cell = match[1].trim();
        if (!cell || cell.startsWith("---") || cell === "Role" || cell === "Plugin")
            continue;
        bodySlugs.add(slugify(cell));
    }
    const missing = disk.filter((s) => !bodySlugs.has(slugify(s)));
    // Stale signal arrives via the per-brand drift gate and the
    // check-roles-doc-completeness gate; this census closes the F5 leak —
    // disk-but-undocumented — which the drift gates cannot see.
    const stale = [];
    return {
        diskCount: disk.length,
        documentedCount: bodySlugs.size,
        missing,
        stale,
    };
}
//# sourceMappingURL=catalogue-census.js.map