/**
 * 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 { blockClass } from "../PageHeaderUtils.js";
import { createOverflowHandler } from "./overflowHandler.js";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { Popover, PopoverContent } from "@carbon/react";

//#region src/components/PageHeader/next/PageHeaderTagOverflow.tsx
/**
* Copyright IBM Corp. 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.
*/
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const PageHeaderTagOverflow = React.forwardRef(({ renderOverflowTag, renderPopoverContent, children }, ref) => {
	const [openPopover, setOpenPopover] = useState(false);
	const [hiddenTags, setHiddenTags] = useState([]);
	const localRef = useRef(null);
	const tagsContainerRef = ref || localRef;
	useEffect(() => {
		const handleResize = () => {
			setOpenPopover(false);
		};
		window.addEventListener("resize", handleResize);
		return () => {
			window.removeEventListener("resize", handleResize);
		};
	}, []);
	const handleOverflowClick = useCallback((event) => {
		event.stopPropagation();
		setOpenPopover((prev) => !prev);
	}, []);
	useEffect(() => {
		createOverflowHandler({
			container: tagsContainerRef.current,
			onChange: (_, hidden) => {
				setHiddenTags(hidden);
			}
		});
	}, []);
	return /* @__PURE__ */ React.createElement("div", {
		ref: tagsContainerRef,
		className: (0, import_classnames.default)(`${pkg.prefix}--page-header--tag-overflow-container`, { [`${pkg.prefix}--page-header--tag-overflow-container__has-no-hidden-items`]: !hiddenTags.length })
	}, children, /* @__PURE__ */ React.createElement(Popover, {
		open: openPopover,
		onRequestClose: () => setOpenPopover(false),
		"data-fixed": true,
		className: (0, import_classnames.default)(`${pkg.prefix}--page-header--tag-overflow-popover`, { [`${pkg.prefix}--page-header--tag-overflow-popover__hidden`]: !hiddenTags.length })
	}, renderOverflowTag?.(hiddenTags, handleOverflowClick, openPopover), /* @__PURE__ */ React.createElement(PopoverContent, null, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__tags-popover-list` }, renderPopoverContent?.(hiddenTags)))));
});
PageHeaderTagOverflow.displayName = "PageHeaderTagOverflow";

//#endregion
export { PageHeaderTagOverflow };