"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.getAutoBeGenerated = getAutoBeGenerated;
const getAutoBeRealizeGenerated_1 = require("./getAutoBeRealizeGenerated");
function getAutoBeGenerated(compiler, state, histories, tokenUsage, options) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b, _c, _d, _e, _f, _g;
        // ANALYZE
        const ret = {};
        if (state.analyze === null)
            return {};
        Object.assign(ret, Object.fromEntries(state.analyze.files.map((file) => [
            `docs/analysis/${file.filename.split("/").at(-1)}`,
            file.content,
        ])));
        if ((options === null || options === void 0 ? void 0 : options.stage) === "analyze")
            return ret;
        // PRISMA
        if (((_a = state.prisma) === null || _a === void 0 ? void 0 : _a.step) === state.analyze.step) {
            const schemaFiles = ((_b = options === null || options === void 0 ? void 0 : options.dbms) !== null && _b !== void 0 ? _b : "postgres") === "postgres"
                ? state.prisma.schemas
                : yield compiler.prisma.write(state.prisma.result.data, options.dbms);
            Object.assign(ret, Object.fromEntries(Object.entries(schemaFiles).map(([key, value]) => [
                `prisma/schema/${key.split("/").at(-1)}`,
                value,
            ])), {
                "autobe/prisma.json": JSON.stringify(state.prisma.result.data),
            });
            if (state.prisma.compiled.type === "success")
                ret["docs/ERD.md"] = state.prisma.compiled.document;
            else if (state.prisma.compiled.type === "failure")
                ret["prisma/compile-error-reason.log"] = state.prisma.compiled.reason;
        }
        if ((options === null || options === void 0 ? void 0 : options.stage) === "prisma")
            return ret;
        // INTERFACE
        if (((_c = state.interface) === null || _c === void 0 ? void 0 : _c.step) === state.analyze.step) {
            const files = yield compiler.interface.write(state.interface.document);
            Object.assign(ret, ((_d = state.test) === null || _d === void 0 ? void 0 : _d.step) === state.interface.step
                ? Object.fromEntries(Object.entries(files).filter(([key]) => key.startsWith("test/features/") === false))
                : files, {
                "autobe/document.json": JSON.stringify(state.interface.document),
            });
        }
        if ((options === null || options === void 0 ? void 0 : options.stage) === "interface")
            return ret;
        // TEST
        if (((_e = state.test) === null || _e === void 0 ? void 0 : _e.step) === state.analyze.step)
            Object.assign(ret, Object.fromEntries(state.test.files.map((f) => [f.location, f.content])), yield compiler.test.getTemplate());
        // REALIZE
        if (((_f = state.realize) === null || _f === void 0 ? void 0 : _f.step) === state.analyze.step)
            Object.assign(ret, yield (0, getAutoBeRealizeGenerated_1.getAutoBeRealizeGenerated)({
                compiler,
                document: state.interface.document,
                authorizations: state.realize.authorizations,
                functions: state.realize.functions,
                options: {
                    dbms: (_g = options === null || options === void 0 ? void 0 : options.dbms) !== null && _g !== void 0 ? _g : "postgres",
                },
            }));
        if ((options === null || options === void 0 ? void 0 : options.stage) === "test")
            return ret;
        // LOGGING
        Object.assign(ret, {
            "autobe/histories.json": JSON.stringify(histories),
            "autobe/tokenUsage.json": JSON.stringify(tokenUsage),
        });
        return ret;
    });
}
//# sourceMappingURL=getAutoBeGenerated.js.map