"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useProviderRates = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const useFetchRates_1 = require("./useFetchRates");
const react_1 = require("react");
const usehooks_ts_1 = require("usehooks-ts");
const timeout_1 = require("../../const/timeout");
function useProviderRates({ fromState, toState, onNoRates, setExchangeRate, allowRefresh = true, isEnabled = true, ...props }) {
    const [countdown, { startCountdown, resetCountdown, stopCountdown }] = (0, usehooks_ts_1.useCountdown)({
        countStart: props.countdown ?? timeout_1.DEFAULT_SWAP_RATES_INTERVAL_MS / 1000,
        countStop: 0,
    });
    const { data, isLoading, error, refetch } = (0, useFetchRates_1.useFetchRates)({
        fromCurrencyAccount: fromState.account,
        toCurrency: toState.currency,
        fromCurrencyAmount: fromState.amount ?? (0, bignumber_js_1.default)(0),
        onSuccess(data) {
            resetCountdown();
            if (data.length === 0) {
                stopCountdown();
                onNoRates?.({ fromState, toState });
            }
            else {
                startCountdown();
                setExchangeRate?.(data[0]);
            }
        },
        isEnabled,
    });
    (0, react_1.useEffect)(() => {
        if (countdown <= 0 && allowRefresh) {
            refetch();
        }
    }, [countdown, refetch, allowRefresh]);
    if (!fromState.amount || fromState.amount.lte(0)) {
        setExchangeRate?.(undefined);
        return {
            rates: {
                status: "idle",
                value: [],
                error: undefined,
            },
            refetchRates: () => undefined,
            updateSelectedRate: () => undefined,
            countdown: undefined,
        };
    }
    if (isLoading) {
        setExchangeRate?.(undefined);
        return {
            rates: {
                status: "loading",
                value: [],
                error: undefined,
            },
            refetchRates: () => undefined,
            updateSelectedRate: () => undefined,
            countdown: undefined,
        };
    }
    if (error) {
        setExchangeRate?.(undefined);
        return {
            rates: {
                status: "error",
                value: [],
                error: error,
            },
            refetchRates: () => undefined,
            updateSelectedRate: () => undefined,
            countdown: undefined,
        };
    }
    if (data) {
        return {
            rates: {
                status: "success",
                value: data,
                error: undefined,
            },
            refetchRates: refetch,
            updateSelectedRate: () => undefined,
            countdown,
        };
    }
    return {
        rates: {
            status: "idle",
            value: [],
            error: undefined,
        },
        refetchRates: () => undefined,
        updateSelectedRate: () => undefined,
        countdown: undefined,
    };
}
exports.useProviderRates = useProviderRates;
//# sourceMappingURL=useProviderRates.js.map