/**
 * 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 { selectionColumnId } from "../common-column-ids.js";
import { DatagridAILabel } from "./addons/AiLabel/DatagridAiLabel.js";
import React, { isValidElement } from "react";
import { SkeletonText, TableCell, TableRow, usePrefix } from "@carbon/react";
import { px } from "@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__ */ __toESM(require_classnames());
const blockClass = `${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 = 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 = 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`, px(rowHeights[rowSizeValue]));
		hoverRow?.style?.setProperty(`--${blockClass}--indicator-offset-amount`, 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) => isValidElement(r?.original?.aiLabel) || 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`]: isValidElement(row?.original?.slug),
		[`${blockClass}__ai-label--row`]: isValidElement(row?.original?.aiLabel)
	});
	const withActionsColumn = headers ? !!headers.filter((header) => header.isAction).length : false;
	const rowKey = !row.isSkeleton ? key : row.skeletonKey;
	return /* @__PURE__ */ React.createElement(React.Fragment, { key: `${rowKey}__${key}__row--fragment` }, /* @__PURE__ */ React.createElement(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.createElement("td", { className: (0, import_classnames.default)(`${blockClass}__table-row-ai-enabled`, { [`${blockClass}__ai-label--expanded`]: row.isExpanded }) }, /* @__PURE__ */ React.createElement(DatagridAILabel, { aiLabel: row?.original?.aiLabel })) : row?.original?.slug ? /* @__PURE__ */ React.createElement("td", { className: (0, import_classnames.default)(`${blockClass}__table-row-ai-enabled`, { [`${blockClass}__slug--expanded`]: row.isExpanded }) }, /* @__PURE__ */ React.createElement(DatagridAILabel, { aiLabel: row?.original?.slug })) : /* @__PURE__ */ React.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.createElement(React.Fragment, null, !row.isSkeleton && cell.render("Cell"), row.isSkeleton && /* @__PURE__ */ React.createElement(SkeletonText, null));
		if (cell?.column?.id === 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.createElement(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 && isValidElement(associatedHeader[0]?.slug),
				[`${blockClass}__ai-label--cell`]: associatedHeader && associatedHeader.length && isValidElement(associatedHeader[0]?.aiLabel)
			}, columnClassname),
			...restProps,
			style,
			key: cell.column.id
		}, content);
	})), renderExpandedRow?.() || void 0);
};

//#endregion
export { DatagridRow as default };