"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLogic = void 0;
const react_1 = require("react");
const utils_1 = require("../../../utils");
const utils_2 = require("./utils");
const useLogic = ({ columns, onSort, sorting, uncheckedRowsCount, rowsCount, onSelectAllRows, }) => {
    const isChecked = (0, react_1.useMemo)(() => !uncheckedRowsCount && rowsCount > 0, [uncheckedRowsCount, rowsCount]);
    const isIndeterminate = (0, react_1.useMemo)(() => uncheckedRowsCount > 0 && uncheckedRowsCount < rowsCount, [uncheckedRowsCount, rowsCount]);
    const isDisabled = !rowsCount;
    const checkboxProps = (0, react_1.useMemo)(() => ({
        checked: isChecked,
        disabled: isDisabled,
        indeterminate: isIndeterminate,
        onChange: onSelectAllRows,
    }), [isChecked, isDisabled, isIndeterminate, onSelectAllRows]);
    const handleSort = (0, react_1.useCallback)((field) => {
        if (!onSort) {
            return;
        }
        const isCurrentField = sorting?.fieldId === field;
        if (isCurrentField && sorting?.sort === 'asc') {
            return onSort({ fieldId: field, sort: 'desc' });
        }
        if (isCurrentField && sorting?.sort === 'desc') {
            return onSort(undefined);
        }
        onSort({ fieldId: field, sort: 'asc' });
    }, [sorting, onSort]);
    const gridPositions = (0, react_1.useMemo)(() => {
        return (0, utils_2.getGridPositions)(columns);
    }, [columns]);
    const headerCssVars = (0, react_1.useCallback)((position) => {
        if (!position) {
            return {};
        }
        return {
            '--grid-column-start': position.start,
            '--grid-column-end': position.end,
        };
    }, []);
    return {
        checkboxProps,
        handleSort,
        gridPositions,
        headerCssVars,
        hasGroupedColumns: columns.some(utils_1.isGroupColumns),
    };
};
exports.useLogic = useLogic;
