/**
 * 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_useFocusRowExpander = require("./useFocusRowExpander.js");
let react = require("react");
react = require_runtime.__toESM(react);
let _carbon_react = require("@carbon/react");
let _carbon_react_icons = require("@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__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--datagrid`;
const useRowExpander = (hooks) => {
	const tempState = (0, react.useRef)(void 0);
	const lastExpandedRowIndex = (0, react.useRef)(void 0);
	const useInstance = (instance) => {
		tempState.current = instance;
	};
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	require_useFocusRowExpander.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.default.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.default.createElement(_carbon_react_icons.ChevronUp, { className: `${blockClass}__row-expander--icon` }) : /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ChevronDown, { className: `${blockClass}__row-expander--icon` }));
			},
			width: 48,
			disableResizing: true,
			disableSortBy: true,
			Header: ""
		}, ...columns];
	};
	hooks.visibleColumns.push(visibleColumns);
	hooks.useInstance.push(useInstance);
};
//#endregion
exports.default = useRowExpander;
