/**
 * 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_useClickOutside = require("../../global/js/hooks/useClickOutside.js");
let react = require("react");
react = require_runtime.__toESM(react);
let prop_types = require("prop-types");
prop_types = require_runtime.__toESM(prop_types);
let _carbon_react = require("@carbon/react");
//#region src/components/TagSet/TagSetOverflow.tsx
/**@ts-ignore */
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const componentName = "TagSetOverflow";
const blockClass = `${require_settings.pkg.prefix}--tag-set-overflow`;
const defaults = { allTagsModalSearchThreshold: 10 };
const TagSetOverflow = react.default.forwardRef(({ allTagsModalSearchThreshold = defaults.allTagsModalSearchThreshold, onOverflowClick, className, onShowAllClick, overflowAlign = "bottom", overflowAutoAlign, overflowTags, overflowType, showAllTagsLabel, popoverOpen = false, setPopoverOpen, size, ...rest }, ref) => {
	const localRef = (0, react.useRef)(null);
	const overflowTagContent = (0, react.useRef)(null);
	require_useClickOutside.useClickOutside(ref || localRef, () => {
		if (popoverOpen) setPopoverOpen(false);
	});
	const handleShowAllTagsClick = (ev) => {
		ev.stopPropagation();
		ev.preventDefault();
		setPopoverOpen(false);
		onShowAllClick();
	};
	const handleEscKeyPress = (event) => {
		const { key } = event;
		if (key === "Escape") setPopoverOpen(false);
	};
	const handleOverflowClick = () => {
		if (onOverflowClick) onOverflowClick(overflowTags);
		else setPopoverOpen(!popoverOpen);
	};
	if (onOverflowClick) return /* @__PURE__ */ react.default.createElement("span", {
		...rest,
		"aria-hidden": overflowTags.length === 0,
		className: (0, import_classnames.default)(`${blockClass}`, { [`${blockClass}--hidden`]: overflowTags.length === 0 }),
		ref: ref || localRef
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.OperationalTag, {
		onClick: () => handleOverflowClick(),
		className: `${blockClass}__popover-trigger`,
		size,
		text: `+${overflowTags.length}`
	}));
	return /* @__PURE__ */ react.default.createElement("span", {
		...rest,
		"aria-hidden": overflowTags.length === 0,
		className: (0, import_classnames.default)(`${blockClass}`, { [`${blockClass}--hidden`]: overflowTags.length === 0 }),
		ref: ref || localRef
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.Popover, {
		align: overflowAlign,
		className: (0, import_classnames.default)(className, `${blockClass}__tagset-popover`),
		dropShadow: true,
		highContrast: true,
		onKeyDown: handleEscKeyPress,
		open: popoverOpen,
		autoAlign: overflowAutoAlign
	}, /* @__PURE__ */ react.default.createElement(_carbon_react.OperationalTag, {
		onClick: () => setPopoverOpen(!popoverOpen),
		className: (0, import_classnames.default)(`${blockClass}__popover-trigger`),
		size,
		text: `+${overflowTags.length}`,
		"aria-expanded": popoverOpen,
		"aria-controls": `${require_settings.pkg.prefix}-overflow-content`
	}), /* @__PURE__ */ react.default.createElement(_carbon_react.PopoverContent, {
		id: `${require_settings.pkg.prefix}-overflow-content`,
		"aria-hidden": !popoverOpen
	}, /* @__PURE__ */ react.default.createElement("div", {
		ref: overflowTagContent,
		className: `${blockClass}__content`
	}, /* @__PURE__ */ react.default.createElement("ul", { className: `${blockClass}__tag-list` }, overflowTags.filter((_, index) => overflowTags.length > allTagsModalSearchThreshold ? index < allTagsModalSearchThreshold : index <= allTagsModalSearchThreshold).map((tag, index) => {
		const tagProps = {};
		if (overflowType === "tag") tagProps.type = "high-contrast";
		if (overflowType === "default") tagProps.filter = false;
		if (react.default.isValidElement(tag)) return /* @__PURE__ */ react.default.createElement("li", {
			className: (0, import_classnames.default)(`${blockClass}__tag-item`, {
				[`${blockClass}__tag-item--default`]: overflowType === "default",
				[`${blockClass}__tag-item--tag`]: overflowType === "tag"
			}),
			key: index
		}, react.default.cloneElement(tag, tagProps));
	})), overflowTags.length > allTagsModalSearchThreshold && /* @__PURE__ */ react.default.createElement(_carbon_react.Link, {
		className: `${blockClass}__show-all-tags-link`,
		href: "",
		onClick: handleShowAllTagsClick,
		role: "button"
	}, showAllTagsLabel)))));
});
TagSetOverflow.displayName = componentName;
TagSetOverflow.propTypes = {
	/**
	* count of overflowTags over which a modal is offered
	*/
	allTagsModalSearchThreshold: prop_types.default.number,
	/**
	* className
	*/
	className: prop_types.default.string,
	/**
	* An optional click handler that overrides the default functionality of displaying all tags in a modal
	*/
	onOverflowClick: prop_types.default.func,
	/**
	* function to execute on clicking show all
	*/
	onShowAllClick: prop_types.default.func.isRequired,
	/**
	* overflowAlign from the standard tooltip
	*/
	overflowAlign: prop_types.default.oneOf([
		"top",
		"top-left",
		"top-right",
		"bottom",
		"bottom-left",
		"bottom-right",
		"left",
		"left-bottom",
		"left-top",
		"right",
		"right-bottom",
		"right-top"
	]),
	/**
	* Will auto-align the popover on first render if it is not visible. This prop is currently experimental and is subject to future changes.
	*/
	overflowAutoAlign: prop_types.default.bool,
	/**
	* tags shown in overflow
	*/
	/**@ts-ignore */
	overflowTags: prop_types.default.arrayOf(prop_types.default.object).isRequired,
	/**
	* Type of rendering displayed inside of the tag overflow component
	*/
	overflowType: prop_types.default.oneOf(["default", "tag"]),
	/**
	* Open state of the popover
	*/
	popoverOpen: prop_types.default.bool,
	/**
	* Setter function for the popoverOpen state value
	*/
	setPopoverOpen: prop_types.default.func.isRequired,
	/**
	* label for the overflow show all tags link
	*/
	showAllTagsLabel: prop_types.default.string,
	/**
	* Size of the overflow tag
	*/
	size: prop_types.default.string
};
//#endregion
exports.TagSetOverflow = TagSetOverflow;
