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

import { __toESM } from "../../_virtual/_rolldown/runtime.js";
import { require_classnames } from "../../node_modules/classnames/index.js";
import { pkg } from "../../settings.js";
import { useIsomorphicEffect } from "../../global/js/hooks/useIsomorphicEffect.js";
import { handleToggleRowSelected } from "./Datagrid/addons/stateReducer.js";
import { selectionColumnId } from "./common-column-ids.js";
import { SelectAll } from "./Datagrid/DatagridSelectAll.js";
import React, { useCallback, useState } from "react";
import { TableSelectRow, usePrefix } from "@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__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--datagrid`;
const checkboxClass = `${pkg.prefix}--datagrid__checkbox-cell`;
const renderSelectAll = (gridState) => /* @__PURE__ */ React.createElement(SelectAll, gridState);
const renderSelectCell = (gridState) => /* @__PURE__ */ React.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: selectionColumnId,
				Header: renderSelectAll,
				Cell: renderSelectCell
			},
			...newColOrder
		];
	});
};
const useHighlightSelection = (hooks) => {
	const carbonPrefix = 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] = useState();
	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 = useCallback((event) => {
		event.stopPropagation();
		if (radio) {
			toggleAllRowsSelected(false);
			if (onRadioSelect) onRadioSelect(row);
		}
		onChange(event);
		onRowSelect?.(row, event);
		handleToggleRowSelected({
			dispatch,
			rowData: row,
			isChecked: event.target.checked,
			getRowId,
			selectAll: null
		});
	}, [
		dispatch,
		getRowId,
		onChange,
		onRadioSelect,
		onRowSelect,
		radio,
		row,
		toggleAllRowsSelected
	]);
	return /* @__PURE__ */ React.createElement(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
export { useSelectRows as default };