"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const ConfigProvider_1 = require("../../ConfigProvider");
const useScrollManagement_1 = require("../../useScrollManagement");
const useStickyButton_1 = require("../../useStickyButton");
const useViewportType_1 = require("../../useViewportType");
const classNames_1 = require("../../utils/classNames");
const constants_1 = require("../constants");
const useLogic = ({ list, isLoading, isError, errorMsg, errorImgSrc, scrollRef, skeleton, onRetry, endOfScrollMsg, isEndReached, onLoadMore, noDataImgSrc, defineCard, desktopCardWidth, }) => {
    const { imagesMap } = (0, react_1.useContext)(ConfigProvider_1.ConfigContext);
    const { isMobile } = (0, useViewportType_1.useViewportType)();
    (0, react_1.useEffect)(() => {
        if (isMobile && scrollRef?.current) {
            scrollRef.current.style.backgroundColor = 'unset';
            scrollRef.current.style.padding = 'unset';
        }
    }, [isMobile]);
    const contentStateProps = (0, react_1.useMemo)(() => {
        const isNoData = !list?.length;
        return {
            isLoading: isNoData && isLoading,
            isError: isNoData && isError,
            loadingTitle: 'Загрузка данных',
            errorState: {
                imgAlt: 'Что-то пошло не так',
                errorList: [errorMsg || ''],
                imgSrc: errorImgSrc || imagesMap.defaultErrorImgSrc,
                onRetry,
            },
        };
    }, [list, isLoading, isError, errorMsg, errorImgSrc, imagesMap]);
    const { scrollParent, recalculateVerticalScroll, handleEndReach, isEndReachedWithScroll, } = (0, useScrollManagement_1.useScrollManagement)({
        scrollRef,
        isEndReached,
        onLoadMore,
        virtuosoId: constants_1.VIRTUOSO_CONTAINER_ID,
    });
    const { virtuoso, isStickyButtonActive, handleRangeChanged: propsHandleRangeChanged, handleScrollToTop, } = (0, useStickyButton_1.useStickyButton)(scrollRef);
    const handleRangeChanged = (range) => {
        recalculateVerticalScroll();
        propsHandleRangeChanged(range);
    };
    const classnames = (0, react_1.useMemo)(() => (0, classNames_1.classNames)(constants_1.cardGridClassnames.root, {
        [constants_1.cardGridClassnames.loading]: isLoading,
    }), [isLoading]);
    return {
        contentStateProps,
        desktopCardWidth,
        virtuosoProps: {
            id: constants_1.VIRTUOSO_CONTAINER_ID,
            style: { height: '100%' },
            ref: virtuoso,
            overscan: constants_1.OVERSCAN,
            rangeChanged: handleRangeChanged,
            endReached: handleEndReach,
        },
        footerProps: {
            isError,
            isEndReached: isEndReachedWithScroll,
            onRetry,
            endOfScrollMsg,
            isLoading,
        },
        onTopButtonProps: {
            isActive: isStickyButtonActive,
            handleScrollToTop,
            scrollRef,
        },
        isMobile,
        scrollParent,
        skeleton,
        list,
        noDataImgSrc,
        defineCard,
        classnames,
    };
};
exports.useLogic = useLogic;
