"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountBannerState = void 0;
const constants_1 = require("./constants");
const hasMinimumDelegableBalance_1 = require("@ledgerhq/coin-multiversx/helpers/hasMinimumDelegableBalance");
function getWorstValidator(delegations, validators, ledgerValidator) {
    return delegations.reduce((worstValidator, delegation) => {
        const validator = validators.find(validator => validator.contract === delegation.contract);
        if (validator && validator.aprValue < worstValidator.aprValue) {
            return validator;
        }
        return worstValidator;
    }, ledgerValidator);
}
function getAccountBannerState(account, multiversxPreloadData) {
    const { validators } = multiversxPreloadData;
    const multiversxResources = account.multiversxResources
        ? account.multiversxResources
        : { delegations: [] };
    const hasDelegations = multiversxResources.delegations.length > 0;
    const ledgerValidator = validators.find(validator => validator.contract === constants_1.MULTIVERSX_LEDGER_VALIDATOR_ADDRESS);
    // // if Ledger doesn't provide validator, we don't display banner
    if (!ledgerValidator) {
        return {
            bannerType: "hidden",
        };
    }
    if (hasDelegations) {
        const worstValidator = getWorstValidator(multiversxResources.delegations, validators, ledgerValidator);
        if (worstValidator.contract !== ledgerValidator.contract) {
            const worstDelegation = multiversxResources.delegations.find(delegation => delegation.contract === worstValidator.contract);
            if (!worstDelegation)
                return { bannerType: "hidden" };
            return {
                bannerType: "redelegate",
                worstDelegation,
            };
        }
    }
    if ((0, hasMinimumDelegableBalance_1.hasMinimumDelegableBalance)(account)) {
        return {
            bannerType: "delegate",
        };
    }
    return {
        bannerType: "hidden",
    };
}
exports.getAccountBannerState = getAccountBannerState;
//# sourceMappingURL=banner.js.map