"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePinnedColumns = void 0;
const react_1 = require("react");
const useViewportType_1 = require("../../../../useViewportType");
const constants_1 = require("../../../constants");
const useRowHover_1 = require("../../../hooks/useRowHover");
const getColumnSections_1 = require("../../utils/getColumnSections");
const syncRowsHeight_1 = require("../../utils/syncRowsHeight");
const usePinnedColumns = ({ isLoading, columns, }) => {
    const containerRef = (0, react_1.useRef)(null);
    const { isMobile } = (0, useViewportType_1.useViewportType)();
    const hasPinnedColumns = (0, react_1.useMemo)(() => columns.some((column) => column.pinned), [columns]);
    const isRowHoverSyncEnabled = hasPinnedColumns && !isMobile;
    const { rowsMapRef, ...rowHoverProps } = (0, useRowHover_1.useRowHover)({
        hasPinnedColumns: isRowHoverSyncEnabled,
    });
    const isScrollStateSupported = CSS.supports('container-type', 'scroll-state');
    const checkOverflow = (0, react_1.useCallback)((element) => {
        if (element) {
            return element.scrollWidth > element.clientWidth;
        }
        return false;
    }, []);
    const { sections } = (0, react_1.useMemo)(() => (0, getColumnSections_1.getColumnSections)(columns), [columns]);
    (0, react_1.useLayoutEffect)(() => {
        if (isLoading ||
            !hasPinnedColumns ||
            isMobile ||
            typeof window === 'undefined') {
            return;
        }
        const resizeObserver = new ResizeObserver(() => {
            (0, syncRowsHeight_1.syncRowsHeight)(rowsMapRef.current);
            if (isScrollStateSupported) {
                return;
            }
            if (checkOverflow(containerRef.current)) {
                containerRef.current?.classList.add(constants_1.dataGridClassnames.overflowed);
            }
            else {
                containerRef.current?.classList.remove(constants_1.dataGridClassnames.overflowed);
            }
        });
        if (!containerRef.current) {
            return;
        }
        resizeObserver.observe(containerRef.current);
        return () => {
            resizeObserver.disconnect();
        };
    }, [hasPinnedColumns, isLoading, isMobile]);
    return { containerRef, rowHoverProps, sections, hasPinnedColumns };
};
exports.usePinnedColumns = usePinnedColumns;
