"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.drillBalance = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const lodash_1 = require("lodash");
const contract_interface_1 = require("../../position/contract.interface");
const display_item_present_1 = require("./presentation/display-item.present");
const drillBalance = (token, balanceRaw, options = {}) => {
    var _a;
    const balance = Number(balanceRaw) / Math.pow(10, token.decimals);
    const balanceUSD = balance * token.price * (options.isDebt ? -1 : 1);
    if (token.type === contract_interface_1.ContractType.BASE_TOKEN || token.type === contract_interface_1.ContractType.NON_FUNGIBLE_TOKEN) {
        const tokenWithBalance = Object.assign(Object.assign({}, token), { balance, balanceRaw, balanceUSD });
        return tokenWithBalance;
    }
    const userStatsItems = [];
    if (token.supply > 0) {
        const share = (balance / token.supply) * 100;
        userStatsItems.push({ label: 'Share', value: (0, display_item_present_1.buildPercentageDisplayItem)(share) });
    }
    const tokenStatsItems = ((_a = token.displayProps) === null || _a === void 0 ? void 0 : _a.statsItems) || [];
    const tokens = token.tokens || [];
    const tokenWithBalance = Object.assign(Object.assign({}, token), { balance,
        balanceRaw,
        balanceUSD, tokens: tokens.map((underlyingToken, i) => {
            const pricePerShare = (0, lodash_1.isArray)(token.pricePerShare) ? token.pricePerShare[i] : token.pricePerShare;
            const underlyingBalanceRawBN = new bignumber_js_1.default(balanceRaw)
                .div(Math.pow(10, token.decimals))
                .times(Math.pow(10, underlyingToken.decimals))
                .times(pricePerShare);
            const underlyingBalanceRaw = underlyingToken.decimals === 0 ? underlyingBalanceRawBN.toFixed(18) : underlyingBalanceRawBN.toFixed(0);
            return (0, exports.drillBalance)(underlyingToken, underlyingBalanceRaw);
        }), dataProps: token.dataProps, displayProps: Object.assign(Object.assign({}, token.displayProps), { statsItems: [...tokenStatsItems, ...userStatsItems] }) });
    return tokenWithBalance;
};
exports.drillBalance = drillBalance;
//# sourceMappingURL=drill-balance.helper.js.map