"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFetchRates = void 0;
const helpers_1 = require("@ledgerhq/coin-framework/account/helpers");
const fetchRates_1 = require("../../api/v5/fetchRates");
const useAPI_1 = require("../../../../hooks/useAPI");
const useFilteredProviders_1 = require("./useFilteredProviders");
function useFetchRates({ fromCurrencyAccount, toCurrency, fromCurrencyAmount, onSuccess, isEnabled = true, }) {
    const currencyFrom = fromCurrencyAccount ? (0, helpers_1.getAccountCurrency)(fromCurrencyAccount).id : undefined;
    const unitFrom = fromCurrencyAccount
        ? (0, helpers_1.getAccountCurrency)(fromCurrencyAccount).units[0]
        : undefined;
    const unitTo = toCurrency?.units[0];
    const formattedCurrencyAmount = (unitFrom && `${fromCurrencyAmount.shiftedBy(-unitFrom.magnitude)}`) ?? "0";
    const { providers, loading, error } = (0, useFilteredProviders_1.useFilteredProviders)();
    const toCurrencyId = toCurrency?.id;
    return (0, useAPI_1.useAPI)({
        queryFn: fetchRates_1.fetchRates,
        queryProps: {
            providers,
            unitTo: unitTo,
            unitFrom: unitFrom,
            currencyFrom,
            toCurrencyId,
            fromCurrencyAmount: formattedCurrencyAmount,
        },
        staleTimeout: 20000,
        enabled: !!toCurrencyId &&
            !!currencyFrom &&
            fromCurrencyAmount.gt(0) &&
            !!unitFrom &&
            !!unitTo &&
            !loading &&
            isEnabled &&
            !error,
        onSuccess,
    });
}
exports.useFetchRates = useFetchRates;
//# sourceMappingURL=useFetchRates.js.map