/**
 * 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.
 */

const require_runtime = require('../../_virtual/_rolldown/runtime.js');
const require_index = require('../../node_modules/classnames/index.js');
const require_settings = require('../../settings.js');
const require_useIsomorphicEffect = require('../../global/js/hooks/useIsomorphicEffect.js');
const require_stateReducer = require('./Datagrid/addons/stateReducer.js');
const require_common_column_ids = require('./common-column-ids.js');
const require_DatagridSelectAll = require('./Datagrid/DatagridSelectAll.js');
let react = require("react");
react = require_runtime.__toESM(react);
let _carbon_react = require("@carbon/react");

//#region src/components/Datagrid/useSelectRows.tsx
/**
* Copyright IBM Corp. 2020, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const checkboxClass = `${require_settings.pkg.prefix}--datagrid__checkbox-cell`;
const renderSelectAll = (gridState) => /* @__PURE__ */ react.default.createElement(require_DatagridSelectAll.SelectAll, gridState);
const renderSelectCell = (gridState) => /* @__PURE__ */ react.default.createElement(SelectRow, gridState);
const useSelectRows = (hooks) => {
	useHighlightSelection(hooks);
	const useInstance = (instance) => {
		const { rows } = instance;
		const rowsWithSelect = rows.map((row) => ({
			...row,
			isSelectable: true
		}));
		Object.assign(instance, { rows: rowsWithSelect });
	};
	hooks.useInstance.push(useInstance);
	hooks.useInstance.push((instance) => {
		Object.assign(instance, { withSelectRows: true });
	});
	hooks.visibleColumns.push((columns) => {
		const newColOrder = [...columns];
		const expanderColumnIndex = newColOrder.findIndex((col) => col.id === "expander");
		const expanderCol = expanderColumnIndex > -1 ? newColOrder.splice(expanderColumnIndex, 1) : [];
		return [
			...expanderColumnIndex > -1 && expanderCol && expanderCol.length ? expanderCol : [],
			{
				id: require_common_column_ids.selectionColumnId,
				Header: renderSelectAll,
				Cell: renderSelectCell
			},
			...newColOrder
		];
	});
};
const useHighlightSelection = (hooks) => {
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	const getRowProps = (props, { row }) => {
		const { checked } = row.getToggleRowSelectedProps();
		return [props, { className: (0, import_classnames.default)([`${blockClass}__carbon-row`, {
			[`${carbonPrefix}--data-table--selected`]: checked,
			[`${blockClass}__active-row`]: checked
		}]) }];
	};
	hooks.getRowProps.push(getRowProps);
};
const SelectRow = (datagridState) => {
	const { isFetching = false, tableId, row, cell, radio, toggleAllRowsSelected, onRadioSelect, onRowSelect, columns, withStickyColumn, dispatch, getRowId } = datagridState;
	const [windowSize, setWindowSize] = (0, react.useState)();
	require_useIsomorphicEffect.useIsomorphicEffect(() => {
		setWindowSize(window.innerWidth);
		function updateSize() {
			setWindowSize(window.innerWidth);
		}
		window.addEventListener("resize", updateSize);
		return () => window.removeEventListener("resize", updateSize);
	}, []);
	const selectDisabled = isFetching || row.getRowProps().disabled;
	const { onChange, title, ...selectProps } = row.getToggleRowSelectedProps();
	const cellProps = cell.getCellProps();
	const isFirstColumnStickyLeft = columns[0]?.sticky === "left" && withStickyColumn;
	const rowId = `${tableId}-${row.id}-${row.index}`;
	const { key, _cellProps } = cellProps;
	const onSelectHandler = (0, react.useCallback)((event) => {
		event.stopPropagation();
		if (radio) {
			toggleAllRowsSelected(false);
			if (onRadioSelect) onRadioSelect(row);
		}
		onChange(event);
		onRowSelect?.(row, event);
		require_stateReducer.handleToggleRowSelected({
			dispatch,
			rowData: row,
			isChecked: event.target.checked,
			getRowId,
			selectAll: null
		});
	}, [
		dispatch,
		getRowId,
		onChange,
		onRadioSelect,
		onRowSelect,
		radio,
		row,
		toggleAllRowsSelected
	]);
	return /* @__PURE__ */ react.default.createElement(_carbon_react.TableSelectRow, {
		key,
		..._cellProps,
		...selectProps,
		radio,
		onSelect: onSelectHandler,
		id: rowId,
		name: `${rowId}-name`,
		className: (0, import_classnames.default)([
			checkboxClass,
			cellProps.className,
			{ [`${checkboxClass}-sticky-left`]: isFirstColumnStickyLeft && Number(windowSize) > 671 }
		]),
		"aria-label": title,
		disabled: selectDisabled
	});
};

//#endregion
exports.default = useSelectRows;