"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSolanaStakesWithMeta = exports.useValidators = exports.useSolanaPreloadData = void 0;
const react_1 = require("react");
const preload_data_1 = require("@ledgerhq/coin-solana/preload-data");
const observable_1 = require("../../observable");
function useSolanaPreloadData(currency) {
    return (0, observable_1.useObservable)((0, preload_data_1.getSolanaPreloadData)(currency), (0, preload_data_1.getCurrentSolanaPreloadData)(currency));
}
exports.useSolanaPreloadData = useSolanaPreloadData;
function useValidators(currency, search) {
    const data = useSolanaPreloadData(currency);
    return (0, react_1.useMemo)(() => {
        const validators = data?.validators ?? [];
        if (validators.length === 0 || !search || search === "") {
            return validators;
        }
        const lowercaseSearch = search.toLowerCase();
        const filtered = validators.filter(validator => validator.name?.toLowerCase().includes(lowercaseSearch) ||
            validator.voteAccount.toLowerCase().includes(lowercaseSearch));
        const flags = [];
        const output = [];
        for (let i = 0; i < filtered.length; i++) {
            if (flags[filtered[i].voteAccount])
                continue;
            flags[filtered[i].voteAccount] = true;
            output.push(filtered[i]);
        }
        return output;
    }, [data, search]);
}
exports.useValidators = useValidators;
function useSolanaStakesWithMeta(currency, stakes) {
    const data = useSolanaPreloadData(currency);
    if (data === null || data === undefined) {
        return [];
    }
    const { validators } = data;
    const validatorByVoteAccAddr = new Map(validators.map(v => [v.voteAccount, v]));
    return stakes.map(stake => {
        const voteAccAddr = stake.delegation?.voteAccAddr;
        const validator = voteAccAddr === undefined ? undefined : validatorByVoteAccAddr.get(voteAccAddr);
        return {
            stake,
            meta: {
                validator: {
                    img: validator?.avatarUrl,
                    name: validator?.name,
                    url: validator?.wwwUrl,
                },
            },
        };
    });
}
exports.useSolanaStakesWithMeta = useSolanaStakesWithMeta;
//# sourceMappingURL=react.js.map