import { Subject, from, empty, } from "rxjs";
import { mergeMap, tap, catchError, startWith, switchMap, } from "rxjs/operators";
import { axiosInstance } from "./axios-instance";
export const CHART_UNMOUNTED = "Chart scrolled out of view";
const METRICS_TIMEOUT = 15000;
export const getFetchStream = (concurrentCallsLimit) => {
    const fetch$ = new Subject();
    const resetFetch$ = new Subject();
    const handler = mergeMap(({ url, method = "GET", params = {}, data, onErrorCallback, onSuccessCallback, cancelTokenSource, }) => {
        var _a;
        return (from(axiosInstance.request({
            url,
            method,
            params,
            data,
            timeout: METRICS_TIMEOUT,
            cancelToken: (_a = cancelTokenSource) === null || _a === void 0 ? void 0 : _a.token,
        })).pipe(tap(({ data: responseData }) => { onSuccessCallback(responseData); }), catchError((error) => {
            var _a;
            // todo implement error handling to support NETDATA.options.current.retries_on_data_failures
            if (((_a = error) === null || _a === void 0 ? void 0 : _a.message) !== CHART_UNMOUNTED) {
                console.warn("fetch error", url); // eslint-disable-line no-console
            }
            onErrorCallback(error);
            return empty();
        })));
    }, concurrentCallsLimit);
    const output = resetFetch$.pipe(startWith(null), switchMap(() => fetch$.pipe(handler)));
    output.subscribe();
    return [fetch$, resetFetch$];
};
//# sourceMappingURL=metrics-stream.js.map