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());
    });
};
import { Observable } from 'rxjs';
import { serializeError } from 'serialize-error';
export function withTimeout(promise, timeout, abortController) {
    return __awaiter(this, void 0, void 0, function* () {
        return Promise.race([
            promise,
            new Promise((_, reject) => {
                const timer = setTimeout(() => {
                    abortController === null || abortController === void 0 ? void 0 : abortController.abort('timeout');
                    clearTimeout(timer);
                    reject(new DOMException('timeout', 'AbortError'));
                }, timeout);
            }),
        ]);
    });
}
export function promiseToObservable(promiseFactory) {
    return new Observable((observer) => {
        promiseFactory()
            .then((response) => {
            observer.next(response);
            observer.complete();
        })
            .catch((error) => {
            const serializedError = serializeError(error);
            console.log('----promiseToObservable error', JSON.stringify(serializedError, null, 2));
            observer.error(error);
        });
    });
}
export function convertTimeToMilliseconds(timeValue) {
    if (typeof timeValue === 'number') {
        return timeValue;
    }
    const numericValue = parseFloat(timeValue);
    const unit = timeValue
        .replace(numericValue.toString(), '')
        .trim()
        .toLowerCase();
    switch (unit) {
        case 'ms':
        case '':
            return numericValue;
        case 's':
            return numericValue * 1000;
        case 'm':
            return numericValue * 60 * 1000;
        case 'h':
            return numericValue * 60 * 60 * 1000;
        case 'd':
            return numericValue * 24 * 60 * 60 * 1000;
        default:
            throw new Error(`Invalid time unit: ${unit}`);
    }
}
export function debounce(func, timeout = 300) {
    let timer;
    return (...args) => {
        clearTimeout(timer);
        timer = setTimeout(() => {
            func.apply(this, args);
        }, timeout);
    };
}
//# sourceMappingURL=helpers.js.map