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