"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePolkadotBondLoading = exports.useSortedValidators = exports.searchFilter = exports.usePolkadotPreloadData = void 0;
const react_1 = require("react");
const state_1 = require("@ledgerhq/coin-polkadot/bridge/state");
const useMemoOnce_1 = __importDefault(require("../../hooks/useMemoOnce"));
const react_2 = require("../../bridge/react");
const SYNC_REFRESH_RATE = 6000; // 6s - block time
function usePolkadotPreloadData() {
    const [state, setState] = (0, react_1.useState)(state_1.getCurrentPolkadotPreloadData);
    (0, react_1.useEffect)(() => {
        const sub = (0, state_1.getPolkadotPreloadDataUpdates)().subscribe(data => {
            setState(data);
        });
        return () => sub.unsubscribe();
    }, []);
    return state;
}
exports.usePolkadotPreloadData = usePolkadotPreloadData;
const searchFilter = query => validator => {
    const terms = `${validator?.identity ?? ""} ${validator?.address ?? ""}`;
    return terms.toLowerCase().includes(query.toLowerCase().trim());
};
exports.searchFilter = searchFilter;
/** Hook to search and sort SR list according to initial votes and query */
function useSortedValidators(search, validators, nominations, validatorSearchFilter = exports.searchFilter) {
    const initialVotes = (0, useMemoOnce_1.default)(() => nominations.map(({ address }) => address));
    const sortedVotes = (0, react_1.useMemo)(() => validators
        .filter(validator => initialVotes.includes(validator.address))
        .concat(validators.filter(validator => !initialVotes.includes(validator.address))), [validators, initialVotes]);
    const sr = (0, react_1.useMemo)(() => (search ? validators.filter(validatorSearchFilter(search)) : sortedVotes), [search, validators, sortedVotes, validatorSearchFilter]);
    return sr;
}
exports.useSortedValidators = useSortedValidators;
/**
 * Sync account until "controller" is set - following a first bond.
 *
 * @param {*} account
 */
function usePolkadotBondLoading(account) {
    const controller = account.polkadotResources?.controller || null;
    const initialAccount = (0, react_1.useRef)(account);
    const [isLoading, setLoading] = (0, react_1.useState)(!controller);
    (0, react_1.useEffect)(() => {
        if (controller) {
            setLoading(false);
        }
    }, [controller]);
    const sync = (0, react_2.useBridgeSync)();
    (0, react_1.useEffect)(() => {
        if (!isLoading)
            return;
        const interval = setInterval(() => {
            sync({
                type: "SYNC_ONE_ACCOUNT",
                priority: 10,
                accountId: initialAccount.current.id,
                reason: "polkadot-bond-loading",
            });
        }, SYNC_REFRESH_RATE);
        return () => clearInterval(interval);
    }, [initialAccount, sync, isLoading]);
    return isLoading;
}
exports.usePolkadotBondLoading = usePolkadotBondLoading;
//# sourceMappingURL=react.js.map