/**
 * 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 React, { useState } from "react";
import PropTypes from "prop-types";
import { Button, ButtonSet, DismissibleTag, Dropdown, Layer, Search } from "@carbon/react";
import { Filter } from "@carbon/react/icons";
//#region src/components/AddSelect/AddSelectFilter.jsx
var import_classnames = /* @__PURE__ */ __toESM(require_classnames());
const blockClass = `${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] = useState({});
	const [open, setOpen] = 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.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}-search` }, /* @__PURE__ */ React.createElement(Search, {
		id: "add-select-global-search-filter",
		labelText: inputLabel,
		placeholder: inputPlaceholder,
		value: searchTerm,
		onChange: searchHandler
	}), showFilter && /* @__PURE__ */ React.createElement(Button, {
		renderIcon: (props) => /* @__PURE__ */ React.createElement(Filter, {
			size: 16,
			...props
		}),
		hasIconOnly: true,
		kind: "ghost",
		onClick: () => setOpen(!open),
		iconDescription,
		className: filterBtnClassnames,
		size: "md",
		tooltipPosition: "left"
	}), open && /* @__PURE__ */ React.createElement(Layer, {
		className: blockClass,
		level: 1
	}, /* @__PURE__ */ React.createElement("div", { className: `${blockClass}-content` }, /* @__PURE__ */ React.createElement("p", null, filtersLabel), /* @__PURE__ */ React.createElement("div", { className: `${blockClass}-opts` }, filterOpts.map((filterOpts) => /* @__PURE__ */ React.createElement(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.createElement(ButtonSet, { className: `${blockClass}-button-set` }, /* @__PURE__ */ React.createElement(Button, {
		kind: "secondary",
		onClick: resetFilters,
		className: `${blockClass}-button`,
		disabled: !dirtyInput
	}, secondaryButtonText), /* @__PURE__ */ React.createElement(Button, {
		kind: "primary",
		onClick: applyFilters,
		className: `${blockClass}-button`,
		disabled: !dirtyInput && !hasFiltersApplied
	}, primaryButtonText)))), hasFiltersApplied && /* @__PURE__ */ React.createElement("div", { className: `${blockClass}-applied` }, Object.keys(appliedFilters).map((filterType) => /* @__PURE__ */ React.createElement(DismissibleTag, {
		key: filterType,
		text: `${filterType}: ${appliedFilters[filterType]}`,
		type: "gray",
		size: "sm",
		onClose: () => removeTag(filterType)
	})), /* @__PURE__ */ React.createElement(Button, {
		kind: "ghost",
		size: "sm",
		onClick: clearFilters
	}, clearFiltersText)));
};
AddSelectFilter.propTypes = {
	appliedFilters: PropTypes.object,
	clearFiltersText: PropTypes.string,
	filterOpts: PropTypes.array,
	filtersLabel: PropTypes.string,
	handleFilter: PropTypes.func,
	handleSearch: PropTypes.func,
	hasFiltersApplied: PropTypes.bool,
	iconDescription: PropTypes.string,
	inputLabel: PropTypes.string.isRequired,
	inputPlaceholder: PropTypes.string,
	multi: PropTypes.bool,
	placeholder: PropTypes.string,
	primaryButtonText: PropTypes.string,
	searchTerm: PropTypes.string,
	secondaryButtonText: PropTypes.string
};
AddSelectFilter.displayName = componentName;
//#endregion
export { AddSelectFilter };
