"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDataGridMobileLogic = void 0;
const react_1 = require("react");
const constants_1 = require("../../../../PageLayoutContainer/constants");
const useViewportType_1 = require("../../../../useViewportType");
const constants_2 = require("../../../constants");
const useDataGridMobileLogic = ({ containerRef, isLoading, columns, rows, isNoScrollToTop = false, }) => {
    const { isMobile } = (0, useViewportType_1.useViewportType)();
    const firstLoadedRowsCountRef = (0, react_1.useRef)(null);
    const mobileSkeletonCellsCount = (0, react_1.useMemo)(() => columns?.filter((column) => !column.isHideOnMobile).length ?? 0, [columns]);
    (0, react_1.useEffect)(() => {
        if (!isLoading &&
            rows &&
            rows.length > 0 &&
            firstLoadedRowsCountRef.current === null) {
            firstLoadedRowsCountRef.current = rows.length;
        }
    }, [rows, isLoading]);
    (0, react_1.useLayoutEffect)(() => {
        if (!isMobile || isNoScrollToTop) {
            return;
        }
        if (isLoading && containerRef?.current) {
            const pageLayout = containerRef.current.closest(`.${constants_1.pageLayoutClassnames.root}`);
            if (pageLayout) {
                pageLayout.scrollTo({
                    top: 0,
                    behavior: 'instant',
                });
            }
        }
    }, [containerRef, isLoading, isMobile, isNoScrollToTop]);
    const mobileSkeletonProps = {
        cellsCount: mobileSkeletonCellsCount,
        rowsCount: firstLoadedRowsCountRef.current ?? constants_2.DEFAULT_MOBILE_SKELETON_ROWS_COUNT,
    };
    return { isMobile, mobileSkeletonProps };
};
exports.useDataGridMobileLogic = useDataGridMobileLogic;
