"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.aggregateAccountStates = exports.calculateWeightedReturnOnEquity = exports.getSelectedEvmAccount = exports.getEvmAccountFromAccountGroup = exports.findEvmAccount = void 0;
const perpsConfig_1 = require("../constants/perpsConfig.cjs");
const EVM_ACCOUNT_TYPES = new Set(['eip155:eoa', 'eip155:erc4337']);
function isEvmAccountType(type) {
    return EVM_ACCOUNT_TYPES.has(type);
}
function findEvmAccount(accounts) {
    const evmAccount = accounts.find((account) => account && isEvmAccountType(account.type));
    return evmAccount ?? null;
}
exports.findEvmAccount = findEvmAccount;
function getEvmAccountFromAccountGroup(accounts) {
    const evmAccount = findEvmAccount(accounts);
    return evmAccount ? { address: evmAccount.address } : undefined;
}
exports.getEvmAccountFromAccountGroup = getEvmAccountFromAccountGroup;
function getSelectedEvmAccount(accounts) {
    return getEvmAccountFromAccountGroup(accounts);
}
exports.getSelectedEvmAccount = getSelectedEvmAccount;
function calculateWeightedReturnOnEquity(accounts) {
    if (accounts.length === 0) {
        return '0';
    }
    let totalWeightedROE = 0;
    let totalMarginUsed = 0;
    for (const account of accounts) {
        const unrealizedPnl = typeof account.unrealizedPnl === 'string'
            ? Number.parseFloat(account.unrealizedPnl)
            : account.unrealizedPnl;
        const returnOnEquity = typeof account.returnOnEquity === 'string'
            ? Number.parseFloat(account.returnOnEquity)
            : account.returnOnEquity;
        if (Number.isNaN(unrealizedPnl) || Number.isNaN(returnOnEquity)) {
            continue;
        }
        if (returnOnEquity === 0) {
            continue;
        }
        const marginUsed = (unrealizedPnl / returnOnEquity) * 100;
        if (Number.isNaN(marginUsed) || marginUsed <= 0) {
            continue;
        }
        const roeDecimal = returnOnEquity / 100;
        totalWeightedROE += roeDecimal * marginUsed;
        totalMarginUsed += marginUsed;
    }
    if (totalMarginUsed <= 0) {
        return '0';
    }
    const weightedROE = (totalWeightedROE / totalMarginUsed) * 100;
    return weightedROE.toString();
}
exports.calculateWeightedReturnOnEquity = calculateWeightedReturnOnEquity;
/**
 * Aggregate multiple per-DEX AccountState objects into one by summing numeric fields.
 * ROE is recalculated as (totalUnrealizedPnl / totalMarginUsed) * 100.
 *
 * @param states - The array of per-DEX account states to aggregate.
 * @returns The combined account state with summed balances and recalculated ROE.
 */
function aggregateAccountStates(states) {
    const fallback = {
        availableBalance: perpsConfig_1.PERPS_CONSTANTS.FallbackDataDisplay,
        totalBalance: perpsConfig_1.PERPS_CONSTANTS.FallbackDataDisplay,
        marginUsed: perpsConfig_1.PERPS_CONSTANTS.FallbackDataDisplay,
        unrealizedPnl: perpsConfig_1.PERPS_CONSTANTS.FallbackDataDisplay,
        returnOnEquity: perpsConfig_1.PERPS_CONSTANTS.FallbackDataDisplay,
    };
    if (states.length === 0) {
        return fallback;
    }
    const aggregated = states.reduce((acc, state, index) => {
        if (index === 0) {
            return { ...state };
        }
        return {
            availableBalance: (parseFloat(acc.availableBalance) + parseFloat(state.availableBalance)).toString(),
            totalBalance: (parseFloat(acc.totalBalance) + parseFloat(state.totalBalance)).toString(),
            marginUsed: (parseFloat(acc.marginUsed) + parseFloat(state.marginUsed)).toString(),
            unrealizedPnl: (parseFloat(acc.unrealizedPnl) + parseFloat(state.unrealizedPnl)).toString(),
            returnOnEquity: '0',
        };
    }, fallback);
    // Recalculate ROE across all DEXs
    const totalMarginUsed = parseFloat(aggregated.marginUsed);
    const totalUnrealizedPnl = parseFloat(aggregated.unrealizedPnl);
    if (totalMarginUsed > 0) {
        aggregated.returnOnEquity = ((totalUnrealizedPnl / totalMarginUsed) *
            100).toString();
    }
    else {
        aggregated.returnOnEquity = '0';
    }
    return aggregated;
}
exports.aggregateAccountStates = aggregateAccountStates;
//# sourceMappingURL=accountUtils.cjs.map