"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.noSelectionState = void 0;
exports.useTableSelection = useTableSelection;
const react_1 = require("react");
const DataGridRoot_context_1 = require("../../../data-grid/root/DataGridRoot.context");
const utils_external_1 = require("../../../utils-external");
const hooks_1 = require("../../../utils/hooks");
const getMultipleSelectProps_1 = require("../helpers/selection/getMultipleSelectProps");
const getSingleSelectProps_1 = require("../helpers/selection/getSingleSelectProps");
function useTableSelection({ selection = {
    mode: "none",
}, selectionTrigger = "row", tableItems, }) {
    const { isLoading } = (0, DataGridRoot_context_1.useDataGridContext)();
    const { mode, defaultSelectedRowIds, selectedRowIds: selectedRowIdsProp, onSelectedRowIdsChange, enableRowSelection, } = selection;
    const { visibleRowIds = [] } = tableItems;
    const radioGroupName = (0, utils_external_1.useId)();
    const [selectedKeys, setSelectedKeys] = (0, hooks_1.useControllableState)({
        value: mode !== "none" ? selectedRowIdsProp : undefined,
        defaultValue: defaultSelectedRowIds !== null && defaultSelectedRowIds !== void 0 ? defaultSelectedRowIds : [],
        onChange: onSelectedRowIdsChange,
    });
    const selectedKeysSet = (0, react_1.useMemo)(() => new Set(selectedKeys), [selectedKeys]);
    const isRowSelected = (0, react_1.useCallback)((rowId) => selectedKeysSet.has(rowId), [selectedKeysSet]);
    return (0, react_1.useMemo)(() => {
        const baseSelection = { selectedKeys, isRowSelected };
        if (mode === "none") {
            return {
                selection: Object.assign(Object.assign({ mode }, baseSelection), { selectedKeys: [] }),
                selectionTrigger,
                renderSelection: false,
            };
        }
        if (mode === "single") {
            return {
                selection: Object.assign(Object.assign({ mode }, baseSelection), (0, getSingleSelectProps_1.getSingleSelectProps)({
                    selectedKeysSet,
                    setSelectedKeys,
                    name: radioGroupName,
                    enableRowSelection,
                })),
                selectionTrigger,
                renderSelection: visibleRowIds.length !== 0,
            };
        }
        return {
            selection: Object.assign(Object.assign({ mode }, baseSelection), (0, getMultipleSelectProps_1.getMultipleSelectProps)({
                selectedKeysSet,
                selectedKeys,
                setSelectedKeys,
                enableRowSelection,
                tableItems,
                isLoading,
            })),
            selectionTrigger,
            renderSelection: visibleRowIds.length !== 0,
        };
    }, [
        mode,
        selectedKeys,
        selectedKeysSet,
        isRowSelected,
        selectionTrigger,
        visibleRowIds,
        setSelectedKeys,
        radioGroupName,
        enableRowSelection,
        tableItems,
        isLoading,
    ]);
}
/**
 * TODO: Only temp needed to keep Root happy
 */
const noSelectionState = {
    selection: {
        mode: "none",
        selectedKeys: [],
        isRowSelected: () => false,
    },
    selectionTrigger: "row",
    renderSelection: false,
};
exports.noSelectionState = noSelectionState;
//# sourceMappingURL=useTableSelection.js.map