"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTokensWithFunds = exports.getTokensWithFundsMap = void 0;
const helpers_1 = require("@ledgerhq/coin-framework/account/helpers");
const getTokensWithFundsMap = (accounts) => {
    const tokensMap = new Map();
    for (const account of accounts) {
        const { balance, currency } = account || {};
        if (!balance?.gt(0) || !currency)
            continue;
        const parentAccount = (0, helpers_1.getParentAccount)(account, accounts);
        const networkName = parentAccount?.currency?.name;
        if (!networkName)
            continue;
        const mainKey = `${currency.ticker}:${networkName}`;
        tokensMap.set(mainKey, {
            ticker: currency.ticker,
            networkName,
            id: currency.id,
        });
        account.subAccounts?.forEach(subAccount => {
            const { balance, token } = subAccount || {};
            if (!balance?.gt(0) || !token)
                return;
            const subKey = `${token.ticker}:${networkName}`;
            tokensMap.set(subKey, {
                ticker: token.ticker,
                networkName,
                id: token.id,
            });
        });
    }
    return tokensMap;
};
exports.getTokensWithFundsMap = getTokensWithFundsMap;
/** Format defaults to "USDT on Ethereum". format: "currencyId" returns the id of each token, e.g. "ethereum/erc20/usde". */
const getTokensWithFunds = (accounts, customFormat = "default") => {
    if (!accounts?.length)
        return [];
    const tokensMap = (0, exports.getTokensWithFundsMap)(accounts);
    if (customFormat === "currencyId") {
        return Array.from(tokensMap.values(), token => token.id);
    }
    return Array.from(tokensMap.values(), ({ ticker, networkName }) => `${ticker} on ${networkName}`);
};
exports.getTokensWithFunds = getTokensWithFunds;
//# sourceMappingURL=getTokensWithFunds.js.map