"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OperationPipeline = void 0;
const core_1 = require("@myycontext/core");
const OperationExecutor_1 = require("./OperationExecutor");
class OperationPipeline {
    constructor(executor) {
        this.executor = executor || new OperationExecutor_1.OperationExecutor();
    }
    compileTaskToOps(task, featureId) {
        const ops = [];
        switch (task.taskType) {
            case "model": {
                ops.push((0, core_1.createOperation)("create-file", `Create model: ${task.name}`, {
                    path: `models/${task.name.toLowerCase().replace(/\s+/g, "-")}.ts`,
                    content: this.modelTemplate(task.name, featureId),
                }));
                break;
            }
            case "route": {
                ops.push((0, core_1.createOperation)("add-route", `Add route: ${task.name}`, {
                    path: task.outputPaths[0] || `app/${task.name.toLowerCase()}/page.tsx`,
                    component: task.name,
                }));
                break;
            }
            case "component": {
                ops.push((0, core_1.createOperation)("add-component", `Add component: ${task.name}`, {
                    name: task.name,
                    type: "client",
                    path: task.outputPaths[0],
                }, undefined, `batch-component`));
                break;
            }
            case "action": {
                ops.push((0, core_1.createOperation)("add-server-action", `Add action: ${task.name}`, {
                    file: task.outputPaths[0] || `actions/${task.name.toLowerCase()}.ts`,
                    name: task.name,
                }));
                break;
            }
            case "i18n": {
                ops.push((0, core_1.createOperation)("add-i18n-key", `Add i18n key: ${task.name}`, {
                    key: task.name.toLowerCase().replace(/\s+/g, "."),
                    value: task.name,
                }));
                break;
            }
            case "test": {
                ops.push((0, core_1.createOperation)("add-test", `Add test: ${task.name}`, {
                    file: task.outputPaths[0] || `__tests__/${task.name.toLowerCase().replace(/\s+/g, "-")}.test.ts`,
                    testName: task.name,
                    content: `describe('${task.name}', () => { it('should work', () => { expect(true).toBe(true); }); });`,
                }));
                break;
            }
            case "permission": {
                ops.push((0, core_1.createOperation)("add-permission", `Add permission: ${task.name}`, {
                    role: "user",
                    resource: task.name.toLowerCase(),
                    action: "read",
                }));
                break;
            }
        }
        return ops;
    }
    compileBatchToOps(batch, featureId) {
        const allOps = [];
        for (const task of batch.tasks) {
            const ops = this.compileTaskToOps(task, featureId);
            allOps.push(...ops);
        }
        return allOps;
    }
    async executeBatch(batch, featureId) {
        const ops = this.compileBatchToOps(batch, featureId);
        const results = await this.executor.executeBatch(ops);
        return { batch, operations: ops, results };
    }
    async executeGraph(graph) {
        const allResults = [];
        for (const batch of graph.batches) {
            const result = await this.executeBatch(batch);
            allResults.push(result);
        }
        return { results: allResults };
    }
    modelTemplate(name, featureId) {
        const varName = name.charAt(0).toLowerCase() + name.slice(1).replace(/\s+/g, "");
        return `export interface ${name.replace(/\s+/g, "")} {
  id: string;
  createdAt: string;
  updatedAt: string;
}\n`;
    }
}
exports.OperationPipeline = OperationPipeline;
//# sourceMappingURL=OperationPipeline.js.map