import { currenciesByMarketcap, getCryptoCurrencyById, getTokenById, hasCryptoCurrencyId, } from "../currencies";
import { getMappedAssets } from "./api";
export const loadCurrenciesByProvider = async (coinsAndTokensSupported) => {
    const [sortedCurrenciesSupported, assets] = await Promise.all([
        currenciesByMarketcap(coinsAndTokensSupported),
        getMappedAssets(),
    ]);
    return groupCurrenciesByProvider(assets, sortedCurrenciesSupported);
};
export const groupCurrenciesByProvider = (assets, sortedCurrencies) => {
    const assetsByLedgerId = new Map();
    for (const asset of assets) {
        /// FIXME(LIVE-10508) drop usage of toLowerCase
        assetsByLedgerId.set(asset.ledgerId.toLowerCase(), asset);
    }
    const assetsByProviderId = new Map();
    const sortedCryptoCurrencies = [];
    // iterate over currencies by preserving their order
    for (const ledgerCurrency of sortedCurrencies) {
        /// FIXME(LIVE-10508) drop usage of toLowerCase
        const asset = assetsByLedgerId.get(ledgerCurrency.id.toLowerCase());
        if (asset) {
            // we only yield the intersection of currencies and mapped assets
            const existingEntry = assetsByProviderId.get(asset.providerId);
            if (!existingEntry) {
                assetsByProviderId.set(asset.providerId, {
                    providerId: asset.providerId,
                    currenciesByNetwork: [ledgerCurrency],
                });
            }
            else {
                existingEntry.currenciesByNetwork.push(ledgerCurrency);
            }
        }
    }
    // in this case, the first currency of the provider is the one we want to display (Wasn't true)
    // So we need to take the first crypto or token currency of each provider to fix that
    for (const [, { currenciesByNetwork }] of assetsByProviderId.entries()) {
        const firstCrypto = currenciesByNetwork.find(c => c.type === "CryptoCurrency");
        const elem = firstCrypto ?? currenciesByNetwork.find(c => c.type === "TokenCurrency");
        if (elem) {
            sortedCryptoCurrencies.push(elem);
        }
    }
    return {
        currenciesByProvider: Array.from(assetsByProviderId.values()),
        sortedCryptoCurrencies,
    };
};
export const searchByProviderId = (list, providerId) => list.filter(elem => elem.providerId.toLowerCase() === providerId.toLowerCase());
export const searchByNameOrTicker = (list, nameOrTicker) => list.filter(elem => elem.name.toLowerCase().includes(nameOrTicker.toLowerCase()) ||
    elem.ticker.toLowerCase().includes(nameOrTicker.toLowerCase()));
export const getTokenOrCryptoCurrencyById = (id) => {
    if (hasCryptoCurrencyId(id)) {
        return getCryptoCurrencyById(id);
    }
    return getTokenById(id);
};
//# sourceMappingURL=helper.js.map