"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAccountBannerState = void 0;
const preload_data_1 = require("@ledgerhq/coin-solana/preload-data");
const logic_1 = require("@ledgerhq/coin-solana/logic");
const utils_1 = require("@ledgerhq/coin-solana/utils");
const account_1 = require("../../account");
function getAccountBannerState(account) {
    // Group current validator
    const solanaResources = account.solanaResources ? account.solanaResources : { stakes: [] };
    const delegations = solanaResources?.stakes.map(delegation => {
        return delegation;
    });
    // Get ledger validator data
    const { validators } = (0, preload_data_1.getCurrentSolanaPreloadData)(account.currency) ?? {
        validators: [],
    };
    const ledgerValidator = validators.find(validator => utils_1.LEDGER_VALIDATORS_VOTE_ACCOUNTS.includes(validator.voteAccount));
    // If Ledger doesn't provide validator, we don't display banner
    if (!ledgerValidator) {
        return {
            display: false,
            redelegate: false,
            stakeAccAddr: "",
            ledgerValidator,
        };
    }
    let redelegate = false;
    let stakeAccAddr = "";
    let display = false;
    // Find user current worst validator (default validator is ledger)
    let worstValidator = ledgerValidator;
    for (const delegation of delegations) {
        const validatorAdress = delegation.delegation?.voteAccAddr;
        const validator = validators.find(validator => validator.voteAccount === validatorAdress);
        const actions = (0, logic_1.stakeActions)(delegation);
        const isValidRedelegation = validator &&
            !utils_1.LEDGER_VALIDATORS_VOTE_ACCOUNTS.includes(validatorAdress) &&
            worstValidator.commission <= validator.commission &&
            actions.includes("deactivate");
        if (isValidRedelegation) {
            stakeAccAddr = delegation.stakeAccAddr;
            worstValidator = validator;
        }
    }
    if (worstValidator) {
        if (utils_1.LEDGER_VALIDATORS_VOTE_ACCOUNTS.includes(worstValidator?.voteAccount)) {
            if (!(0, account_1.isAccountEmpty)(account)) {
                display = true;
            }
        }
        else {
            redelegate = true;
            display = true;
        }
    }
    return {
        display,
        redelegate,
        stakeAccAddr,
        ledgerValidator,
    };
}
exports.getAccountBannerState = getAccountBannerState;
//# sourceMappingURL=banner.js.map