"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.AutoBeMockAgent = void 0;
const tstl_1 = require("tstl");
const uuid_1 = require("uuid");
const AutoBeAgentBase_1 = require("./AutoBeAgentBase");
const AutoBeTokenUsage_1 = require("./context/AutoBeTokenUsage");
const createAutoBeState_1 = require("./factory/createAutoBeState");
/** @internal */
class AutoBeMockAgent extends AutoBeAgentBase_1.AutoBeAgentBase {
    constructor(props) {
        super({
            compiler: () => this.compiler_.get(),
            state: () => (0, createAutoBeState_1.createAutoBeState)(this.histories_),
        });
        this.props_ = props;
        this.histories_ = [];
        this.compiler_ = new tstl_1.Singleton(() => __awaiter(this, void 0, void 0, function* () {
            return props.compiler({
                realize: {
                    test: {
                        onOperation: () => __awaiter(this, void 0, void 0, function* () { }),
                        onReset: () => __awaiter(this, void 0, void 0, function* () { }),
                    },
                },
            });
        }));
        this.token_usage_ = new AutoBeTokenUsage_1.AutoBeTokenUsage();
    }
    conversate(content) {
        return __awaiter(this, void 0, void 0, function* () {
            // THE USER-MESSAGE
            const userMessage = {
                id: (0, uuid_1.v7)(),
                type: "userMessage",
                contents: typeof content === "string"
                    ? [
                        {
                            type: "text",
                            text: content,
                        },
                    ]
                    : Array.isArray(content)
                        ? content
                        : [content],
                created_at: new Date().toISOString(),
            };
            void this.dispatch(userMessage).catch(() => { });
            // ALREADY REALIZED CASE
            const state = (0, createAutoBeState_1.createAutoBeState)(this.histories_);
            if (state.realize !== null) {
                yield (0, tstl_1.sleep_for)(2000);
                const assistantMessage = {
                    id: (0, uuid_1.v7)(),
                    type: "assistantMessage",
                    text: [
                        "AutoBE has successfully realized the application.",
                        "",
                        "Thanks for using AutoBE!",
                    ].join("\n"),
                    created_at: new Date().toISOString(),
                    completed_at: new Date().toISOString(),
                };
                void this.dispatch(assistantMessage).catch(() => { });
                this.histories_.push(userMessage, assistantMessage);
                return this.histories_;
            }
            const take = (type) => __awaiter(this, void 0, void 0, function* () {
                var _a;
                const snapshots = this.getEventSnapshots(type);
                if (snapshots === null) {
                    this.histories_.push(userMessage);
                    this.histories_.push({
                        id: (0, uuid_1.v7)(),
                        type: "assistantMessage",
                        text: [
                            "The histories are prepared until current state.",
                            "",
                            "Thanks for using AutoBE!",
                        ].join("\n"),
                        created_at: new Date().toISOString(),
                        completed_at: new Date().toISOString(),
                    });
                    return;
                }
                for (const s of snapshots) {
                    const time = (_a = sleepMap[s.event.type]) !== null && _a !== void 0 ? _a : 500;
                    yield (0, tstl_1.sleep_for)((0, tstl_1.randint)(time * 0.2, time * 1.8));
                    void this.dispatch(s.event).catch(() => { });
                    this.token_usage_ = new AutoBeTokenUsage_1.AutoBeTokenUsage(s.tokenUsage);
                }
                this.histories_.push(userMessage);
                this.histories_.push(this.props_.replay.histories.find((h) => h.type === type));
            });
            if (state.analyze === null)
                yield take("analyze");
            else if (state.prisma === null)
                yield take("prisma");
            else if (state.interface === null)
                yield take("interface");
            else if (state.test === null)
                yield take("test");
            else if (state.realize === null)
                yield take("realize");
            return this.histories_;
        });
    }
    getHistories() {
        return this.histories_;
    }
    getTokenUsage() {
        return this.token_usage_;
    }
    getPhase() {
        var _a, _b, _c, _d;
        const state = (0, createAutoBeState_1.createAutoBeState)(this.histories_);
        if (state.analyze === null)
            return null;
        else if (((_a = state.realize) === null || _a === void 0 ? void 0 : _a.step) === state.analyze.step)
            return "realize";
        else if (((_b = state.test) === null || _b === void 0 ? void 0 : _b.step) === state.analyze.step)
            return "test";
        else if (((_c = state.interface) === null || _c === void 0 ? void 0 : _c.step) === state.analyze.step)
            return "interface";
        else if (((_d = state.prisma) === null || _d === void 0 ? void 0 : _d.step) === state.analyze.step)
            return "prisma";
        return "analyze";
    }
    getEventSnapshots(state) {
        var _a;
        return (_a = this.props_.replay[state]) !== null && _a !== void 0 ? _a : null;
    }
}
exports.AutoBeMockAgent = AutoBeMockAgent;
const sleepMap = {
    userMessage: 1000,
    assistantMessage: 1000,
    vendorRequest: 0,
    vendorResponse: 0,
    vendorTimeout: 0,
    jsonParseError: 0,
    jsonValidateError: 0,
    consentFunctionCall: 0,
    // ANALYZE
    analyzeStart: 1000,
    analyzeScenario: 1000,
    analyzeWrite: 500,
    analyzeReview: 300,
    analyzeComplete: 1000,
    // PRISMA
    prismaStart: 1000,
    prismaComponents: 1000,
    prismaSchemas: 500,
    prismaInsufficient: 1000,
    prismaReview: 500,
    prismaValidate: 2000,
    prismaCorrect: 500,
    prismaComplete: 1000,
    // INTERFACE
    interfaceStart: 1000,
    interfaceGroups: 1000,
    interfaceEndpoints: 1000,
    interfaceEndpointsReview: 1000,
    interfaceOperations: 400,
    interfaceOperationsReview: 400,
    interfaceAuthorization: 400,
    interfaceSchemas: 400,
    interfaceSchemasReview: 400,
    interfaceComplement: 2000,
    interfaceComplete: 1000,
    // TEST
    testStart: 1000,
    testScenarios: 40,
    testScenariosReview: 40,
    testWrite: 40,
    testValidate: 100,
    testCorrect: 100,
    testComplete: 1000,
    // REALIZE
    realizeStart: 1000,
    realizeComplete: 1000,
    realizeWrite: 80,
    realizeCorrect: 80,
    realizeValidate: 200,
    realizeAuthorizationStart: 1000,
    realizeAuthorizationWrite: 200,
    realizeAuthorizationValidate: 200,
    realizeAuthorizationCorrect: 200,
    realizeAuthorizationComplete: 1000,
    realizeTestStart: 1000,
    realizeTestReset: 2500,
    realizeTestOperation: 400,
    realizeTestComplete: 1000,
};
//# sourceMappingURL=AutoBeMockAgent.js.map