"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.TimedConversation = void 0;
const tstl_1 = require("tstl");
const AutoBeTimeoutError_1 = require("./AutoBeTimeoutError");
var TimedConversation;
(function (TimedConversation) {
    TimedConversation.process = (props) => __awaiter(this, void 0, void 0, function* () {
        if (props.timeout === null)
            try {
                const histories = yield props.agent.conversate(props.message);
                return {
                    type: "success",
                    histories,
                };
            }
            catch (error) {
                return {
                    type: "error",
                    error: error,
                };
            }
        // PREPARE TIMEOUT HANDLERS
        const result = {
            value: null,
        };
        const holder = new tstl_1.ConditionVariable();
        const abort = new AbortController();
        const timeout = new tstl_1.Singleton(() => setTimeout(() => {
            if (result.value !== null)
                return;
            result.value = {
                type: "timeout",
                error: new AutoBeTimeoutError_1.AutoBeTimeoutError(`Timeout, over ${props.timeout} ms.`),
            };
            abort.abort(`Timeout, over ${props.timeout} ms`);
            void holder.notify_all().catch(() => { });
        }, props.timeout));
        // DO CONVERSATE
        props.agent.on("request", () => {
            timeout.get();
        });
        props.agent
            .conversate(props.message, {
            abortSignal: abort.signal,
        })
            .then((v) => {
            var _a;
            return ((_a = result.value) !== null && _a !== void 0 ? _a : (result.value = {
                type: "success",
                histories: v,
            }));
        })
            .catch((e) => {
            var _a;
            return ((_a = result.value) !== null && _a !== void 0 ? _a : (result.value = {
                type: "error",
                error: e,
            }));
        })
            .finally(() => {
            void holder.notify_all().catch(() => { });
            clearTimeout(timeout.get());
        });
        yield holder.wait();
        yield (0, tstl_1.sleep_for)(0);
        return result.value;
    });
})(TimedConversation || (exports.TimedConversation = TimedConversation = {}));
//# sourceMappingURL=TimedConversation.js.map