var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _DeferredImpl_resolve, _DeferredImpl_reject, _DeferredImpl_promise, _DeferredImpl_status, _DeferredImpl_error, _DeferredImpl_value;
class DeferredImpl {
    constructor() {
        _DeferredImpl_resolve.set(this, void 0);
        _DeferredImpl_reject.set(this, void 0);
        _DeferredImpl_promise.set(this, void 0);
        _DeferredImpl_status.set(this, void 0);
        _DeferredImpl_error.set(this, void 0);
        _DeferredImpl_value.set(this, void 0);
        __classPrivateFieldSet(this, _DeferredImpl_status, 'pending', "f");
        __classPrivateFieldSet(this, _DeferredImpl_promise, new Promise((res, rej) => {
            __classPrivateFieldSet(this, _DeferredImpl_resolve, res, "f");
            __classPrivateFieldSet(this, _DeferredImpl_reject, rej, "f");
        }), "f");
    }
    get status() {
        return __classPrivateFieldGet(this, _DeferredImpl_status, "f");
    }
    getOrThrow() {
        if (this.status === 'pending') {
            throw new Error('Deferred not resolved');
        }
        if (this.status === 'rejected') {
            throw __classPrivateFieldGet(this, _DeferredImpl_error, "f");
        }
        return __classPrivateFieldGet(this, _DeferredImpl_value, "f");
    }
    getValue() {
        if (this.status === 'pending') {
            return __classPrivateFieldGet(this, _DeferredImpl_promise, "f");
        }
        if (this.status === 'rejected') {
            // eslint-disable-next-line @typescript-eslint/prefer-promise-reject-errors
            return Promise.reject(__classPrivateFieldGet(this, _DeferredImpl_error, "f"));
        }
        return Promise.resolve(__classPrivateFieldGet(this, _DeferredImpl_value, "f"));
    }
    resolve(value) {
        if (__classPrivateFieldGet(this, _DeferredImpl_status, "f") === 'resolved') {
            throw new Error('Deferred already resolved');
        }
        else if (__classPrivateFieldGet(this, _DeferredImpl_status, "f") === 'rejected') {
            throw new Error('Deferred already rejected');
        }
        __classPrivateFieldSet(this, _DeferredImpl_status, 'resolved', "f");
        __classPrivateFieldSet(this, _DeferredImpl_value, value, "f");
        __classPrivateFieldGet(this, _DeferredImpl_resolve, "f").call(this, value);
    }
    reject(reason) {
        if (__classPrivateFieldGet(this, _DeferredImpl_status, "f") === 'resolved') {
            throw new Error('Deferred already resolved');
        }
        else if (__classPrivateFieldGet(this, _DeferredImpl_status, "f") === 'rejected') {
            throw new Error('Deferred already rejected');
        }
        __classPrivateFieldSet(this, _DeferredImpl_status, 'rejected', "f");
        __classPrivateFieldSet(this, _DeferredImpl_error, reason, "f");
        __classPrivateFieldGet(this, _DeferredImpl_reject, "f").call(this, reason);
    }
}
_DeferredImpl_resolve = new WeakMap(), _DeferredImpl_reject = new WeakMap(), _DeferredImpl_promise = new WeakMap(), _DeferredImpl_status = new WeakMap(), _DeferredImpl_error = new WeakMap(), _DeferredImpl_value = new WeakMap();
/**
 * Create a deferred promise
 */
export function deferred() {
    return new DeferredImpl();
}
/**
 * Check if a value is a Deferred
 */
export function isDeferred(value) {
    // extra checks so instanceof doesn't throw
    return value && typeof value === 'object' && value instanceof DeferredImpl;
}
//# sourceMappingURL=deferred.js.map