"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;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeatureGraphCompiler = void 0;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const core_1 = require("@myycontext/core");
class FeatureGraphCompiler {
    constructor(brainPath) {
        this.brainPath = brainPath || path.join(process.cwd(), ".mycontext", "context.json");
    }
    async compileFromContext(context) {
        const ctx = context || await this.loadContext();
        const features = ctx.features || [];
        const allComponents = ctx.components || [];
        const allRoutes = ctx.routes || [];
        const allActions = ctx.actions || [];
        const featureNodes = [];
        const dependencyMap = {};
        for (const feat of features) {
            dependencyMap[feat.id] = feat.dependencies || [];
            const ownership = {
                routes: feat.ownership?.routes || feat.routeIds || [],
                components: feat.ownership?.components || feat.componentIds || [],
                actions: feat.ownership?.actions || feat.actionIds || [],
                models: feat.ownership?.models || [],
                permissions: feat.ownership?.permissions || [],
                i18n: feat.ownership?.i18n || [],
                tests: feat.ownership?.tests || [],
            };
            const existing = {
                routes: ownership.routes.filter((r) => allRoutes.some((ar) => ar.path === r)),
                components: ownership.components.filter((c) => allComponents.some((ac) => ac.name === c)),
                actions: ownership.actions.filter((a) => allActions.some((aa) => aa.name === a)),
                models: [],
                permissions: [],
                i18nKeys: [],
                testFiles: [],
            };
            const completeness = (0, core_1.computeFeatureCompleteness)(ownership, existing);
            const node = (0, core_1.createFeatureNode)(feat.id, feat.name, feat.description, feat.priority, feat.status || "planned", ownership, feat.acceptanceCriteria);
            node.completeness = completeness;
            featureNodes.push(node);
        }
        return (0, core_1.createFeatureGraph)(featureNodes, dependencyMap);
    }
    async detectGaps(feature) {
        const gaps = [];
        const ctx = await this.loadContext();
        const dims = [
            { key: "routes", label: "Routes" },
            { key: "components", label: "Components" },
            { key: "actions", label: "Actions" },
            { key: "models", label: "Models" },
            { key: "permissions", label: "Permissions" },
            { key: "i18n", label: "i18n" },
            { key: "tests", label: "Tests" },
        ];
        for (const dim of dims) {
            const items = feature.ownership[dim.key];
            if (items.length === 0) {
                gaps.push({
                    dimension: dim.label,
                    severity: "unowned",
                    message: `No ${dim.label.toLowerCase()} declared for feature`,
                    suggested: [],
                });
            }
        }
        return gaps;
    }
    async getNextRecommended() {
        const graph = await this.compileFromContext();
        return graph.getNextRecommended();
    }
    async loadContext() {
        try {
            if (await fs.pathExists(this.brainPath)) {
                return await fs.readJson(this.brainPath);
            }
        }
        catch { }
        return {};
    }
}
exports.FeatureGraphCompiler = FeatureGraphCompiler;
//# sourceMappingURL=FeatureGraphCompiler.js.map