"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.ImplGraphCompiler = void 0;
const path = __importStar(require("path"));
const core_1 = require("@myycontext/core");
class ImplGraphCompiler {
    constructor(brainPath) {
        this.brainPath = brainPath || path.join(process.cwd(), ".mycontext", "context.json");
    }
    async compileFromFeature(feature) {
        const tasks = [];
        const edges = [];
        const batches = [];
        const plan = this.deriveBatchPlan(feature);
        for (const batchDef of plan) {
            const batchTasks = [];
            for (const item of batchDef.items) {
                const taskId = `impl-${feature.id.toLowerCase()}-${batchDef.type}-${item.name.toLowerCase().replace(/\s+/g, "-")}`;
                const task = (0, core_1.createImplTaskNode)(taskId, batchDef.type, item.name, item.description, `batch-${batchDef.type}`, item.effort, item.outputPaths, [], feature.id);
                // Add dependency edges
                for (const dep of item.dependsOn) {
                    edges.push({
                        source: taskId,
                        target: dep,
                        type: "depends_on",
                    });
                }
                batchTasks.push(task);
            }
            if (batchTasks.length > 0) {
                const batch = (0, core_1.createImplBatch)(batchDef.type, batchDef.label, batchTasks);
                // If this batch depends on a previous batch type
                const prevBatch = batches[batches.length - 1];
                if (prevBatch) {
                    for (const task of batchTasks) {
                        for (const prevTask of prevBatch.tasks) {
                            edges.push({
                                source: task.id,
                                target: prevTask.id,
                                type: "depends_on",
                            });
                        }
                    }
                }
                tasks.push(...batchTasks);
                batches.push(batch);
            }
        }
        return (0, core_1.createImplementationGraph)(tasks, edges, batches);
    }
    deriveBatchPlan(feature) {
        const plan = (0, core_1.defaultBatchPlan)();
        const ownershipMap = {
            model: feature.ownership.models,
            route: feature.ownership.routes,
            component: feature.ownership.components,
            action: feature.ownership.actions,
            permission: feature.ownership.permissions,
            i18n: feature.ownership.i18n,
            test: feature.ownership.tests,
        };
        for (const batch of plan) {
            const items = ownershipMap[batch.type] || [];
            const prevItems = plan
                .slice(0, plan.indexOf(batch))
                .flatMap(b => b.items.map(i => i.name));
            batch.items = items.map(name => ({
                name,
                description: `${batch.type} for ${feature.name}: ${name}`,
                effort: "medium",
                dependsOn: [...prevItems],
                outputPaths: [`${batch.type}s/${name.toLowerCase().replace(/\s+/g, "-")}`],
            }));
        }
        return plan;
    }
}
exports.ImplGraphCompiler = ImplGraphCompiler;
//# sourceMappingURL=ImplGraphCompiler.js.map