/**
 * 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_devtools = require("../../global/js/utils/devtools.js");
const require_useResizeObserver = require("../../global/js/hooks/useResizeObserver.js");
const require_TagSetModal = require("./TagSetModal.js");
const require_TagSetOverflow = require("./TagSetOverflow.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/TagSet.tsx
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const componentName = "TagSet";
const blockClass = `${require_settings.pkg.prefix}--tag-set`;
const allTagsModalSearchThreshold = 10;
const defaults = {
	align: "start",
	measurementOffset: 0,
	overflowAlign: "bottom",
	overflowType: "default",
	onOverflowTagChange: () => {}
};
const TagSet = react.default.forwardRef(({ align = "start", allTagsModalTarget, className, maxVisible, multiline, overflowAutoAlign, overflowAlign = "bottom", overflowClassName, overflowType = "default", allTagsModalAriaLabel, allTagsModalTitle = "All tags", allTagsModalSearchLabel = "Search all tags", allTagsModalSearchPlaceholderText = "Search all tags", showAllTagsLabel = "View all tags", onOverflowClick, tags, containingElementRef, measurementOffset = defaults.measurementOffset, onOverflowTagChange = defaults.onOverflowTagChange, ...rest }, ref) => {
	const [displayCount, setDisplayCount] = (0, react.useState)(3);
	const [displayedTags, setDisplayedTags] = (0, react.useState)([]);
	const [hiddenSizingTags, setHiddenSizingTags] = (0, react.useState)([]);
	const [showAllModalOpen, setShowAllModalOpen] = (0, react.useState)(false);
	const localTagSetRef = (0, react.useRef)(null);
	const tagSetRef = ref || localTagSetRef;
	const sizingContainerRef = (0, react.useRef)(null);
	const displayedArea = (0, react.useRef)(null);
	const [sizingTags, setSizingTags] = (0, react.useState)([]);
	const overflowTag = (0, react.useRef)(null);
	const [maxVisibleCount, setMaxVisibleCount] = (0, react.useState)(0);
	const [popoverOpen, setPopoverOpen] = (0, react.useState)(false);
	const handleShowAllClick = () => {
		setShowAllModalOpen(true);
	};
	(0, react.useEffect)(() => {
		setMaxVisibleCount(maxVisible || tags?.length || 0);
	}, [maxVisible, tags]);
	(0, react.useEffect)(() => {
		const newSizingTags = [];
		setHiddenSizingTags(tags && tags.length > 0 ? tags.map(({ label, id, filter, onClose, ...other }, index) => {
			return /* @__PURE__ */ react.default.createElement("div", {
				key: index,
				className: `${blockClass}__sizing-tag`,
				ref: (el) => {
					if (el != null) newSizingTags[index] = el;
				}
			}, typeof onClose === "function" || filter ? /* @__PURE__ */ react.default.createElement(_carbon_react.DismissibleTag, {
				...other,
				"data-original-id": id,
				text: label
			}) : /* @__PURE__ */ react.default.createElement(_carbon_react.Tag, {
				...other,
				"data-original-id": id
			}, label));
		}) : []);
		setSizingTags(newSizingTags);
	}, [tags]);
	const handleTagOnClose = (0, react.useCallback)((onClose, index) => {
		onClose?.();
		if (index <= displayCount - 1) setPopoverOpen(false);
	}, [displayCount]);
	(0, react.useEffect)(() => {
		let size = "md";
		let newDisplayedTags = tags && tags.length > 0 ? tags.map(({ label, onClose, filter, ...other }, index) => {
			if (index == tags.length - 1 && other.size) size = other.size;
			if (typeof onClose === "function" || filter) return /* @__PURE__ */ react.default.createElement(_carbon_react.DismissibleTag, {
				...other,
				key: `displayed-tag-${index}`,
				onClose: () => handleTagOnClose(onClose, index),
				text: label
			});
			return /* @__PURE__ */ react.default.createElement(_carbon_react.Tag, {
				...other,
				key: `displayed-tag-${index}`
			}, label);
		}) : [];
		const newOverflowTags = newDisplayedTags.splice(displayCount, newDisplayedTags.length - displayCount);
		newDisplayedTags = newDisplayedTags.map((tag, index) => /* @__PURE__ */ react.default.createElement("div", {
			key: index,
			className: `${blockClass}__displayed-tag`
		}, tag));
		newDisplayedTags.push(/* @__PURE__ */ react.default.createElement(require_TagSetOverflow.TagSetOverflow, {
			allTagsModalSearchThreshold,
			overflowAutoAlign,
			className: overflowClassName,
			onShowAllClick: handleShowAllClick,
			overflowTags: newOverflowTags,
			overflowAlign,
			overflowType,
			showAllTagsLabel,
			size,
			key: "displayed-tag-overflow",
			ref: overflowTag,
			popoverOpen,
			onOverflowClick,
			setPopoverOpen
		}));
		onOverflowTagChange?.(newOverflowTags);
		setDisplayedTags(newDisplayedTags);
	}, [
		displayCount,
		overflowAlign,
		overflowClassName,
		overflowType,
		showAllTagsLabel,
		onOverflowClick,
		tags,
		onOverflowTagChange,
		popoverOpen,
		handleTagOnClose,
		overflowAutoAlign
	]);
	const checkFullyVisibleTags = (0, react.useCallback)(() => {
		if (multiline) return setDisplayCount(maxVisibleCount);
		let willFit = 0;
		if (sizingTags.length > 0) {
			const optionalContainingElement = typeof containingElementRef !== "function" && containingElementRef?.current;
			const measurementOffsetValue = typeof measurementOffset === "number" ? measurementOffset : 0;
			let spaceAvailable = optionalContainingElement ? optionalContainingElement.offsetWidth - measurementOffsetValue : tagSetRef?.["current"].offsetWidth;
			for (const i in sizingTags) {
				const tagWidth = sizingTags[i]?.offsetWidth || 0;
				if (spaceAvailable >= tagWidth) {
					spaceAvailable -= tagWidth;
					willFit += 1;
				} else break;
			}
			if (willFit < sizingTags.length && overflowTag.current) while (willFit > 0 && spaceAvailable < overflowTag.current.offsetWidth) {
				willFit -= 1;
				spaceAvailable += sizingTags[willFit].offsetWidth;
			}
		}
		if (willFit < 1) setDisplayCount(0);
		else setDisplayCount(maxVisibleCount ? Math.min(willFit, maxVisibleCount) : willFit);
	}, [
		maxVisibleCount,
		multiline,
		sizingTags,
		tagSetRef,
		measurementOffset,
		containingElementRef
	]);
	(0, react.useEffect)(() => {
		checkFullyVisibleTags();
	}, [
		checkFullyVisibleTags,
		maxVisibleCount,
		multiline,
		sizingTags
	]);
	/* istanbul ignore next */
	const handleResize = () => {
		/* istanbul ignore next */ checkFullyVisibleTags();
	};
	/* istanbul ignore next */
	const handleSizerTagsResize = () => {
		/* istanbul ignore next */ checkFullyVisibleTags();
	};
	const handleModalClose = () => {
		setShowAllModalOpen(false);
		const launcherButton = overflowTag.current?.querySelector("button");
		setTimeout(() => launcherButton?.focus(), 0);
	};
	require_useResizeObserver.useResizeObserver(sizingContainerRef, handleSizerTagsResize);
	require_useResizeObserver.useResizeObserver(containingElementRef ? containingElementRef : tagSetRef, handleResize);
	return /* @__PURE__ */ react.default.createElement("div", {
		...rest,
		className: (0, import_classnames.default)([blockClass, className]),
		ref: tagSetRef,
		...require_devtools.getDevtoolsProps(componentName)
	}, /* @__PURE__ */ react.default.createElement("div", { className: (0, import_classnames.default)([`${blockClass}__space`, `${blockClass}__space--align-${align}`]) }, /* @__PURE__ */ react.default.createElement("div", {
		className: `${blockClass}__tag-container ${blockClass}__tag-container--hidden`,
		"aria-hidden": true,
		ref: sizingContainerRef
	}, hiddenSizingTags), /* @__PURE__ */ react.default.createElement("div", {
		className: (0, import_classnames.default)([`${blockClass}__tag-container`, multiline && `${blockClass}__tag-container--multiline`]),
		ref: displayedArea
	}, displayedTags)), !onOverflowClick && /* @__PURE__ */ react.default.createElement(require_TagSetModal.TagSetModal, {
		allTags: tags,
		open: showAllModalOpen,
		title: allTagsModalTitle,
		onClose: handleModalClose,
		modalAriaLabel: allTagsModalAriaLabel,
		searchLabel: allTagsModalSearchLabel,
		searchPlaceholder: allTagsModalSearchPlaceholderText,
		portalTarget: allTagsModalTarget
	}));
});
const tagTypes = Object.keys({
	red: "Red",
	magenta: "Magenta",
	purple: "Purple",
	blue: "Blue",
	cyan: "Cyan",
	teal: "Teal",
	green: "Green",
	gray: "Gray",
	"cool-gray": "Cool-Gray",
	"warm-gray": "Warm-Gray",
	"high-contrast": "High-Contrast",
	outline: "Outline"
});
TagSet["types"] = tagTypes;
TagSet.propTypes = {
	/**
	* align the Tags displayed by the TagSet. Default start.
	*/
	align: prop_types.default.oneOf([
		"start",
		"center",
		"end"
	]),
	/**
	* aria label for all tags modal with hasScrollingContent
	*/
	allTagsModalAriaLabel: prop_types.default.string,
	/**
	* label text for the show all search.
	*/
	allTagsModalSearchLabel: prop_types.default.string,
	/**
	* placeholder text for the show all search.
	*/
	allTagsModalSearchPlaceholderText: prop_types.default.string,
	/**
	* portal target for the all tags modal
	*/
	allTagsModalTarget: prop_types.default.node,
	/**
	* title for the show all modal.
	*/
	allTagsModalTitle: prop_types.default.string,
	/**
	* className
	*/
	className: prop_types.default.string,
	/**
	* Optional ref for custom resize container to measure available space
	* Default will measure the available space of the TagSet container itself.
	*/
	/**@ts-ignore */
	containingElementRef: prop_types.default.object,
	/**
	* maximum visible tags
	*/
	maxVisible: prop_types.default.number,
	/**
	* Specify offset amount for measure available space, only used when `containingElementSelector`
	* is also provided
	*/
	measurementOffset: prop_types.default.number,
	/**
	* display tags in multiple lines
	*/
	multiline: prop_types.default.bool,
	/**
	* An optional click handler that overrides the default functionality of displaying all tags in a modal
	*/
	onOverflowClick: prop_types.default.func,
	/**
	* Handler to get overflow tags
	*/
	onOverflowTagChange: prop_types.default.func,
	/**
	* overflowAlign from the standard tooltip. Default center.
	*/
	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,
	/**
	* overflowClassName for the tooltip popup
	*/
	overflowClassName: prop_types.default.string,
	/**
	* Type of rendering displayed inside of the tag overflow component
	*/
	overflowType: prop_types.default.oneOf(["default", "tag"]),
	/**
	* label for the overflow show all tags link.
	*/
	showAllTagsLabel: prop_types.default.string,
	/**
	* The tags to be shown in the TagSet. Each tag is specified as an object
	* with properties: **label**\* (required) to supply the tag content, and
	* other properties will be passed to the Carbon Tag component, such as
	* **type**, **disabled**, **ref**, **className** , and any other Tag props.
	*
	* See https://react.carbondesignsystem.com/?path=/docs/components-tag--default
	*/
	tags: prop_types.default.arrayOf(prop_types.default.shape({
		..._carbon_react.Tag.propTypes,
		label: prop_types.default.string.isRequired,
		type: prop_types.default.oneOf(tagTypes)
	}))
};
TagSet.displayName = componentName;
//#endregion
exports.default = TagSet;
