/**
 * 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 { prepareProps } from "../../global/js/utils/props-helper.js";
import React from "react";
import { IconButton, IconSkeleton, OverflowMenu, OverflowMenuItem } from "@carbon/react";

//#region src/components/Datagrid/useActionsColumn.tsx
/**
* Copyright IBM Corp. 2021, 2023
*
* 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 useActionsColumn = (hooks) => {
	const useAttachActionsOnInstance = (instance) => {
		const { rowActions, isFetching, state: { selectedRowIds } } = instance;
		const getDisabledState = (rowIndex) => {
			if (Object.keys(selectedRowIds).map((n) => Number(n)).includes(rowIndex)) return true;
			return false;
		};
		if (rowActions && Array.isArray(rowActions)) {
			const addActionsMenu = (props, cellData) => {
				const { cell } = cellData;
				const { row, column } = cell;
				if (column.isAction) {
					const isColumnSticky = !!column.sticky;
					return [props, {
						children: /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__actions-column-contents` }, isFetching && /* @__PURE__ */ React.createElement(IconSkeleton, { className: `${blockClass}__actions-column-loading` }), !isFetching && rowActions.length <= 2 && !isColumnSticky && /* @__PURE__ */ React.createElement("div", { className: `${blockClass}_actions-column` }, rowActions.map((action, index) => {
							const { align, id, itemText, onClick, icon, shouldHideMenuItem, shouldDisableMenuItem, disabled, ...rest } = prepareProps(action, ["isDelete"]);
							const hidden = typeof shouldHideMenuItem === "function" && shouldHideMenuItem(row);
							const isDisabledByRow = typeof shouldDisableMenuItem === "function" ? shouldDisableMenuItem(row) : disabled;
							if (hidden) return null;
							const Icon = icon;
							return /* @__PURE__ */ React.createElement("div", {
								className: (0, import_classnames.default)(`${blockClass}__actions-column-button`, { [`${blockClass}__disabled-row-action-button`]: getDisabledState(row.index) }),
								key: `${itemText}__${index}`
							}, /* @__PURE__ */ React.createElement(IconButton, {
								...rest,
								align: align || "top",
								label: itemText,
								kind: "ghost",
								name: itemText,
								className: (0, import_classnames.default)({ [`${blockClass}__disabled-row-action`]: getDisabledState(row.index) }),
								onClick: (e) => {
									if (getDisabledState(row.index)) return;
									e.stopPropagation();
									onClick?.(id, row, e);
								},
								disabled: isDisabledByRow
							}, Icon && /* @__PURE__ */ React.createElement(Icon, null)));
						})), !isFetching && (rowActions.length > 2 || isColumnSticky) && /* @__PURE__ */ React.createElement("div", null, /* @__PURE__ */ React.createElement(OverflowMenu, {
							align: "left",
							size: "sm",
							flipped: true,
							onClick: (e) => {
								e.stopPropagation();
							}
						}, rowActions.map((action) => {
							const { id, onClick, shouldHideMenuItem, shouldDisableMenuItem, disabled, ...rest } = action;
							const hidden = typeof shouldHideMenuItem === "function" && shouldHideMenuItem(row);
							const isDisabledByRow = typeof shouldDisableMenuItem === "function" ? shouldDisableMenuItem(row) : disabled;
							if (hidden) return null;
							return /* @__PURE__ */ React.createElement(OverflowMenuItem, {
								...rest,
								disabled: isDisabledByRow,
								onClick: (e) => {
									e.stopPropagation();
									onClick?.(id, row, e);
								},
								key: id
							});
						})))),
						className: (0, import_classnames.default)({
							[`${blockClass}__actions-column-cell`]: true,
							[`${blockClass}__cell`]: true,
							[`${blockClass}__actions-column-cell-non-sticky`]: !isColumnSticky
						}),
						style: { width: rowActions.length > 2 || isColumnSticky ? 48 : 96 }
					}];
				}
				return [props];
			};
			hooks.getCellProps.push(addActionsMenu);
		}
	};
	const useStickyHeaderWidth = (instance) => {
		const { rowActions } = instance;
		if (rowActions && Array.isArray(rowActions)) {
			const addHeaderWidth = (props, cellData) => {
				const { column } = cellData;
				if (column.isAction) {
					const isColumnSticky = !!column.sticky;
					return [props, { style: {
						...props.style,
						width: rowActions.length > 2 || isColumnSticky ? 48 : 96
					} }];
				}
				return [props];
			};
			hooks.getHeaderProps.push(addHeaderWidth);
		}
	};
	hooks.useInstance.push(useStickyHeaderWidth);
	hooks.useInstance.push(useAttachActionsOnInstance);
};

//#endregion
export { useActionsColumn as default };