"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountBannerState = void 0;
const preload_1 = require("@ledgerhq/coin-near/preload");
const constants_1 = require("@ledgerhq/coin-near/constants");
const logic_1 = require("@ledgerhq/coin-near/logic");
function getAccountBannerState(account) {
    // Group current validator
    const nearResources = account.nearResources ? account.nearResources : { stakingPositions: [] };
    const delegations = nearResources?.stakingPositions.map(delegation => {
        return delegation;
    });
    // Get ledger validator data
    const { validators } = (0, preload_1.getCurrentNearPreloadData)() ?? {
        validators: [],
    };
    const ledgerValidator = validators.find(validator => validator.validatorAddress === constants_1.FIGMENT_NEAR_VALIDATOR_ADDRESS);
    // If Ledger doesn't provide validator, we don't display banner
    if (!ledgerValidator) {
        return {
            display: false,
            redelegate: false,
            validatorId: "",
            ledgerValidator: undefined,
        };
    }
    let redelegate = false;
    let validatorId = "";
    let display = false;
    // Find user current worst validator (default validator is ledger)
    let worstValidator = ledgerValidator;
    for (const delegation of delegations) {
        const validatorAdress = delegation?.validatorId;
        const validator = validators.find(validator => validator.validatorAddress === validatorAdress);
        const isValidRedelegation = validator &&
            validatorAdress !== ledgerValidator.validatorAddress &&
            validator.commission &&
            worstValidator.commission &&
            worstValidator?.commission <= validator?.commission &&
            (0, logic_1.canUnstake)(delegation);
        if (isValidRedelegation) {
            validatorId = delegation.validatorId;
            worstValidator = validator;
        }
    }
    if (worstValidator) {
        if (worstValidator?.validatorAddress === ledgerValidator?.validatorAddress) {
            if ((0, logic_1.canStake)(account)) {
                display = true;
            }
        }
        else {
            redelegate = true;
            display = true;
        }
    }
    return {
        display,
        redelegate,
        validatorId,
        ledgerValidator,
    };
}
exports.getAccountBannerState = getAccountBannerState;
//# sourceMappingURL=banner.js.map