"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.currenciesByMarketcap = exports.fetchMarketcapIds = exports.sortCurrenciesByIds = void 0;
const cache_1 = require("@ledgerhq/live-network/cache");
const index_1 = __importDefault(require("@ledgerhq/live-countervalues/api/index"));
// sort currencies by ids provided
const sortCurrenciesByIds = (currencies, ids) => {
    const currenciesById = new Map();
    for (const c of currencies) {
        if (c.type !== "FiatCurrency") {
            currenciesById.set(c.id, c);
        }
    }
    const all = new Set();
    for (const id of ids) {
        const currency = currenciesById.get(id);
        if (currency) {
            all.add(currency);
        }
    }
    for (const cur of currencies) {
        all.add(cur);
    }
    return [...all];
};
exports.sortCurrenciesByIds = sortCurrenciesByIds;
/**
 * @deprecated live-countervalues-react context unify a single fetch of this API data, so you may want to just use `useCurrenciesByMarketcap` instead OR get the marketcapIds from that context and directly use sortByCurrenciesById function
 */
exports.fetchMarketcapIds = (0, cache_1.makeLRUCache)(() => index_1.default.fetchIdsSortedByMarketcap());
/**
 * @deprecated live-countervalues-react context unify a single fetch of this API data, so you may want to just use `useCurrenciesByMarketcap` instead OR get the marketcapIds from that context and directly use sortByCurrenciesById function
 */
const currenciesByMarketcap = (currencies) => (0, exports.fetchMarketcapIds)().then(ids => (0, exports.sortCurrenciesByIds)(currencies, ids), () => currencies);
exports.currenciesByMarketcap = currenciesByMarketcap;
//# sourceMappingURL=sortByMarketcap.js.map