"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.orchestrateInterface = void 0;
const utils_1 = require("@autobe/utils");
const tstl_1 = require("tstl");
const uuid_1 = require("uuid");
const predicateStateMessage_1 = require("../../utils/predicateStateMessage");
const orchestrateInterfaceAuthorizations_1 = require("./orchestrateInterfaceAuthorizations");
const orchestrateInterfaceComplement_1 = require("./orchestrateInterfaceComplement");
const orchestrateInterfaceEndpoints_1 = require("./orchestrateInterfaceEndpoints");
const orchestrateInterfaceGroups_1 = require("./orchestrateInterfaceGroups");
const orchestrateInterfaceOperations_1 = require("./orchestrateInterfaceOperations");
const orchestrateInterfaceSchemas_1 = require("./orchestrateInterfaceSchemas");
const orchestrateInterfaceSchemasReview_1 = require("./orchestrateInterfaceSchemasReview");
const orchestrateInterface = (ctx) => (props) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b, _c, _d, _e, _f;
    // PREDICATION
    const start = new Date();
    const predicate = (0, predicateStateMessage_1.predicateStateMessage)(ctx.state(), "interface");
    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: "interfaceStart",
        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,
    });
    // ENDPOINTS
    const init = yield (0, orchestrateInterfaceGroups_1.orchestrateInterfaceGroups)(ctx, {
        instruction: props.instruction,
    });
    ctx.dispatch(init);
    // AUTHORIZATION
    const authorizations = yield (0, orchestrateInterfaceAuthorizations_1.orchestrateInterfaceAuthorizations)(ctx, props.instruction);
    const authOperations = authorizations
        .map((authorization) => authorization.operations)
        .flat();
    // ENDPOINTS & OPERATIONS
    const endpoints = yield (0, orchestrateInterfaceEndpoints_1.orchestrateInterfaceEndpoints)(ctx, {
        groups: init.groups,
        authorizations: authOperations,
        instruction: props.instruction,
    });
    const firstOperations = yield (0, orchestrateInterfaceOperations_1.orchestrateInterfaceOperations)(ctx, {
        endpoints,
        instruction: props.instruction,
    });
    const operations = new tstl_1.HashMap([...authOperations, ...firstOperations].map((o) => new tstl_1.Pair({
        path: o.path,
        method: o.method,
    }, o)), utils_1.AutoBeEndpointComparator.hashCode, utils_1.AutoBeEndpointComparator.equals)
        .toJSON()
        .map((it) => it.second);
    // TYPE SCHEMAS
    const document = {
        operations,
        components: {
            authorization: (_d = (_c = ctx.state().analyze) === null || _c === void 0 ? void 0 : _c.roles) !== null && _d !== void 0 ? _d : [],
            schemas: yield (0, orchestrateInterfaceSchemas_1.orchestrateInterfaceSchemas)(ctx, {
                instruction: props.instruction,
                operations,
            }),
        },
    };
    const complementedSchemas = yield (0, orchestrateInterfaceComplement_1.orchestrateInterfaceComplement)(ctx, {
        instruction: props.instruction,
        document,
    });
    Object.assign(document.components.schemas, complementedSchemas);
    const schemas = yield (0, orchestrateInterfaceSchemasReview_1.orchestrateInterfaceSchemasReview)(ctx, operations, document.components.schemas);
    Object.assign(document.components.schemas, schemas);
    for (const key of Object.keys(document.components.schemas))
        if (key === "IPageI" || key.startsWith("IPageI."))
            delete document.components.schemas[key];
    // DO COMPILE
    return ctx.dispatch({
        type: "interfaceComplete",
        id: (0, uuid_1.v7)(),
        document,
        authorizations,
        created_at: new Date().toISOString(),
        elapsed: new Date().getTime() - start.getTime(),
        step: (_f = (_e = ctx.state().analyze) === null || _e === void 0 ? void 0 : _e.step) !== null && _f !== void 0 ? _f : 0,
    });
});
exports.orchestrateInterface = orchestrateInterface;
//# sourceMappingURL=orchestrateInterface.js.map