/**
 * Copyright IBM Corp. 2020, 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

//#region src/components/Datagrid/useDisableSelectRows.ts
const nonselectablerowsList = (instance) => {
	return instance?.rows?.filter((row) => instance.shouldDisableSelectRow && instance.shouldDisableSelectRow(row)).map((row) => row.id) || [];
};
const useDisableSelectRows = (hooks) => {
	updateSelectAll(hooks);
	updatePageSelectAll(hooks);
	let nonselectablerows = [];
	const useInstance = (instance) => {
		nonselectablerows = nonselectablerowsList(instance);
	};
	hooks.useInstance.push(useInstance);
	const getRowProps = (props, { row, instance }) => {
		return [props, {
			disabled: instance?.shouldDisableSelectRow?.(row),
			nonselectablerows
		}];
	};
	hooks.getRowProps.push(getRowProps);
};
const updateSelectAll = (hooks) => {
	const getToggleAllRowsSelectedProps = (props, { instance }) => {
		const selectableRows = instance?.rows?.filter((row) => !(instance.shouldDisableSelectRow && instance.shouldDisableSelectRow(row))) || [];
		const isAllRowsSelected = selectableRows?.length > 0 && selectableRows.every(({ id }) => instance?.state?.selectedRowIds?.[id]);
		return [props, {
			onChange: (e) => {
				selectableRows.forEach((row) => row?.toggleRowSelected?.(e.target.checked));
			},
			style: { cursor: "pointer" },
			checked: isAllRowsSelected,
			disabled: instance?.disableSelectAll,
			title: instance?.disableSelectRowsProps?.labels?.toggleAllRowsLabel || "Toggle All Rows Selected",
			indeterminate: Boolean(!isAllRowsSelected && Object.keys(instance?.state?.selectedRowIds || {}).length)
		}];
	};
	hooks.getToggleAllRowsSelectedProps.push(getToggleAllRowsSelectedProps);
};
const updatePageSelectAll = (hooks) => {
	const getToggleAllPageRowsSelectedProps = (props, { instance }) => {
		const selectableRows = instance?.page?.filter((row) => !(instance.shouldDisableSelectRow && instance.shouldDisableSelectRow(row))) || [];
		const isAllRowsSelected = selectableRows?.length > 0 && selectableRows.every(({ id }) => instance?.state?.selectedRowIds?.[id]);
		return [props, {
			onChange: (e) => {
				selectableRows.forEach((row) => row.toggleRowSelected(e.target.checked));
			},
			style: { cursor: "pointer" },
			checked: isAllRowsSelected,
			disabled: instance?.disableSelectAll,
			title: instance?.disableSelectRowsProps?.labels?.toggleAllRowsLabel || "Toggle All Rows Selected",
			indeterminate: Boolean(!isAllRowsSelected && instance?.page?.some(({ id }) => instance?.state?.selectedRowIds?.[id]))
		}];
	};
	hooks.getToggleAllPageRowsSelectedProps.push(getToggleAllPageRowsSelectedProps);
};
//#endregion
export { useDisableSelectRows as default };
