import { useCallback, useRef } from 'react';
import { useToggle } from '../useToggle';
/**
 * Управление кнопкой "наверх"
 */
export const useStickyButton = (scrollRef) => {
    const virtuoso = useRef(null);
    const [isStickyButtonActive, showStickyButton, hideStickyButton] = useToggle();
    const handleRangeChanged = useCallback((range) => {
        const shouldShow = range.startIndex > 2;
        if (isStickyButtonActive !== shouldShow) {
            shouldShow ? showStickyButton() : hideStickyButton();
        }
    }, [showStickyButton, hideStickyButton]);
    const handleScrollToTop = () => {
        scrollRef.current.style.scrollBehavior = 'auto';
        virtuoso.current?.scrollToIndex(0);
    };
    return {
        virtuoso,
        isStickyButtonActive,
        handleRangeChanged,
        handleScrollToTop,
    };
};
