import { useEffect, useRef, useState } from "react";
import { fetchPoolList } from "@ledgerhq/coin-cardano/api/getPools";
export function useCardanoFamilyPools(currency) {
    const [pools, setPools] = useState([]);
    const [searchQuery, setSearchQuery] = useState("");
    const [isSearching, setIsSearching] = useState(false);
    const [isPaginating, setIsPaginating] = useState(false);
    const limit = 100;
    const pageNo = useRef(1);
    const isPaginationDisabled = useRef(false);
    useEffect(() => {
        isPaginationDisabled.current = false;
        setPools([]);
        setIsSearching(true);
        pageNo.current = 1;
        const delayDebounceFn = setTimeout(() => {
            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++;
        fetchPoolList(currency, searchQuery, pageNo.current, limit)
            .then((apiRes) => {
            setPools(currentPools => {
                return [...currentPools, ...apiRes.pools];
            });
        })
            .finally(() => {
            setIsPaginating(false);
        });
    };
    return {
        pools,
        searchQuery,
        setSearchQuery,
        onScrollEndReached,
        isSearching,
        isPaginating,
    };
}
//# sourceMappingURL=react.js.map