/**
 * 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 { handleDynamicRowCheck } from "./Datagrid/addons/stateReducer.js";
import { useFocusRowExpander } from "./useFocusRowExpander.js";
import React, { useRef } from "react";
import { usePrefix } from "@carbon/react";
import { ChevronRight } from "@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__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--datagrid`;
const useNestedRowExpander = (hooks) => {
	const carbonPrefix = usePrefix();
	const tempState = useRef(void 0);
	const lastExpandedRowIndex = useRef(void 0);
	const useInstance = (instance) => {
		tempState.current = instance;
	};
	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 {
							handleDynamicRowCheck({
								dispatch,
								status: "start",
								rowId: row.id,
								depth: row.depth,
								index: row.index
							});
							if (getAsyncSubRows) await getAsyncSubRows?.(row);
							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.createElement("button", {
					type: "button",
					"aria-label": expanderTitle,
					className: (0, import_classnames.default)(`${blockClass}__row-expander`, `${carbonPrefix}--btn`, `${carbonPrefix}--btn--ghost`),
					...expanderButtonProps
				}, /* @__PURE__ */ React.createElement(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
export { useNestedRowExpander as default };
