import {
  Filter
} from "./chunk-LAIAW5BT.js";
import {
  Select_default
} from "./chunk-DC6PYU2D.js";
import {
  Spacing
} from "./chunk-E5DUKMCU.js";
import {
  Button
} from "./chunk-SIPHI6JZ.js";
import {
  z
} from "./chunk-55OQPFLF.js";
import {
  __spreadProps,
  __spreadValues
} from "./chunk-W7FAWX6T.js";

// src/components/DataManagerTable/EnumFilter.tsx
import styled from "@emotion/styled";
import { ensureArray } from "@lightsparkdev/core";
import { Fragment, jsx, jsxs } from "@emotion/react/jsx-runtime";
var isEnumFilterState = (state) => state.type === "enum" /* ENUM */;
var getDefaultEnumFilterState = () => ({
  type: "enum" /* ENUM */,
  value: "",
  isApplied: false
});
var EnumFilter = ({
  updateFilterState,
  state,
  options,
  placeholder,
  label,
  isMulti
}) => {
  var _a;
  const appliedValueButtons = (_a = state.appliedValues) == null ? void 0 : _a.map((value) => {
    return /* @__PURE__ */ jsx(
      Button,
      {
        kind: "primary",
        size: "ExtraSmall",
        text: value,
        icon: { name: "Close" },
        iconSide: "right",
        onClick: () => {
          var _a2;
          updateFilterState(__spreadProps(__spreadValues({}, state), {
            appliedValues: (_a2 = state.appliedValues) == null ? void 0 : _a2.filter(
              (appliedValue) => appliedValue !== value
            )
          }));
        }
      },
      value
    );
  });
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(Filter, { label, children: /* @__PURE__ */ jsx(
      Select_default,
      {
        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(__spreadProps(__spreadValues({}, state), {
              value: option.label,
              isApplied: true,
              appliedValues: updatedAppliedValues
            }));
          }
        }
      }
    ) }),
    !!(appliedValueButtons == null ? void 0 : appliedValueButtons.length) ? /* @__PURE__ */ jsx(AppliedButtonsContainer, { children: appliedValueButtons }) : null
  ] });
};
var AppliedButtonsContainer = styled.div`
  margin-top: ${Spacing.px.sm};
  display: flex;
  gap: ${Spacing.px.xs};
  flex-wrap: wrap;
`;

export {
  isEnumFilterState,
  getDefaultEnumFilterState,
  EnumFilter
};
