import { z } from "../../styles/z-index.js";
import { Button } from "../Button.js";
import { Select } from "../Select.js";
import { Filter } from "./Filter.js";
import "./filters.js";
import { AppliedButtonsContainer } from "./AppliedButtonsContainer.js";
import { Fragment, jsx, jsxs } from "@emotion/react/jsx-runtime";
import { ensureArray } from "@lightsparkdev/core";
//#region src/components/DataManagerTable/EnumFilter.tsx
const isEnumFilterState = (state) => state.type === "enum";
const getDefaultEnumFilterState = () => ({
	type: "enum",
	value: "",
	isApplied: false
});
const EnumFilter = ({ updateFilterState, state, options, placeholder, label, isMulti }) => {
	const appliedValueButtons = state.appliedValues?.map((value) => {
		return /* @__PURE__ */ jsx(Button, {
			kind: "primary",
			size: "ExtraSmall",
			text: value,
			icon: { name: "Close" },
			iconSide: "right",
			onClick: () => {
				updateFilterState({
					...state,
					appliedValues: state.appliedValues?.filter((appliedValue) => appliedValue !== value)
				});
			}
		}, value);
	});
	return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Filter, {
		label,
		children: /* @__PURE__ */ jsx(Select, {
			zIndex: z.dropdown + 1,
			placeholder: "Select value",
			options,
			onChange: (option) => {
				if (option) {
					const optionValues = ensureArray(option.value);
					let updatedAppliedValues = [];
					if (isMulti) updatedAppliedValues = state.appliedValues ? [...state.appliedValues.filter((appliedValue) => appliedValue !== option.value), ...optionValues] : [...optionValues];
					else updatedAppliedValues = [...optionValues];
					updateFilterState({
						...state,
						value: option.label,
						isApplied: true,
						appliedValues: updatedAppliedValues
					});
				}
			}
		})
	}), !!appliedValueButtons?.length ? /* @__PURE__ */ jsx(AppliedButtonsContainer, { children: appliedValueButtons }) : null] });
};
//#endregion
export { EnumFilter, getDefaultEnumFilterState, isEnumFilterState };
