"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMultipleSelectProps = getMultipleSelectProps;
const consoleWarning_1 = require("../../../../utils/helpers/consoleWarning");
const selection_utils_1 = require("./selection.utils");
function getMultipleSelectProps({ selectedKeysSet, selectedKeys, setSelectedKeys, enableRowSelection, tableItems, isLoading, }) {
    const selectableIdsSet = new Set();
    for (const [id, { rowData }] of tableItems.itemDetails) {
        if ((0, selection_utils_1.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) {
            (0, consoleWarning_1.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 = (0, selection_utils_1.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: !(0, selection_utils_1.canSelectTableRow)(enableRowSelection, { row, id: key }),
            };
        },
        toggleSelection: handleToggleRow,
    };
}
//# sourceMappingURL=getMultipleSelectProps.js.map