/**
 * 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_useWindowResize = require('../../global/js/hooks/useWindowResize.js');
const require_usePreviousValue = require('../../global/js/hooks/usePreviousValue.js');
const require_usePresence = require('../../global/js/hooks/usePresence.js');
const require_devtools = require('../../global/js/utils/devtools.js');
const require_uuidv4 = require('../../global/js/utils/uuidv4.js');
const require_TagSet = require('../TagSet/TagSet.js');
const require_debounce = require('../../global/js/utils/debounce.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");
let _carbon_react_icons = require("@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__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--filter-summary`;
const FilterSummary = react.default.forwardRef(({ className = "", clearFiltersText = "Clear filters", clearFilters, filters, renderLabel, overflowType = "default", clearButtonInline = true, ...rest }, ref) => {
	const filterSummaryId = `${blockClass}__${require_uuidv4.default()}`;
	const tagFilters = filters.map(({ key, value, ...rest }) => {
		return {
			...rest,
			type: "gray",
			label: renderLabel?.(key, value) ?? `${key}: ${value}`
		};
	});
	const filterSummaryClearButton = (0, react.useRef)(null);
	const viewAllButtonRef = (0, react.useRef)(null);
	const localRef = (0, react.useRef)(null) || ref;
	const [overflowCount, setOverflowCount] = (0, react.useState)(0);
	const [multiline, setMultiline] = (0, react.useState)(false);
	const previousState = require_usePreviousValue.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 } = require_usePresence.usePresence(true, localRef, "filter-summary-enter");
	const renderTagSet = (type) => {
		return shouldRender ? /* @__PURE__ */ react.default.createElement("div", { key: type }, /* @__PURE__ */ react.default.createElement(require_TagSet.default, {
			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;
	};
	require_useWindowResize.useWindowResize(() => {
		const handleFilterSummaryResize = () => {
			if (multiline && localRef?.current?.offsetHeight && localRef?.current?.offsetHeight <= 50) setMultiline(false);
		};
		require_debounce.debounce(handleFilterSummaryResize, 500)();
	}, [previousState?.multiline, multiline]);
	return /* @__PURE__ */ react.default.createElement("div", {
		...require_devtools.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.default.createElement(_carbon_react.Button, {
		kind: "ghost",
		size: "sm",
		onClick: clearFilters,
		ref: filterSummaryClearButton,
		className: `${blockClass}__clear-all-button`
	}, clearFiltersText), (overflowCount > 0 || multiline) && /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}__view-all--wrapper` }, /* @__PURE__ */ react.default.createElement(_carbon_react.IconButton, {
		ref: viewAllButtonRef,
		kind: "ghost",
		label: "View all",
		className: `${blockClass}__view-all--trigger`,
		align: "left",
		onClick: handleViewAll,
		size: "sm"
	}, /* @__PURE__ */ react.default.createElement(_carbon_react_icons.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: prop_types.default.string,
	clearButtonInline: prop_types.default.bool,
	clearFilters: prop_types.default.func.isRequired,
	clearFiltersText: prop_types.default.string,
	filters: prop_types.default.arrayOf(prop_types.default.object).isRequired,
	overflowType: prop_types.default.oneOf(["default", "tag"]),
	renderLabel: prop_types.default.func
};

//#endregion
exports.default = FilterSummary;