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());
    });
};
export function isPromise(value) {
    return (typeof value === 'object' &&
        value !== null &&
        typeof value.then === 'function');
}
function observableToPromise(observable) {
    return new Promise((resolve, reject) => {
        const subscription = observable.subscribe({
            next(v) {
                resolve(v);
                subscription.unsubscribe();
            },
            error: reject,
            complete() {
                reject(new Error('no value resolved'));
            },
        });
    });
}
export function isObservable(value) {
    return (typeof value === 'object' &&
        value !== null &&
        'subscribe' in value &&
        typeof value.subscribe === 'function');
}
export function isAsyncIterable(input) {
    return (typeof input === 'object' &&
        input !== null &&
        (input[Symbol.toStringTag] === 'AsyncGenerator' ||
            Symbol.asyncIterator in input));
}
function asyncIterableToPromise(input) {
    var _a;
    return __awaiter(this, void 0, void 0, function* () {
        const iteratorReturn = (_a = ('return' in input ? input : input[Symbol.asyncIterator]()).return) === null || _a === void 0 ? void 0 : _a.bind(input);
        const iteratorNext = ('next' in input ? input : input[Symbol.asyncIterator]()).next.bind(input);
        const result = yield iteratorNext();
        void (iteratorReturn === null || iteratorReturn === void 0 ? void 0 : iteratorReturn());
        return result.value;
    });
}
export function fetcherReturnToPromise(fetcherResult) {
    return __awaiter(this, void 0, void 0, function* () {
        const result = yield fetcherResult;
        if (isAsyncIterable(result)) {
            return asyncIterableToPromise(result);
        }
        if (isObservable(result)) {
            return observableToPromise(result);
        }
        return result;
    });
}
//# sourceMappingURL=index.js.map