"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useScrollManagement = void 0;
const react_1 = require("react");
const PageLayoutContainer_1 = require("../PageLayoutContainer");
const useViewportType_1 = require("../useViewportType");
/**
 * Отслеживание скролла для динамического отображения футера
 */
const useScrollManagement = ({ scrollRef, isEndReached, onLoadMore, virtuosoId, }) => {
    const [scrollParent, setScrollParent] = (0, react_1.useState)(null);
    const [hasVerticalScroll, setHasVerticalScroll] = (0, react_1.useState)(false);
    const { isMobile } = (0, useViewportType_1.useViewportType)();
    (0, react_1.useEffect)(() => {
        if (isMobile) {
            // На мобильных устройствах scroll находится на контейнере pageLayout
            const rootScrollElement = document.querySelector(`.${PageLayoutContainer_1.pageLayoutClassnames.root}`);
            if (rootScrollElement instanceof HTMLElement) {
                setScrollParent(rootScrollElement);
                return;
            }
        }
        if (scrollRef?.current) {
            setScrollParent(scrollRef.current);
        }
        if (scrollRef?.current?.parentElement) {
            scrollRef.current.parentElement.style.position = 'relative';
        }
    }, [scrollRef]);
    const recalculateVerticalScroll = () => {
        const list = document.getElementById(virtuosoId);
        if (scrollRef?.current && list) {
            setHasVerticalScroll(list?.clientHeight > scrollRef.current?.clientHeight);
        }
    };
    const handleEndReach = () => {
        if (!isEndReached && onLoadMore) {
            onLoadMore();
        }
    };
    return {
        scrollParent,
        hasVerticalScroll,
        recalculateVerticalScroll,
        handleEndReach,
        isEndReachedWithScroll: isEndReached && hasVerticalScroll,
    };
};
exports.useScrollManagement = useScrollManagement;
