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

require("../../_virtual/_rolldown/runtime.js");
const require_settings = require("../../settings.js");
let _carbon_react = require("@carbon/react");
//#region src/components/Datagrid/useOnRowClick.ts
const useOnRowClick = (hooks) => {
	const useInstance = (rowInstance) => {
		const carbonPrefix = (0, _carbon_react.usePrefix)();
		const { onRowClick } = rowInstance;
		const getRowProps = (props, datagridState) => {
			const { isFetching, row, instance } = datagridState;
			const { id, toggleRowSelected } = row;
			const { withSelectRows, tableId } = instance;
			const onClick = (event) => {
				if (!isFetching && onRowClick) {
					onRowClick(row, event);
					const clickableSelectedRows = document.querySelectorAll(`#${tableId}.${require_settings.pkg.prefix}--datagrid .${carbonPrefix}--data-table--selected:not(.${require_settings.pkg.prefix}--datagrid__active-row)`);
					if (clickableSelectedRows.length) Array.from(clickableSelectedRows).forEach((row) => {
						row.classList.remove(`${carbonPrefix}--data-table--selected`);
					});
					event.currentTarget.closest("tr").classList.add(`${carbonPrefix}--data-table--selected`);
					if (!withSelectRows) {
						if (instance.selectedFlatRows) instance.selectedFlatRows.map((toggleRow) => toggleRow.toggleRowSelected?.(false));
						toggleRowSelected(id, true);
					}
				}
			};
			const onKeyDown = (event) => {
				const { key } = event;
				if (key === "Enter") onClick(event);
			};
			return [
				props,
				{
					onClick,
					onKeyDown
				},
				{
					tabIndex: 0,
					style: { cursor: "pointer" }
				}
			];
		};
		hooks.getRowProps.push(getRowProps);
	};
	hooks.useInstance.push(useInstance);
};
//#endregion
exports.default = useOnRowClick;
