"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.semanticHealthRules = void 0;
function diag(rule, filePath, message, opts = {}) {
    return {
        ruleId: rule.id,
        filePath,
        severity: opts.severity || rule.severity,
        message,
        help: opts.help || rule.help,
        autoFixable: opts.autoFixable ?? false,
    };
}
const semanticDictionaryPresent = {
    id: "semantic/dictionary-exists",
    name: "Semantic Dictionary exists",
    category: "i18n",
    severity: "warning",
    description: "Checks that a semantic dictionary is configured in the context",
    help: "Run `mycontext semantics:extract` to auto-discover terms from the codebase",
    appliesTo: ["node", "nextjs", "turbo"],
    async check(ctx) {
        const results = [];
        const data = await ctx.readJson(".mycontext/context.json");
        const dict = data?.brain?.context?.semanticDictionary;
        if (!dict) {
            results.push(diag(this, ".mycontext/context.json", "No semantic dictionary configured", { severity: "warning" }));
            return results;
        }
        const entries = dict.entries || dict;
        const entryCount = Object.keys(entries).length;
        if (entryCount === 0) {
            results.push(diag(this, ".mycontext/context.json", "Semantic dictionary exists but has no entries", { severity: "warning" }));
        }
        return results;
    },
};
const termDefinitionsComplete = {
    id: "semantic/definitions-complete",
    name: "Term definitions are complete",
    category: "i18n",
    severity: "warning",
    description: "Checks that all dictionary entries have descriptions/definitions",
    help: "Run `mycontext semantics:define <term> <definition>` for each term missing a definition",
    appliesTo: ["node", "nextjs", "turbo"],
    async check(ctx) {
        const results = [];
        const data = await ctx.readJson(".mycontext/context.json");
        const dict = data?.brain?.context?.semanticDictionary;
        if (!dict)
            return results;
        const entries = dict.entries || dict;
        for (const [term, entry] of Object.entries(entries)) {
            const e = entry;
            if (!e.definition && !e.description) {
                results.push(diag(this, ".mycontext/context.json", `Term "${term}" has no definition`, {
                    severity: "warning",
                    autoFixable: false,
                    help: `Run \`mycontext semantics:define ${term} "<definition>"\``,
                }));
            }
        }
        return results;
    },
};
const noUndefinedTerms = {
    id: "semantic/no-undefined-terms",
    name: "No undefined terms referenced",
    category: "i18n",
    severity: "warning",
    description: "Checks that terms referenced in feature specs or prompts are defined in the dictionary",
    help: "Run `mycontext semantics:extract --scan src/` to discover undocumented terms",
    appliesTo: ["node", "nextjs", "turbo"],
    async check(ctx) {
        const results = [];
        const data = await ctx.readJson(".mycontext/context.json");
        const features = data?.features || [];
        const dict = data?.brain?.context?.semanticDictionary;
        const definedTerms = dict ? new Set(Object.keys(dict.entries || dict)) : new Set();
        const referencedTerms = new Set();
        for (const f of features) {
            if (f.spec && typeof f.spec === "object") {
                for (const val of Object.values(f.spec)) {
                    const strVal = String(val);
                    const candidates = strVal.match(/\b[A-Z][a-z]+(?:[A-Z][a-z]+)+\b/g);
                    if (candidates) {
                        candidates.forEach((c) => referencedTerms.add(c));
                    }
                }
            }
        }
        if (referencedTerms.size > 0 && definedTerms.size === 0) {
            results.push(diag(this, ".mycontext/context.json", `${referencedTerms.size} terms referenced but no dictionary exists`, {
                severity: "warning",
                help: "Run `mycontext semantics:extract` to build the initial dictionary",
            }));
        }
        return results;
    },
};
exports.semanticHealthRules = [
    semanticDictionaryPresent,
    termDefinitionsComplete,
    noUndefinedTerms,
];
//# sourceMappingURL=semantic-health-rules.js.map