/**
 * 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_common_column_ids = require('../common-column-ids.js');
const require_DatagridAiLabel = require('./addons/AiLabel/DatagridAiLabel.js');
let react = require("react");
react = require_runtime.__toESM(react);
let _carbon_react = require("@carbon/react");
let _carbon_layout = require("@carbon/layout");

//#region src/components/Datagrid/Datagrid/DatagridRow.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 rowHeights = {
	xs: 24,
	sm: 32,
	md: 40,
	lg: 48,
	xl: 64
};
const DatagridRow = (datagridState) => {
	const { row, rows, rowSize, withNestedRows, prepareRow, key, tableId, withExpandedRows, withMouseHover, setMouseOverRowIndex, headers, visibleColumns, getAsyncSubRows } = datagridState;
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	const getVisibleNestedRowCount = ({ isExpanded, subRows }) => {
		let size = 0;
		if (isExpanded && subRows) {
			size += subRows.length;
			subRows.forEach((child) => {
				size += getVisibleNestedRowCount(child);
			});
		}
		return size;
	};
	const hoverHandler = (event) => {
		if (!withNestedRows) return;
		const totalNestedRowIndicatorHeight = (0, _carbon_layout.px)(getVisibleNestedRowCount(row) * rowHeights[Number(rowSize)]);
		const hoverRow = event.target.closest(`.${blockClass}__carbon-row-expanded`);
		hoverRow?.classList.add(`${blockClass}__carbon-row-expanded-hover-active`);
		const rowExpanderButton = hoverRow?.querySelector(`.${blockClass}__row-expander`);
		const rowSizeValue = rowSize || "lg";
		hoverRow?.style?.setProperty(`--${blockClass}--indicator-height`, totalNestedRowIndicatorHeight);
		hoverRow?.style?.setProperty(`--${blockClass}--row-height`, (0, _carbon_layout.px)(rowHeights[rowSizeValue]));
		hoverRow?.style?.setProperty(`--${blockClass}--indicator-offset-amount`, (0, _carbon_layout.px)(rowExpanderButton?.offsetLeft || 0));
	};
	const focusRemover = () => {
		document.querySelectorAll(`#${tableId} .${blockClass}__carbon-row-expanded`).forEach((el) => {
			el.classList.remove(`${blockClass}__carbon-row-expanded-hover-active`);
		});
	};
	const renderExpandedRow = () => {
		if (row.isExpanded) {
			prepareRow(row);
			return row?.RowExpansionRenderer?.({
				...datagridState,
				row
			});
		}
		return null;
	};
	const handleMouseLeave = (event) => {
		if (withMouseHover) setMouseOverRowIndex?.(null);
		event.target.closest(`.${blockClass}__carbon-row-expanded`)?.classList.remove(`${blockClass}__carbon-row-expanded-hover-active`);
	};
	const handleOnKeyUp = (event) => {
		if (!withNestedRows) return;
		if (event.key === "Enter" || event.key === "Space") {
			focusRemover();
			hoverHandler(event);
		}
	};
	const setAdditionalRowProps = () => {
		if (withNestedRows || withExpandedRows) return { "data-nested-row-id": row.id };
		return {};
	};
	const { className, ...rowProps } = row.getRowProps({ role: void 0 });
	const foundAIRow = rows.some((r) => (0, react.isValidElement)(r?.original?.aiLabel) || (0, react.isValidElement)(r?.original?.slug));
	const rowClassNames = (0, import_classnames.default)(`${blockClass}__carbon-row`, {
		[`${blockClass}__carbon-row-expanded`]: row.isExpanded,
		[`${blockClass}__carbon-row-expandable`]: row.canExpand,
		[`${blockClass}__carbon-row-expandable--async`]: getAsyncSubRows && row.depth > 0,
		[`${carbonPrefix}--data-table--selected`]: row.isSelected,
		[`${blockClass}__slug--row`]: (0, react.isValidElement)(row?.original?.slug),
		[`${blockClass}__ai-label--row`]: (0, react.isValidElement)(row?.original?.aiLabel)
	});
	const withActionsColumn = headers ? !!headers.filter((header) => header.isAction).length : false;
	const rowKey = !row.isSkeleton ? key : row.skeletonKey;
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, { key: `${rowKey}__${key}__row--fragment` }, /* @__PURE__ */ react.default.createElement(_carbon_react.TableRow, {
		...rowProps,
		className: (0, import_classnames.default)(rowClassNames, className),
		key: rowKey,
		onMouseEnter: hoverHandler,
		onMouseLeave: handleMouseLeave,
		onFocus: hoverHandler,
		onBlur: focusRemover,
		onKeyUp: handleOnKeyUp,
		...setAdditionalRowProps()
	}, foundAIRow ? row?.original?.aiLabel ? /* @__PURE__ */ react.default.createElement("td", { className: (0, import_classnames.default)(`${blockClass}__table-row-ai-enabled`, { [`${blockClass}__ai-label--expanded`]: row.isExpanded }) }, /* @__PURE__ */ react.default.createElement(require_DatagridAiLabel.DatagridAILabel, { aiLabel: row?.original?.aiLabel })) : row?.original?.slug ? /* @__PURE__ */ react.default.createElement("td", { className: (0, import_classnames.default)(`${blockClass}__table-row-ai-enabled`, { [`${blockClass}__slug--expanded`]: row.isExpanded }) }, /* @__PURE__ */ react.default.createElement(require_DatagridAiLabel.DatagridAILabel, { aiLabel: row?.original?.slug })) : /* @__PURE__ */ react.default.createElement("td", { className: `${blockClass}__table-row-ai-spacer` }) : null, row.cells.map((cell, index) => {
		const { style, children, ...restProps } = cell.getCellProps({ role: void 0 });
		const columnClassname = cell?.column?.className;
		const content = children || /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, !row.isSkeleton && cell.render("Cell"), row.isSkeleton && /* @__PURE__ */ react.default.createElement(_carbon_react.SkeletonText, null));
		if (cell?.column?.id === require_common_column_ids.selectionColumnId) return cell.render("Cell", { key: cell.column.id });
		const associatedHeader = headers?.filter((h) => h.id === cell.column.id);
		const lastVisibleIndex = withActionsColumn ? 2 : 1;
		const lastVisibleFlexStyle = index === visibleColumns.length - lastVisibleIndex ? "1 0 auto" : "0 0 auto";
		if (style) style.flex = lastVisibleFlexStyle;
		return /* @__PURE__ */ react.default.createElement(_carbon_react.TableCell, {
			className: (0, import_classnames.default)(`${blockClass}__cell`, {
				[`${blockClass}__expandable-row-cell`]: (row.canExpand || getAsyncSubRows) && index === 0,
				[`${blockClass}__expandable-row-cell--is-expanded`]: row.isExpanded && index === 0,
				[`${blockClass}__slug--cell`]: associatedHeader && associatedHeader.length && (0, react.isValidElement)(associatedHeader[0]?.slug),
				[`${blockClass}__ai-label--cell`]: associatedHeader && associatedHeader.length && (0, react.isValidElement)(associatedHeader[0]?.aiLabel)
			}, columnClassname),
			...restProps,
			style,
			key: cell.column.id
		}, content);
	})), renderExpandedRow?.() || void 0);
};

//#endregion
exports.default = DatagridRow;