import { existsSync, readFileSync } from "node:fs";
import { unwrapKitJsonExportPayload } from "../../core/kit-export-envelope.js";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
let slugSet = null;
function resolveFeatureTaxonomyPath() {
    const here = dirname(fileURLToPath(import.meta.url));
    const fromPackageRoot = join(here, "..", "..", "..", "src", "modules", "documentation", "data", "feature-taxonomy.json");
    if (existsSync(fromPackageRoot)) {
        return fromPackageRoot;
    }
    return join(here, "..", "..", "..", "..", "src", "modules", "documentation", "data", "feature-taxonomy.json");
}
function loadKnownFeatureSlugs() {
    if (slugSet) {
        return slugSet;
    }
    slugSet = loadKnownFeatureSlugsFromJsonFile();
    return slugSet;
}
/** Load taxonomy slugs from shipped `feature-taxonomy.json` (no memo — prefer `loadKnownFeatureSlugs()` for hot path). */
export function loadKnownFeatureSlugsFromJsonFile() {
    const path = resolveFeatureTaxonomyPath();
    const raw = unwrapKitJsonExportPayload(JSON.parse(readFileSync(path, "utf8")));
    const next = new Set();
    for (const f of raw.features ?? []) {
        if (typeof f.slug === "string" && f.slug.length > 0) {
            next.add(f.slug);
        }
    }
    return next;
}
/** Advisory warnings for slugs not present in the known set (registry or JSON fallback). */
export function collectUnknownFeatureSlugWarnings(features, knownSlugs) {
    if (!features?.length) {
        return [];
    }
    const known = knownSlugs ?? loadKnownFeatureSlugs();
    const unknown = features.filter((s) => !known.has(s));
    if (unknown.length === 0) {
        return [];
    }
    return [`unknown feature slug(s) not in taxonomy: ${unknown.join(", ")}`];
}
