"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLedgerFirstShuffledValidatorsNear = exports.useNearStakingPositionsQuerySelector = exports.useNearMappedStakingPositions = void 0;
const invariant_1 = __importDefault(require("invariant"));
const react_1 = require("react");
const bignumber_js_1 = require("bignumber.js");
const constants_1 = require("@ledgerhq/coin-near/constants");
const logic_1 = require("@ledgerhq/coin-near/logic");
const preload_1 = require("@ledgerhq/coin-near/preload");
const account_1 = require("../../account");
function useNearMappedStakingPositions(account) {
    const { validators } = (0, preload_1.getCurrentNearPreloadData)();
    const stakingPositions = account.nearResources?.stakingPositions;
    (0, invariant_1.default)(stakingPositions, "near: stakingPositions is required");
    const unit = (0, account_1.getAccountCurrency)(account).units[0];
    return (0, react_1.useMemo)(() => {
        const mappedStakingPositions = (0, logic_1.mapStakingPositions)(stakingPositions || [], validators, unit);
        return mappedStakingPositions;
    }, [stakingPositions, validators, unit]);
}
exports.useNearMappedStakingPositions = useNearMappedStakingPositions;
function useNearStakingPositionsQuerySelector(account, transaction) {
    const stakingPositions = useNearMappedStakingPositions(account);
    const options = (0, react_1.useMemo)(() => stakingPositions.filter(sp => transaction.mode === "unstake" ? sp.staked.gt(0) : sp.available.gt(0)), [stakingPositions, transaction]);
    const selectedValidatorAddress = transaction.recipient;
    const value = (0, react_1.useMemo)(() => stakingPositions.find(({ validatorId }) => validatorId === selectedValidatorAddress), [stakingPositions, selectedValidatorAddress]);
    return {
        options,
        value,
    };
}
exports.useNearStakingPositionsQuerySelector = useNearStakingPositionsQuerySelector;
function useLedgerFirstShuffledValidatorsNear(search) {
    const { validators: unorderedValidators } = (0, preload_1.getCurrentNearPreloadData)();
    const validators = reorderValidators(unorderedValidators);
    return (0, react_1.useMemo)(() => {
        if (validators.length === 0 || !search || search === "") {
            return validators;
        }
        const lowercaseSearch = search.toLowerCase();
        const filtered = validators.filter(validator => validator.validatorAddress?.toLowerCase().includes(lowercaseSearch));
        return filtered;
    }, [validators, search]);
}
exports.useLedgerFirstShuffledValidatorsNear = useLedgerFirstShuffledValidatorsNear;
function reorderValidators(validators) {
    const sortedValidators = validators.sort((a, b) => new bignumber_js_1.BigNumber(b.tokens).minus(new bignumber_js_1.BigNumber(a.tokens)).toNumber());
    // move Ledger validator to the first position
    const ledgerValidator = sortedValidators.find(v => v.validatorAddress === constants_1.FIGMENT_NEAR_VALIDATOR_ADDRESS);
    if (ledgerValidator) {
        const sortedValidatorsLedgerFirst = sortedValidators.filter(v => v.validatorAddress !== constants_1.FIGMENT_NEAR_VALIDATOR_ADDRESS);
        sortedValidatorsLedgerFirst.unshift(ledgerValidator);
        return sortedValidatorsLedgerFirst;
    }
    return sortedValidators;
}
//# sourceMappingURL=react.js.map