"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const ConfigProvider_1 = require("../../ConfigProvider");
const constants_1 = require("../../PageContent/constants");
const useViewportType_1 = require("../../useViewportType");
const useZoom_1 = require("../hooks/useZoom");
const utils_1 = require("../utils");
const useLogic = ({ file, scrollRef, paginationRef, isFullHeight, isCentered, renderPageRange, isHidePersonalData, isViewerLite, enabledZoom = true, options, ...props }) => {
    const [containerWidth, setContainerWidth] = (0, react_1.useState)(1);
    const { isMobile } = (0, useViewportType_1.useViewportType)();
    const scrollContainerRef = (0, react_1.useRef)(null);
    (0, react_1.useEffect)(() => {
        const el = scrollRef?.current;
        if (!el) {
            return;
        }
        const isPageContentScroll = el.classList.contains(constants_1.pageContentClassnames.content);
        if (!isPageContentScroll) {
            return;
        }
        el.classList.add(constants_1.pageContentClassnames.withPdfViewer);
    }, [scrollRef]);
    const { zoomOut, scale, zoomIn, shouldAnimateZoom } = (0, useZoom_1.useZoom)({
        scrollContainerRef,
        enabledZoom: enabledZoom && !isMobile,
    });
    const [isError, setIsError] = (0, react_1.useState)(false);
    const [errorData, setErrorData] = (0, react_1.useState)();
    const [isLoading, setIsLoading] = (0, react_1.useState)(true);
    const [pdfParams, setPdfParams] = (0, react_1.useState)(null);
    const [pageHeightList, setPageHeightList] = (0, react_1.useState)(null);
    const onLoadSuccess = (pdf) => {
        props.onLoadSuccess?.(pdf);
        setPdfParams(pdf);
        setIsLoading(false);
    };
    (0, react_1.useEffect)(() => {
        if (!pdfParams) {
            return;
        }
        (0, utils_1.getPageHeightList)(pdfParams, containerWidth, scrollRef, renderPageRange, isFullHeight).then((res) => setPageHeightList(res));
    }, [pdfParams, containerWidth]);
    /**
     * Компонент обернут в ContentState. При переключении флага isError происходит mount компонента и запрос отправляется заново
     */
    const onRetry = () => {
        setIsError(false);
    };
    const { imagesMap } = (0, react_1.useContext)(ConfigProvider_1.ConfigContext);
    const errorState = (0, react_1.useMemo)(() => {
        if (!errorData) {
            return;
        }
        const customErrorState = props?.getErrorState?.(errorData);
        return {
            imgSrc: customErrorState?.errorImgSrc || imagesMap.defaultErrorImgSrc,
            imgAlt: 'Ошибка',
            actions: customErrorState?.actions,
            onRetry: customErrorState?.onRetry || onRetry,
            title: customErrorState?.message || 'Ошибка при загрузке файла',
            errorList: customErrorState?.errorList || ['Попробуйте еще раз'],
        };
    }, [errorData]);
    const onLoadError = (error) => {
        setIsLoading(false);
        setIsError(true);
        setErrorData(error);
        props.onLoadError?.(error);
    };
    const [initialIndex, setInitialIndex] = (0, react_1.useState)(null);
    const onChangePage = (newPage) => {
        props.onChangePage?.(newPage);
        setInitialIndex(newPage - 1);
    };
    const isRenderList = pageHeightList?.[0] !== 0;
    const loadingPlaceholderTitle = isViewerLite
        ? undefined
        : 'Загрузка данных...';
    const getOptions = (0, react_1.useMemo)(() => {
        return {
            withCredentials: true,
            ...options,
        };
    }, [options]);
    return {
        documentProps: {
            file,
            options: getOptions,
            onLoadSuccess,
            onLoadError,
            setContainerWidth,
        },
        pageProps: {
            scrollRef,
            onChangePage,
            isHidePersonalData,
            currentPage: Number(initialIndex) + 1,
        },
        pageCount: pdfParams?.numPages || 0,
        pageHeightList,
        isError,
        errorState,
        onRetry,
        paginationRef,
        isLoading,
        initialIndex,
        isRenderList,
        isViewerLite,
        scale,
        zoomIn,
        zoomOut,
        isDisabledZoomOutButton: scale === 1,
        isDisabledZoomInButton: scale === 3,
        scrollContainerRef,
        shouldAnimateZoom,
        loadingPlaceholderTitle,
        enabledZoom,
        isCentered,
    };
};
exports.useLogic = useLogic;
