"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.compileRealizeFiles = compileRealizeFiles;
const uuid_1 = require("uuid");
function compileRealizeFiles(ctx, props) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b, _c;
        const prisma = (_a = ctx.state().prisma) === null || _a === void 0 ? void 0 : _a.compiled;
        const payloads = Object.fromEntries(props.authorizations.map((el) => [el.payload.location, el.payload.content]));
        const compiler = yield ctx.compiler();
        const templateFiles = yield compiler.realize.getTemplate({
            dbms: "sqlite",
        });
        const nodeModules = (prisma === null || prisma === void 0 ? void 0 : prisma.type) === "success" ? prisma.nodeModules : {};
        const filterTsFiles = (location) => location.startsWith("src/") && location.endsWith(".ts");
        const files = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, nodeModules), payloads), Object.fromEntries(Object.entries(yield ctx.files({ dbms: "sqlite" })).filter(([key]) => filterTsFiles(key)))), Object.fromEntries(Object.entries(templateFiles).filter(([key]) => filterTsFiles(key)))), Object.fromEntries(props.functions.map((el) => [el.location, el.content])));
        const compiled = yield compiler.typescript.compile({
            files: files,
        });
        const event = {
            type: "realizeValidate",
            id: (0, uuid_1.v7)(),
            files: files,
            result: compiled,
            step: (_c = (_b = ctx.state().analyze) === null || _b === void 0 ? void 0 : _b.step) !== null && _c !== void 0 ? _c : 0,
            created_at: new Date().toISOString(),
        };
        return event;
    });
}
//# sourceMappingURL=compileRealizeFiles.js.map