import BigNumber from "bignumber.js";
import { calculate } from "@ledgerhq/live-countervalues/logic";
export const groupAccountsByAsset = (accounts, counterValuesState, targetCurrency) => {
    const initialGroupedAccounts = {};
    return accounts.reduce((groupedAccounts, account) => {
        const assetId = account.type === "Account" ? account.currency.id : account.token.id;
        let assetGroup = groupedAccounts[assetId];
        if (!assetGroup) {
            const currency = account.type === "Account" ? account.currency : account.token;
            assetGroup = groupedAccounts[assetId] = {
                totalBalance: new BigNumber(0),
                totalFiatValue: new BigNumber(0),
                accounts: [],
                referenceCurrency: currency, // First currency becomes the reference
            };
        }
        const currency = account.type === "Account" ? account.currency : account.token;
        const balance = account.balance;
        // Convert balance to reference currency magnitude before summing
        const referenceMagnitude = assetGroup.referenceCurrency.units[0].magnitude || 0;
        const currentMagnitude = currency.units[0].magnitude || 0;
        const magnitudeDiff = referenceMagnitude - currentMagnitude;
        const normalizedBalance = balance.shiftedBy(magnitudeDiff);
        const fiatValue = calculateFiatValue(account, counterValuesState, targetCurrency);
        assetGroup.totalBalance = assetGroup.totalBalance.plus(normalizedBalance);
        assetGroup.totalFiatValue = assetGroup.totalFiatValue.plus(fiatValue || 0);
        assetGroup.accounts.push(account);
        return groupedAccounts;
    }, initialGroupedAccounts);
};
function calculateFiatValue(account, state, toCurrency) {
    const currency = account.type === "Account" ? account.currency : account.token;
    const balanceNumber = account.balance.toNumber();
    const fiatValue = calculate(state, {
        from: currency,
        to: toCurrency,
        value: balanceNumber,
    });
    return fiatValue || 0;
}
//# sourceMappingURL=groupAccountsByAsset.js.map