"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SemanticsExtractCommand = void 0;
const commander_1 = require("commander");
const chalk_1 = __importDefault(require("chalk"));
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const LivingDictionary_1 = require("../services/LivingDictionary");
exports.SemanticsExtractCommand = new commander_1.Command("semantics:extract")
    .description("Discover terms from text and add to the dictionary")
    .argument("[source-text]", "text to extract terms from")
    .option("--from-context", "scan context.json features for terms")
    .option("--json", "output as JSON")
    .action(async (sourceText, options) => {
    const dict = new LivingDictionary_1.LivingDictionary();
    await dict.load();
    const discovered = [];
    if (sourceText) {
        const terms = await dict.discoverTerms(sourceText);
        discovered.push(...terms);
    }
    if (options.fromContext) {
        const brainPath = path.join(process.cwd(), ".mycontext", "context.json");
        if (await fs.pathExists(brainPath)) {
            const brain = await fs.readJson(brainPath);
            if (brain?.features) {
                for (const feat of brain.features) {
                    const text = `${feat.name} ${feat.description} ${(feat.acceptanceCriteria || []).join(" ")}`;
                    const terms = await dict.discoverTerms(text);
                    discovered.push(...terms);
                }
            }
            if (brain?.prd?.goals) {
                const terms = await dict.discoverTerms(brain.prd.goals.join(" "));
                discovered.push(...terms);
            }
        }
    }
    await dict.save();
    if (options.json) {
        console.log(JSON.stringify(discovered, null, 2));
        return;
    }
    if (discovered.length === 0) {
        console.log(chalk_1.default.dim("\n  No new terms discovered\n"));
        return;
    }
    console.log(chalk_1.default.green(`\n  ✓ Discovered ${discovered.length} term(s)`));
    for (const term of discovered) {
        console.log(`     ${chalk_1.default.cyan(term.term)} ${chalk_1.default.dim(`(${term.status})`)}`);
    }
    console.log();
});
//# sourceMappingURL=semantics-extract.js.map