"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapStakingPositions = exports.useLedgerFirstShuffledValidatorsSui = exports.useGetExtraDetails = exports.useSuiMappedStakingPositions = void 0;
const react_1 = require("react");
const constants_1 = require("@ledgerhq/coin-sui/constants");
const bignumber_js_1 = require("bignumber.js");
const account_1 = require("../../account");
const currencies_1 = require("@ledgerhq/coin-framework/currencies");
const preload_1 = require("@ledgerhq/coin-sui/preload");
const getOperationExtra_1 = require("@ledgerhq/coin-sui/getOperationExtra");
function useSuiMappedStakingPositions(account) {
    const { validators } = (0, preload_1.getCurrentSuiPreloadData)();
    const stakingPositions = account.suiResources?.stakes;
    const unit = (0, account_1.getAccountCurrency)(account).units[0];
    return (0, react_1.useMemo)(() => (0, exports.mapStakingPositions)(stakingPositions ?? [], validators, unit), [stakingPositions, validators, unit]);
}
exports.useSuiMappedStakingPositions = useSuiMappedStakingPositions;
function useGetExtraDetails(account, type, digest) {
    if (!account.suiResources) {
        account.suiResources = {};
    }
    if (!account.suiResources.cachedOps) {
        account.suiResources.cachedOps = {};
    }
    const cache = account.suiResources.cachedOps;
    const [data, setData] = (0, react_1.useState)(cache[digest]);
    (0, react_1.useEffect)(() => {
        if (type !== "DELEGATE" && type !== "UNDELEGATE")
            return;
        if (data)
            return;
        (0, getOperationExtra_1.getOperationExtra)(digest).then(result => {
            setData(result);
            cache[digest] = result;
        });
    }, [data, digest, cache, type]);
    return data;
}
exports.useGetExtraDetails = useGetExtraDetails;
function useLedgerFirstShuffledValidatorsSui(search) {
    const { validators: unorderedValidators } = (0, preload_1.getCurrentSuiPreloadData)();
    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.suiAddress?.toLowerCase().includes(lowercaseSearch) ||
            validator.name.toLowerCase().includes(lowercaseSearch));
        return filtered;
    }, [validators, search]);
}
exports.useLedgerFirstShuffledValidatorsSui = useLedgerFirstShuffledValidatorsSui;
function reorderValidators(validators) {
    const sortedValidators = [...validators].sort((a, b) => new bignumber_js_1.BigNumber(b.stakingPoolSuiBalance).minus(new bignumber_js_1.BigNumber(a.stakingPoolSuiBalance)).toNumber());
    // move Ledger validator to the first position
    const ledgerValidator = sortedValidators.find(v => v.suiAddress === constants_1.FIGMENT_SUI_VALIDATOR_ADDRESS);
    if (ledgerValidator) {
        const sortedValidatorsLedgerFirst = sortedValidators.filter(v => v.suiAddress !== constants_1.FIGMENT_SUI_VALIDATOR_ADDRESS);
        sortedValidatorsLedgerFirst.unshift(ledgerValidator);
        return sortedValidatorsLedgerFirst;
    }
    return sortedValidators;
}
const mapStakingPositions = (stakingPositions, validators, unit) => {
    const data = stakingPositions.flatMap(sp => sp.stakes.map(s => ({ ...s, validatorAddress: sp.validatorAddress })));
    return data.map(sp => {
        const rank = validators.findIndex(v => v.suiAddress === sp.validatorAddress);
        const validator = validators[rank] ?? sp;
        const formatConfig = {
            disableRounding: false,
            alwaysShowSign: false,
            showCode: true,
        };
        return {
            ...sp,
            rank,
            validator,
            stakedSuiId: sp.stakedSuiId,
            formattedAmount: (0, currencies_1.formatCurrencyUnit)(unit, (0, bignumber_js_1.BigNumber)(sp.principal), formatConfig),
            formattedEstimatedReward: (0, currencies_1.formatCurrencyUnit)(unit, (0, bignumber_js_1.BigNumber)(sp.status === "Active" ? sp.estimatedReward : 0), formatConfig),
        };
    });
};
exports.mapStakingPositions = mapStakingPositions;
//# sourceMappingURL=react.js.map