import { consoleWarning } from "../../../../utils/helpers/consoleWarning.js";
import { canSelectTableRow, mutateRowSelection } from "./selection.utils.js";
function getMultipleSelectProps({ selectedKeysSet, selectedKeys, setSelectedKeys, enableRowSelection, tableItems, isLoading, }) {
    const selectableIdsSet = new Set();
    for (const [id, { rowData }] of tableItems.itemDetails) {
        if (canSelectTableRow(enableRowSelection, { row: rowData, id })) {
            selectableIdsSet.add(id);
        }
    }
    let selectedOnPageCount = 0;
    for (const id of selectableIdsSet) {
        selectedKeysSet.has(id) && selectedOnPageCount++;
    }
    const isAllSelected = selectableIdsSet.size > 0 && selectedOnPageCount === selectableIdsSet.size;
    const someSelected = selectedOnPageCount > 0;
    const handleToggleRow = (key, row) => {
        if (!row) {
            consoleWarning(`DataGrid.Table: Row data is undefined for key ${key}. This may cause issues with selection if enableRowSelection is used.`);
        }
        const checked = !selectedKeysSet.has(key);
        const nextSet = new Set(selectedKeysSet);
        const changed = mutateRowSelection({
            selectedRowIds: nextSet,
            rowId: key,
            checked,
            childRowIdsById: tableItems.childRowIdsById,
            itemDetails: tableItems.itemDetails,
            enableRowSelection,
        });
        if (changed) {
            setSelectedKeys([...nextSet]);
        }
    };
    const toggleAllRowSelected = (event) => {
        if (isLoading) {
            return;
        }
        if (event.target.checked) {
            const preserved = selectedKeys.filter((k) => !selectableIdsSet.has(k));
            setSelectedKeys([...preserved, ...selectableIdsSet]);
        }
        else {
            setSelectedKeys(selectedKeys.filter((k) => !selectableIdsSet.has(k)));
        }
    };
    return {
        getTheadCheckboxProps: () => ({
            checked: isAllSelected,
            indeterminate: !isAllSelected && someSelected,
            onChange: toggleAllRowSelected,
            disabled: selectableIdsSet.size === 0 || isLoading,
        }),
        getRowCheckboxProps: (key, row) => {
            return {
                onChange: () => handleToggleRow(key, row),
                checked: selectedKeysSet.has(key),
                indeterminate: false,
                disabled: !canSelectTableRow(enableRowSelection, { row, id: key }),
            };
        },
        toggleSelection: handleToggleRow,
    };
}
export { getMultipleSelectProps };
//# sourceMappingURL=getMultipleSelectProps.js.map