"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const constants_1 = require("../../constants");
const useLogic = ({ isLoading, setContainerWidth, isViewerLite, }) => {
    const documentRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(() => {
        if (isViewerLite) {
            return;
        }
        const textLayerLink = document.createElement('link');
        textLayerLink.rel = 'stylesheet';
        textLayerLink.href = constants_1.TEXT_LAYER_CSS_URL;
        document.head.appendChild(textLayerLink);
        const annotationLayerLink = document.createElement('link');
        annotationLayerLink.rel = 'stylesheet';
        annotationLayerLink.href = constants_1.ANNOTATION_LAYER_CSS_URL;
        document.head.appendChild(annotationLayerLink);
        return () => {
            document.head.removeChild(textLayerLink);
            document.head.removeChild(annotationLayerLink);
        };
    }, []);
    /**
     * Следит за изменением широты pageContent и при ее изменении - меняет ширину страниц
     */
    (0, react_1.useEffect)(() => {
        if (!documentRef.current) {
            const container = document.querySelector(`.${constants_1.pdfViewerClassnames.document}`);
            if (!container) {
                return;
            }
            documentRef.current = container;
        }
        const resizeObserver = new ResizeObserver(() => {
            documentRef?.current?.classList.add(constants_1.pdfViewerClassnames.animation);
            setContainerWidth(documentRef?.current?.clientWidth || 0);
        });
        resizeObserver.observe(documentRef?.current);
        return () => {
            resizeObserver.disconnect();
        };
    }, []);
    const onAnimationEnd = (event) => {
        if (event.animationName === constants_1.PDF_VIEWER_ANIMATION) {
            documentRef?.current?.classList.remove(constants_1.pdfViewerClassnames.animation);
        }
    };
    return {
        documentRef,
        onAnimationEnd,
        isLoading,
    };
};
exports.useLogic = useLogic;
