import { useCallback, useMemo } from "react";
import { useDataGridContext } from "../../../data-grid/root/DataGridRoot.context.js";
import { useId } from "../../../utils-external/index.js";
import { useControllableState } from "../../../utils/hooks/index.js";
import { getMultipleSelectProps } from "../helpers/selection/getMultipleSelectProps.js";
import { getSingleSelectProps } from "../helpers/selection/getSingleSelectProps.js";
function useTableSelection({ selection = {
    mode: "none",
}, selectionTrigger = "row", tableItems, }) {
    const { isLoading } = useDataGridContext();
    const { mode, defaultSelectedRowIds, selectedRowIds: selectedRowIdsProp, onSelectedRowIdsChange, enableRowSelection, } = selection;
    const { visibleRowIds = [] } = tableItems;
    const radioGroupName = useId();
    const [selectedKeys, setSelectedKeys] = useControllableState({
        value: mode !== "none" ? selectedRowIdsProp : undefined,
        defaultValue: defaultSelectedRowIds !== null && defaultSelectedRowIds !== void 0 ? defaultSelectedRowIds : [],
        onChange: onSelectedRowIdsChange,
    });
    const selectedKeysSet = useMemo(() => new Set(selectedKeys), [selectedKeys]);
    const isRowSelected = useCallback((rowId) => selectedKeysSet.has(rowId), [selectedKeysSet]);
    return 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), getSingleSelectProps({
                    selectedKeysSet,
                    setSelectedKeys,
                    name: radioGroupName,
                    enableRowSelection,
                })),
                selectionTrigger,
                renderSelection: visibleRowIds.length !== 0,
            };
        }
        return {
            selection: Object.assign(Object.assign({ mode }, baseSelection), 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,
};
export { useTableSelection, noSelectionState };
//# sourceMappingURL=useTableSelection.js.map