/**
 * 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 { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import { useResizeObserver } from "../../global/js/hooks/useResizeObserver.js";
import { TagSetModal } from "./TagSetModal.js";
import { TagSetOverflow } from "./TagSetOverflow.js";
import React, { useCallback, useEffect, useRef, useState } from "react";
import PropTypes from "prop-types";
import { DismissibleTag, Tag } from "@carbon/react";

//#region src/components/TagSet/TagSet.tsx
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const componentName = "TagSet";
const blockClass = `${pkg.prefix}--tag-set`;
const allTagsModalSearchThreshold = 10;
const defaults = {
	align: "start",
	measurementOffset: 0,
	overflowAlign: "bottom",
	overflowType: "default",
	onOverflowTagChange: () => {}
};
const TagSet = React.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] = useState(3);
	const [displayedTags, setDisplayedTags] = useState([]);
	const [hiddenSizingTags, setHiddenSizingTags] = useState([]);
	const [showAllModalOpen, setShowAllModalOpen] = useState(false);
	const localTagSetRef = useRef(null);
	const tagSetRef = ref || localTagSetRef;
	const sizingContainerRef = useRef(null);
	const displayedArea = useRef(null);
	const [sizingTags, setSizingTags] = useState([]);
	const overflowTag = useRef(null);
	const [maxVisibleCount, setMaxVisibleCount] = useState(0);
	const [popoverOpen, setPopoverOpen] = useState(false);
	const handleShowAllClick = () => {
		setShowAllModalOpen(true);
	};
	useEffect(() => {
		setMaxVisibleCount(maxVisible || tags?.length || 0);
	}, [maxVisible, tags]);
	useEffect(() => {
		const newSizingTags = [];
		setHiddenSizingTags(tags && tags.length > 0 ? tags.map(({ label, id, filter, onClose, ...other }, index) => {
			return /* @__PURE__ */ React.createElement("div", {
				key: index,
				className: `${blockClass}__sizing-tag`,
				ref: (el) => {
					if (el != null) newSizingTags[index] = el;
				}
			}, typeof onClose === "function" || filter ? /* @__PURE__ */ React.createElement(DismissibleTag, {
				...other,
				"data-original-id": id,
				text: label
			}) : /* @__PURE__ */ React.createElement(Tag, {
				...other,
				"data-original-id": id
			}, label));
		}) : []);
		setSizingTags(newSizingTags);
	}, [tags]);
	const handleTagOnClose = useCallback((onClose, index) => {
		onClose?.();
		if (index <= displayCount - 1) setPopoverOpen(false);
	}, [displayCount]);
	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.createElement(DismissibleTag, {
				...other,
				key: `displayed-tag-${index}`,
				onClose: () => handleTagOnClose(onClose, index),
				text: label
			});
			return /* @__PURE__ */ React.createElement(Tag, {
				...other,
				key: `displayed-tag-${index}`
			}, label);
		}) : [];
		const newOverflowTags = newDisplayedTags.splice(displayCount, newDisplayedTags.length - displayCount);
		newDisplayedTags = newDisplayedTags.map((tag, index) => /* @__PURE__ */ React.createElement("div", {
			key: index,
			className: `${blockClass}__displayed-tag`
		}, tag));
		newDisplayedTags.push(/* @__PURE__ */ React.createElement(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 = 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
	]);
	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);
	};
	useResizeObserver(sizingContainerRef, handleSizerTagsResize);
	useResizeObserver(containingElementRef ? containingElementRef : tagSetRef, handleResize);
	return /* @__PURE__ */ React.createElement("div", {
		...rest,
		className: (0, import_classnames.default)([blockClass, className]),
		ref: tagSetRef,
		...getDevtoolsProps(componentName)
	}, /* @__PURE__ */ React.createElement("div", { className: (0, import_classnames.default)([`${blockClass}__space`, `${blockClass}__space--align-${align}`]) }, /* @__PURE__ */ React.createElement("div", {
		className: `${blockClass}__tag-container ${blockClass}__tag-container--hidden`,
		"aria-hidden": true,
		ref: sizingContainerRef
	}, hiddenSizingTags), /* @__PURE__ */ React.createElement("div", {
		className: (0, import_classnames.default)([`${blockClass}__tag-container`, multiline && `${blockClass}__tag-container--multiline`]),
		ref: displayedArea
	}, displayedTags)), !onOverflowClick && /* @__PURE__ */ React.createElement(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: PropTypes.oneOf([
		"start",
		"center",
		"end"
	]),
	allTagsModalAriaLabel: PropTypes.string,
	allTagsModalSearchLabel: PropTypes.string,
	allTagsModalSearchPlaceholderText: PropTypes.string,
	allTagsModalTarget: PropTypes.node,
	allTagsModalTitle: PropTypes.string,
	className: PropTypes.string,
	containingElementRef: PropTypes.object,
	maxVisible: PropTypes.number,
	measurementOffset: PropTypes.number,
	multiline: PropTypes.bool,
	onOverflowClick: PropTypes.func,
	onOverflowTagChange: PropTypes.func,
	overflowAlign: PropTypes.oneOf([
		"top",
		"top-left",
		"top-right",
		"bottom",
		"bottom-left",
		"bottom-right",
		"left",
		"left-bottom",
		"left-top",
		"right",
		"right-bottom",
		"right-top"
	]),
	overflowAutoAlign: PropTypes.bool,
	overflowClassName: PropTypes.string,
	overflowType: PropTypes.oneOf(["default", "tag"]),
	showAllTagsLabel: PropTypes.string,
	tags: PropTypes.arrayOf(PropTypes.shape({
		...Tag.propTypes,
		label: PropTypes.string.isRequired,
		type: PropTypes.oneOf(tagTypes)
	}))
};
TagSet.displayName = componentName;

//#endregion
export { TagSet as default };