"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.orchestratePrisma = void 0;
const uuid_1 = require("uuid");
const predicateStateMessage_1 = require("../../utils/predicateStateMessage");
const orchestratePrismaComponent_1 = require("./orchestratePrismaComponent");
const orchestratePrismaCorrect_1 = require("./orchestratePrismaCorrect");
const orchestratePrismaReview_1 = require("./orchestratePrismaReview");
const orchestratePrismaSchemas_1 = require("./orchestratePrismaSchemas");
const orchestratePrisma = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b, _c, _d;
    // PREDICATION
    const start = new Date();
    const predicate = (0, predicateStateMessage_1.predicateStateMessage)(ctx.state(), "prisma");
    if (predicate !== null)
        return ctx.assistantMessage({
            type: "assistantMessage",
            id: (0, uuid_1.v7)(),
            created_at: start.toISOString(),
            text: predicate,
            completed_at: new Date().toISOString(),
        });
    ctx.dispatch({
        type: "prismaStart",
        id: (0, uuid_1.v7)(),
        created_at: start.toISOString(),
        reason: props.instruction,
        step: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0,
    });
    // COMPONENTS
    const componentEvent = yield (0, orchestratePrismaComponent_1.orchestratePrismaComponents)(ctx, props.instruction);
    ctx.dispatch(componentEvent);
    // CONSTRUCT AST DATA
    const schemaEvents = yield (0, orchestratePrismaSchemas_1.orchestratePrismaSchemas)(ctx, props.instruction, componentEvent.components);
    const application = {
        files: schemaEvents.map((e) => e.file),
    };
    // REVIEW
    const compiler = yield ctx.compiler();
    const reviewSchemas = yield compiler.prisma.write(application, "postgres");
    const reviewEvents = yield (0, orchestratePrismaReview_1.orchestratePrismaReview)(ctx, application, reviewSchemas, componentEvent.components);
    for (const event of reviewEvents) {
        const file = application.files.find((f) => f.filename === event.filename);
        if (file === undefined)
            continue;
        for (const modification of event.modifications) {
            const index = file.models.findIndex((m) => m.name === modification.name);
            if (index === -1)
                file.models.push(modification);
            else
                file.models[index] = modification;
        }
    }
    // VALIDATE
    const result = yield (0, orchestratePrismaCorrect_1.orchestratePrismaCorrect)(ctx, application);
    const finalSchemas = yield compiler.prisma.write(result.data, "postgres");
    // PROPAGATE
    return ctx.dispatch({
        type: "prismaComplete",
        id: (0, uuid_1.v7)(),
        result,
        schemas: finalSchemas,
        compiled: yield compiler.prisma.compile({
            files: finalSchemas,
        }),
        step: (_d = (_c = ctx.state().analyze) === null || _c === void 0 ? void 0 : _c.step) !== null && _d !== void 0 ? _d : 0,
        elapsed: new Date().getTime() - start.getTime(),
        created_at: new Date().toISOString(),
    });
});
exports.orchestratePrisma = orchestratePrisma;
//# sourceMappingURL=orchestratePrisma.js.map