/**
 * 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 { useWindowResize } from "../../global/js/hooks/useWindowResize.js";
import { usePreviousValue } from "../../global/js/hooks/usePreviousValue.js";
import { usePresence } from "../../global/js/hooks/usePresence.js";
import { getDevtoolsProps } from "../../global/js/utils/devtools.js";
import uuidv4 from "../../global/js/utils/uuidv4.js";
import TagSet from "../TagSet/TagSet.js";
import { debounce } from "../../global/js/utils/debounce.js";
import React, { useRef, useState } from "react";
import PropTypes from "prop-types";
import { Button, IconButton } from "@carbon/react";
import { ChevronDown } from "@carbon/react/icons";
//#region src/components/FilterSummary/FilterSummary.tsx
/**
* Copyright IBM Corp. 2022, 2024
*
* 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 blockClass = `${pkg.prefix}--filter-summary`;
const FilterSummary = React.forwardRef(({ className = "", clearFiltersText = "Clear filters", clearFilters, filters, renderLabel, overflowType = "default", clearButtonInline = true, ...rest }, ref) => {
	const filterSummaryId = `${blockClass}__${uuidv4()}`;
	const tagFilters = filters.map(({ key, value, ...rest }) => {
		return {
			...rest,
			type: "gray",
			label: renderLabel?.(key, value) ?? `${key}: ${value}`
		};
	});
	const filterSummaryClearButton = useRef(null);
	const viewAllButtonRef = useRef(null);
	const localRef = useRef(null) || ref;
	const [overflowCount, setOverflowCount] = useState(0);
	const [multiline, setMultiline] = useState(false);
	const previousState = usePreviousValue({ multiline }) ?? {};
	const handleViewAll = () => {
		if (overflowCount === 0) {
			setMultiline(false);
			return;
		}
		setMultiline((prev) => !prev);
	};
	const viewAllWidth = typeof viewAllButtonRef?.current?.offsetWidth === "undefined" ? 0 : overflowCount > 0 ? 48 : 0;
	const measurementOffset = (filterSummaryClearButton?.current?.offsetWidth || 0) + viewAllWidth;
	const { shouldRender } = usePresence(true, localRef, "filter-summary-enter");
	const renderTagSet = (type) => {
		return shouldRender ? /* @__PURE__ */ React.createElement("div", { key: type }, /* @__PURE__ */ React.createElement(TagSet, {
			allTagsModalSearchLabel: "Search all tags",
			allTagsModalSearchPlaceholderText: "Search all tags",
			allTagsModalTitle: "All tags",
			showAllTagsLabel: "View all tags",
			tags: tagFilters,
			overflowType,
			className: (0, import_classnames.default)({ [`${blockClass}__clear-button-inline`]: clearButtonInline }),
			containingElementRef: localRef,
			measurementOffset,
			onOverflowTagChange: (overflowTags) => setOverflowCount(overflowTags?.length),
			multiline
		})) : null;
	};
	useWindowResize(() => {
		const handleFilterSummaryResize = () => {
			if (multiline && localRef?.current?.offsetHeight && localRef?.current?.offsetHeight <= 50) setMultiline(false);
		};
		debounce(handleFilterSummaryResize, 500)();
	}, [previousState?.multiline, multiline]);
	return /* @__PURE__ */ React.createElement("div", {
		...getDevtoolsProps(componentName),
		id: filterSummaryId,
		...rest,
		ref: localRef,
		className: (0, import_classnames.default)([blockClass, className], { [`${blockClass}__expanded`]: multiline })
	}, !multiline && renderTagSet("single"), multiline && renderTagSet("multiline"), /* @__PURE__ */ React.createElement(Button, {
		kind: "ghost",
		size: "sm",
		onClick: clearFilters,
		ref: filterSummaryClearButton,
		className: `${blockClass}__clear-all-button`
	}, clearFiltersText), (overflowCount > 0 || multiline) && /* @__PURE__ */ React.createElement("div", { className: `${blockClass}__view-all--wrapper` }, /* @__PURE__ */ React.createElement(IconButton, {
		ref: viewAllButtonRef,
		kind: "ghost",
		label: "View all",
		className: `${blockClass}__view-all--trigger`,
		align: "left",
		onClick: handleViewAll,
		size: "sm"
	}, /* @__PURE__ */ React.createElement(ChevronDown, { className: (0, import_classnames.default)(`${blockClass}__view-all--chevron`, { [`${blockClass}__view-all--chevron-multiline`]: multiline }) }))));
});
const componentName = "FilterSummary";
FilterSummary.displayName = componentName;
FilterSummary.propTypes = {
	className: PropTypes.string,
	clearButtonInline: PropTypes.bool,
	clearFilters: PropTypes.func.isRequired,
	clearFiltersText: PropTypes.string,
	/**@ts-ignore */
	filters: PropTypes.arrayOf(PropTypes.object).isRequired,
	overflowType: PropTypes.oneOf(["default", "tag"]),
	renderLabel: PropTypes.func
};
//#endregion
export { FilterSummary as default };
