"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const useLogic = ({ pageNumber, scale, onChangePage, pageCount, scrollRef, isHidePersonalData, currentPage, isViewerLite, enabledZoom, }) => {
    const ref = (0, react_1.useRef)(null);
    const [isVisible, setIsVisible] = (0, react_1.useState)(false);
    (0, react_1.useEffect)(() => {
        const threshold = 0.1 / scale;
        const observer = new IntersectionObserver(([entry]) => {
            if (entry.isIntersecting && pageNumber) {
                onChangePage?.(pageNumber);
                setIsVisible(true);
            }
        }, {
            root: scrollRef.current,
            threshold: Math.max(0.01, threshold),
        });
        if (ref.current) {
            observer.observe(ref.current);
        }
        return () => {
            observer.disconnect();
        };
    }, [scale]);
    const label = `Стр. ${pageNumber}`;
    const isShowTag = isViewerLite || enabledZoom ? false : pageCount > 1;
    // Флаг, находится ли страница по соседству с текущей
    const isNearTheCurrentPage = pageNumber === currentPage + 1 || pageNumber === currentPage - 1;
    return {
        ref,
        isHidePersonalData,
        isShowPage: isVisible || isNearTheCurrentPage,
        isViewerLite,
        label,
        isShowTag,
    };
};
exports.useLogic = useLogic;
