"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGroupedCurrenciesByProvider = void 0;
const type_1 = require("./type");
const react_1 = require("react");
const currencies_1 = require("../currencies");
const helper_1 = require("./helper");
// FIXME(LIVE-10505): bad performane & shared utility to move to coin-framework
const listSupportedTokens = () => (0, currencies_1.listTokens)().filter(t => (0, currencies_1.isCurrencySupported)(t.parentCurrency));
const initialResult = {
    sortedCryptoCurrencies: [],
    currenciesByProvider: [],
};
const useGroupedCurrenciesByProvider = (withLoading) => {
    const [result, setResult] = (0, react_1.useState)(initialResult);
    const [loadingStatus, setLoadingStatus] = (0, react_1.useState)(type_1.LoadingStatus.Idle);
    const coinsAndTokensSupported = (0, react_1.useMemo)(() => (0, currencies_1.listSupportedCurrencies)().concat(listSupportedTokens()), []);
    // Get mapped assets filtered by supported & sorted currencies, grouped by provider id
    (0, react_1.useEffect)(() => {
        if (withLoading) {
            setLoadingStatus(type_1.LoadingStatus.Idle);
            (0, helper_1.loadCurrenciesByProvider)(coinsAndTokensSupported)
                .then(data => {
                setResult(data);
                setLoadingStatus(type_1.LoadingStatus.Success);
            })
                .catch(() => setLoadingStatus(type_1.LoadingStatus.Error));
        }
        else {
            (0, helper_1.loadCurrenciesByProvider)(coinsAndTokensSupported).then(setResult);
        }
    }, [coinsAndTokensSupported, withLoading]);
    return withLoading ? { result, loadingStatus } : result;
};
exports.useGroupedCurrenciesByProvider = useGroupedCurrenciesByProvider;
//# sourceMappingURL=useGroupedCurrenciesByProvider.hook.js.map