/**
 * 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 { useFocusRowExpander } from "./useFocusRowExpander.js";
import React, { useRef } from "react";
import { usePrefix } from "@carbon/react";
import { ChevronDown, ChevronUp } from "@carbon/react/icons";
//#region src/components/Datagrid/useRowExpander.jsx
/**
* 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 useRowExpander = (hooks) => {
	const tempState = useRef(void 0);
	const lastExpandedRowIndex = useRef(void 0);
	const useInstance = (instance) => {
		tempState.current = instance;
	};
	const carbonPrefix = usePrefix();
	useFocusRowExpander({
		instance: tempState?.current,
		lastExpandedRowIndex: lastExpandedRowIndex?.current,
		blockClass,
		activeElement: document.activeElement
	});
	const visibleColumns = (columns) => {
		return [{
			id: "expander",
			Cell: ({ row }) => {
				const expanderButtonProps = {
					...row.getToggleRowExpandedProps(),
					onClick: (event) => {
						event.stopPropagation();
						row.toggleRowExpanded();
						row?.onClick?.(row, event);
						lastExpandedRowIndex.current = row.id;
					},
					title: null
				};
				const { expanderButtonTitleExpanded, expanderButtonTitleCollapsed } = tempState?.current || {};
				const expanderTitle = row.isExpanded ? expanderButtonTitleExpanded : expanderButtonTitleCollapsed;
				return row.canExpand && /* @__PURE__ */ React.createElement("button", {
					type: "button",
					"aria-label": expanderTitle,
					className: (0, import_classnames.default)(`${blockClass}__row-expander`, `${carbonPrefix}--btn`, `${carbonPrefix}--btn--ghost`),
					...expanderButtonProps
				}, row.isExpanded ? /* @__PURE__ */ React.createElement(ChevronUp, { className: `${blockClass}__row-expander--icon` }) : /* @__PURE__ */ React.createElement(ChevronDown, { className: `${blockClass}__row-expander--icon` }));
			},
			width: 48,
			disableResizing: true,
			disableSortBy: true,
			Header: ""
		}, ...columns];
	};
	hooks.visibleColumns.push(visibleColumns);
	hooks.useInstance.push(useInstance);
};
//#endregion
export { useRowExpander as default };
