/**
 * 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");
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/AddSelect/AddSelectFilter.jsx
var import_classnames = /* @__PURE__ */ require_runtime.__toESM(require_index.default);
const blockClass = `${require_settings.pkg.prefix}--add-select__global-filter`;
const componentName = "AddSelectFilter";
let AddSelectFilter = ({ appliedFilters, clearFiltersText, filterOpts, filtersLabel, handleFilter, handleSearch, hasFiltersApplied, iconDescription, inputLabel, inputPlaceholder, multi, placeholder, primaryButtonText, searchTerm, secondaryButtonText }) => {
	const [filters, setFilters] = (0, react.useState)({});
	const [open, setOpen] = (0, react.useState)(false);
	const searchHandler = (e) => {
		handleSearch(e.target.value);
	};
	const onchangeHandler = ({ selectedItem }, id) => {
		setFilters({
			...filters,
			[id]: selectedItem
		});
	};
	/**
	* this component needs to manage it's own internal state of filters before they're applied
	* setFilters manages the local filter state
	* applyFilters adds the filter state to the parent
	* resetFilters resets the local state
	* clearFilters resets both
	*/
	const applyFilters = () => {
		setOpen(false);
		handleFilter(filters);
	};
	const resetFilters = () => {
		setFilters({});
	};
	const clearFilters = () => {
		resetFilters();
		handleFilter({});
	};
	const removeTag = (key) => {
		const newFilters = { ...filters };
		delete newFilters[key];
		setFilters(newFilters);
		handleFilter(newFilters);
	};
	const getSelectedItem = (id) => {
		return filters[id] || "";
	};
	const showFilter = multi && filterOpts?.length > 0;
	const filterBtnClassnames = (0, import_classnames.default)(`${blockClass}-toggle`, { [`${blockClass}-toggle--open`]: open });
	const dirtyInput = Object.keys(filters).length > 0;
	return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}-search` }, /* @__PURE__ */ react.default.createElement(_carbon_react.Search, {
		id: "add-select-global-search-filter",
		labelText: inputLabel,
		placeholder: inputPlaceholder,
		value: searchTerm,
		onChange: searchHandler
	}), showFilter && /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
		renderIcon: (props) => /* @__PURE__ */ react.default.createElement(_carbon_react_icons.Filter, {
			size: 16,
			...props
		}),
		hasIconOnly: true,
		kind: "ghost",
		onClick: () => setOpen(!open),
		iconDescription,
		className: filterBtnClassnames,
		size: "md",
		tooltipPosition: "left"
	}), open && /* @__PURE__ */ react.default.createElement(_carbon_react.Layer, {
		className: blockClass,
		level: 1
	}, /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}-content` }, /* @__PURE__ */ react.default.createElement("p", null, filtersLabel), /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}-opts` }, filterOpts.map((filterOpts) => /* @__PURE__ */ react.default.createElement(_carbon_react.Dropdown, {
		key: filterOpts.id,
		id: filterOpts.id,
		titleText: filterOpts.label,
		items: filterOpts.opts,
		onChange: (value) => onchangeHandler(value, filterOpts.id),
		selectedItem: getSelectedItem(filterOpts.id),
		label: placeholder
	})))), /* @__PURE__ */ react.default.createElement(_carbon_react.ButtonSet, { className: `${blockClass}-button-set` }, /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
		kind: "secondary",
		onClick: resetFilters,
		className: `${blockClass}-button`,
		disabled: !dirtyInput
	}, secondaryButtonText), /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
		kind: "primary",
		onClick: applyFilters,
		className: `${blockClass}-button`,
		disabled: !dirtyInput && !hasFiltersApplied
	}, primaryButtonText)))), hasFiltersApplied && /* @__PURE__ */ react.default.createElement("div", { className: `${blockClass}-applied` }, Object.keys(appliedFilters).map((filterType) => /* @__PURE__ */ react.default.createElement(_carbon_react.DismissibleTag, {
		key: filterType,
		text: `${filterType}: ${appliedFilters[filterType]}`,
		type: "gray",
		size: "sm",
		onClose: () => removeTag(filterType)
	})), /* @__PURE__ */ react.default.createElement(_carbon_react.Button, {
		kind: "ghost",
		size: "sm",
		onClick: clearFilters
	}, clearFiltersText)));
};
AddSelectFilter.propTypes = {
	appliedFilters: prop_types.default.object,
	clearFiltersText: prop_types.default.string,
	filterOpts: prop_types.default.array,
	filtersLabel: prop_types.default.string,
	handleFilter: prop_types.default.func,
	handleSearch: prop_types.default.func,
	hasFiltersApplied: prop_types.default.bool,
	iconDescription: prop_types.default.string,
	inputLabel: prop_types.default.string.isRequired,
	inputPlaceholder: prop_types.default.string,
	multi: prop_types.default.bool,
	placeholder: prop_types.default.string,
	primaryButtonText: prop_types.default.string,
	searchTerm: prop_types.default.string,
	secondaryButtonText: prop_types.default.string
};
AddSelectFilter.displayName = componentName;
//#endregion
exports.AddSelectFilter = AddSelectFilter;
