"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useHederaEnrichedDelegation = exports.useHederaValidators = exports.useHederaPreloadData = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const react_1 = require("react");
const preload_data_1 = require("@ledgerhq/coin-hedera/preload-data");
const utils_1 = require("./utils");
const observable_1 = require("../../observable");
function useHederaPreloadData(currency) {
    return (0, observable_1.useObservable)((0, preload_data_1.getHederaPreloadData)(currency), (0, preload_data_1.getCurrentHederaPreloadData)(currency));
}
exports.useHederaPreloadData = useHederaPreloadData;
function useHederaValidators(currency, search) {
    const data = useHederaPreloadData(currency);
    return (0, react_1.useMemo)(() => {
        const validators = data?.validators ?? [];
        if (validators.length === 0 || !search || search === "") {
            return validators;
        }
        return validators.filter(validator => {
            return (0, utils_1.filterValidatorBySearchTerm)(validator, search);
        });
    }, [data, search]);
}
exports.useHederaValidators = useHederaValidators;
function useHederaEnrichedDelegation(account, delegation) {
    const validators = useHederaValidators(account.currency);
    const validatorByNodeId = new Map(validators.map(v => [v.nodeId, v]));
    const validator = validatorByNodeId.get(delegation.nodeId) ?? null;
    return {
        ...delegation,
        status: (0, utils_1.getDelegationStatus)(validator),
        validator: {
            name: validator?.name ?? "",
            address: validator?.address ?? "",
            addressChecksum: validator?.addressChecksum ?? null,
            nodeId: delegation.nodeId,
            minStake: validator?.minStake ?? new bignumber_js_1.default(0),
            maxStake: validator?.maxStake ?? new bignumber_js_1.default(0),
            activeStake: validator?.activeStake ?? new bignumber_js_1.default(0),
            activeStakePercentage: validator?.activeStakePercentage ?? new bignumber_js_1.default(0),
            overstaked: validator?.overstaked ?? false,
        },
    };
}
exports.useHederaEnrichedDelegation = useHederaEnrichedDelegation;
//# sourceMappingURL=react.js.map