/**
 * 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_stateReducer = require("./Datagrid/addons/stateReducer.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/useNestedRowExpander.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 useNestedRowExpander = (hooks) => {
	const carbonPrefix = (0, _carbon_react.usePrefix)();
	const tempState = (0, react.useRef)(void 0);
	const lastExpandedRowIndex = (0, react.useRef)(void 0);
	const useInstance = (instance) => {
		tempState.current = instance;
	};
	require_useFocusRowExpander.useFocusRowExpander({
		instance: tempState?.current,
		lastExpandedRowIndex: lastExpandedRowIndex?.current,
		blockClass,
		activeElement: typeof document !== "undefined" && document.activeElement
	});
	const visibleColumns = (columns, state) => {
		const { getAsyncSubRows, dispatch, state: tableState } = state.instance || {};
		const fetchingDynamicSubRows = !!tableState.dynamicRowSkeleton;
		return [{
			id: "expander",
			Cell: ({ row }) => {
				const expanderButtonProps = {
					...row.getToggleRowExpandedProps(),
					disabled: getAsyncSubRows && fetchingDynamicSubRows,
					onClick: async (event) => {
						event.stopPropagation();
						row.toggleRowExpanded();
						lastExpandedRowIndex.current = row.id;
						if (!row.isExpanded) try {
							require_stateReducer.handleDynamicRowCheck({
								dispatch,
								status: "start",
								rowId: row.id,
								depth: row.depth,
								index: row.index
							});
							if (getAsyncSubRows) await getAsyncSubRows?.(row);
							require_stateReducer.handleDynamicRowCheck({
								dispatch,
								status: "finish",
								rowId: row.id
							});
						} catch (error) {
							console.log({ error });
						}
					}
				};
				const { expanderButtonTitleExpanded = "Collapse row", expanderButtonTitleCollapsed = "Expand row" } = tempState?.current || {};
				const expanderTitle = row.isExpanded ? expanderButtonTitleExpanded : expanderButtonTitleCollapsed;
				return row.canExpand || getAsyncSubRows ? /* @__PURE__ */ react.default.createElement("button", {
					type: "button",
					"aria-label": expanderTitle,
					className: (0, import_classnames.default)(`${blockClass}__row-expander`, `${carbonPrefix}--btn`, `${carbonPrefix}--btn--ghost`),
					...expanderButtonProps
				}, /* @__PURE__ */ react.default.createElement(_carbon_react_icons.ChevronRight, { className: (0, import_classnames.default)(`${blockClass}__expander-icon`, {
					[`${blockClass}__expander-icon--not-open`]: !row.isExpanded,
					[`${blockClass}__expander-icon--open`]: row.isExpanded
				}) })) : null;
			},
			width: 48,
			disableResizing: true,
			disableSortBy: true,
			Header: ""
		}, ...columns];
	};
	hooks.visibleColumns.push(visibleColumns);
	hooks.useInstance.push(useInstance);
};
//#endregion
exports.default = useNestedRowExpander;
