/**
 * 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 uuidv4 from "../../global/js/utils/uuidv4.js";
import { ActionBarItem } from "./ActionBarItem.js";
import { ActionBarOverflowItems } from "./ActionBarOverflowItems.js";
import { useOverflowItems } from "../../global/js/hooks/useOverflowItems/useOverflowItems.js";
import React, { useRef } from "react";
import PropTypes from "prop-types";
import { Button } from "@carbon/react";

//#region src/components/ActionBar/ActionBar.tsx
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${pkg.prefix}--action-bar`;
const componentName = "ActionBar";
const defaults = { actions: Object.freeze([]) };
/**
* The ActionBar is used internally by the PageHeader to wrap ActionBarItems.
*/
const ActionBar = React.forwardRef(({ actions = defaults.actions, className, maxVisible, menuOptionsClass, onWidthChange, overflowAriaLabel, overflowMenuRef, rightAlign, ...rest }, ref) => {
	const internalId = useRef(uuidv4());
	const refDisplayedItems = useRef(null);
	const backupRef = useRef(null);
	const localRef = ref || backupRef;
	const _offsetRef = useRef(null);
	const offsetRef = overflowMenuRef || _offsetRef;
	const { visibleItems, hiddenItems, itemRefHandler, offsetRefHandler } = useOverflowItems(actions.map((action) => ({
		id: action?.key,
		...action
	})), localRef, offsetRef, maxVisible, onWidthChange);
	const overflowMenuItems = hiddenItems?.map(({ id: key, ...rest }) => /* @__PURE__ */ React.createElement(ActionBarItem, {
		...rest,
		key
	}));
	return /* @__PURE__ */ React.createElement("div", {
		...rest,
		className: (0, import_classnames.default)([blockClass, className]),
		ref: localRef
	}, /* @__PURE__ */ React.createElement("div", {
		ref: refDisplayedItems,
		className: (0, import_classnames.default)([`${blockClass}__displayed-items`, { [`${blockClass}__displayed-items--right`]: rightAlign }])
	}, visibleItems.map(({ key, id, ...rest }) => /* @__PURE__ */ React.createElement(ActionBarItem, {
		id,
		...rest,
		key,
		ref: (node) => {
			itemRefHandler(id, node);
		}
	})), overflowMenuItems?.length > 0 && /* @__PURE__ */ React.createElement(ActionBarOverflowItems, {
		menuOptionsClass,
		overflowAriaLabel,
		overflowMenuRef: (node) => offsetRef.current = offsetRefHandler(node),
		overflowItems: overflowMenuItems,
		key: `overflow-menu-${internalId.current}`
	})));
});
ActionBar.displayName = componentName;
ActionBar.propTypes = {
	actions: PropTypes.arrayOf(PropTypes.shape({
		...prepareProps(Button.propTypes, [
			"kind",
			"size",
			"tooltipPosition",
			"tooltipAlignment"
		]),
		id: PropTypes.string,
		key: PropTypes.string.isRequired,
		iconDescription: PropTypes.string.isRequired,
		label: PropTypes.string.isRequired,
		renderIcon: Button.propTypes.renderIcon.isRequired,
		onClick: PropTypes.func
	})),
	className: PropTypes.string,
	maxVisible: PropTypes.number,
	menuOptionsClass: PropTypes.string,
	onWidthChange: PropTypes.func,
	overflowAriaLabel: PropTypes.string.isRequired,
	overflowMenuRef: PropTypes.oneOfType([PropTypes.shape({ current: PropTypes.elementType }), PropTypes.object]),
	rightAlign: PropTypes.bool
};

//#endregion
export { ActionBar };