"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeControlledPromise = void 0;
function makeControlledPromise() {
    let promise;
    let resolve;
    let reject;
    let result;
    return {
        then(onFulfilled, onRejected) {
            if (!promise) {
                promise = new Promise((...args) => ([resolve, reject] = args));
                if ((result === null || result === void 0 ? void 0 : result.status) === 'fulfilled')
                    resolve(result.value);
                else if ((result === null || result === void 0 ? void 0 : result.status) === 'rejected')
                    reject(result.reason);
            }
            return promise.then(onFulfilled, onRejected);
        },
        resolve(value) {
            if (resolve)
                resolve(value);
            else
                result !== null && result !== void 0 ? result : (result = { status: 'fulfilled', value });
        },
        reject(reason) {
            if (reject)
                reject(reason);
            else
                result !== null && result !== void 0 ? result : (result = { status: 'rejected', reason });
        },
    };
}
exports.makeControlledPromise = makeControlledPromise;
