"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useCardanoFamilyPools = void 0;
const react_1 = require("react");
const getPools_1 = require("@ledgerhq/coin-cardano/api/getPools");
function useCardanoFamilyPools(currency) {
    const [pools, setPools] = (0, react_1.useState)([]);
    const [searchQuery, setSearchQuery] = (0, react_1.useState)("");
    const [isSearching, setIsSearching] = (0, react_1.useState)(false);
    const [isPaginating, setIsPaginating] = (0, react_1.useState)(false);
    const limit = 100;
    const pageNo = (0, react_1.useRef)(1);
    const isPaginationDisabled = (0, react_1.useRef)(false);
    (0, react_1.useEffect)(() => {
        isPaginationDisabled.current = false;
        setPools([]);
        setIsSearching(true);
        pageNo.current = 1;
        const delayDebounceFn = setTimeout(() => {
            (0, getPools_1.fetchPoolList)(currency, searchQuery, pageNo.current, limit)
                .then((apiRes) => {
                setPools([...apiRes.pools]);
                if (searchQuery && apiRes.pools.length < limit) {
                    isPaginationDisabled.current = true;
                }
            })
                .finally(() => {
                setIsSearching(false);
            });
        }, searchQuery ? 500 : 0);
        return () => clearInterval(delayDebounceFn);
    }, [searchQuery]); // eslint-disable-line react-hooks/exhaustive-deps
    const onScrollEndReached = () => {
        if (isPaginationDisabled.current)
            return;
        setIsPaginating(true);
        pageNo.current++;
        (0, getPools_1.fetchPoolList)(currency, searchQuery, pageNo.current, limit)
            .then((apiRes) => {
            setPools(currentPools => {
                return [...currentPools, ...apiRes.pools];
            });
        })
            .finally(() => {
            setIsPaginating(false);
        });
    };
    return {
        pools,
        searchQuery,
        setSearchQuery,
        onScrollEndReached,
        isSearching,
        isPaginating,
    };
}
exports.useCardanoFamilyPools = useCardanoFamilyPools;
//# sourceMappingURL=react.js.map