import { jsx as l, jsxs as g, Fragment as D } from "react/jsx-runtime";
import { useState as W, useRef as A, useEffect as _, useMemo as X } from "react";
import { Dropdown as v, Segment as Z, Label as K, Container as G, Icon as ee, Divider as te, Radio as ne, Checkbox as re } from "semantic-ui-react";
const F = "multi-select", h = "single-select", N = Number.MIN_SAFE_INTEGER, E = (p) => p == null ? null : String(p), de = (p) => {
  const {
    allLabel: U,
    noneLabel: j,
    placeholder: S,
    options: n,
    // group,
    current: L,
    onChange: i,
    useSingleColumn: k,
    enableTextSearch: C,
    filterType: c,
    // defaultValues,
    // showNoDataOption,
    // defaultValueCriteria,
    allNoneSameBehaviour: R,
    hiddenFilters: m,
    // autoApply,
    // taxonomy,
    // type,
    value: o,
    closeOnSelect: I = !0,
    noneFunction: V,
    alphabeticalSort: le,
    ascOrder: ie,
    ...B
  } = p, u = c === F, x = Array.isArray(o) ? o : L || [], d = (Array.isArray(x) ? x.some((e) => e === N) : !1) ? x.filter((e) => e !== N) : x, [a, Y] = W(""), M = (e, t) => {
    if (c === F) {
      const r = E(t), s = Array.isArray(d) ? [...d] : [];
      let T = s.some((w) => E(w) === r) ? s.filter((w) => E(w) !== r) : [...s, t];
      return !R && T.length === 0 && (T = [N]), i && i(e, T);
    }
    if (c === h)
      return i && i(e, t);
    I && f.current && f.current.close();
  }, q = () => {
    const e = (n || []).filter((t) => {
      var r;
      return C && a && a.trim().length > 0 && t.text ? (r = t.text) == null ? void 0 : r.toString().toLowerCase().includes(a.toLowerCase()) : !0;
    }).map((t) => t.value);
    u && i && i({}, e), !u && I && f.current && f.current.close();
  }, z = (e) => {
    if (V) {
      V(e);
      return;
    } else {
      if (!n) return [];
      const t = n.filter((r) => {
        var s;
        return C && a && a.trim().length > 0 && r.text ? (s = r.text) == null ? void 0 : s.toString().toLowerCase().includes(a == null ? void 0 : a.toString().toLowerCase()) : !0;
      });
      if (u && i) {
        const r = R ? t.map((s) => s.value) : [N];
        i({}, r);
      }
      !u && I && f.current && f.current.close();
    }
  }, O = (e) => {
    Y(e);
  }, y = A(!1);
  _(() => {
    if (!(!u || y.current) && n && n.length > 0)
      if (!(Array.isArray(d) ? d.length > 0 : !!d) && i) {
        y.current = !0;
        const t = n.map((r) => r.value);
        i({}, t);
      } else
        y.current = !0;
  }, [u, n]), _(() => {
    if (u && n && n.length > 0 && i) {
      const e = n.map((t) => t.value);
      i({}, e);
    }
  }, [u]);
  const H = X(() => {
    if (c == h) {
      const e = o ? n == null ? void 0 : n.filter((t) => t.value == o)[0] : null;
      return e ? `${S ? S + " " : ""}${e.text}` : "";
    } else {
      const e = d ? d.filter((r) => r == Number.MIN_SAFE_INTEGER ? !1 : m && m.length > 0 ? m.indexOf(r) == -1 : !0).length : 0, t = (n == null ? void 0 : n.filter((r) => m && m.length > 0 ? m.indexOf(r.id) == -1 : !0).length) || 0;
      return `${S} (${e}/${t})`;
    }
  }, [n, d, c]), f = A(null), $ = H, b = typeof $ == "string" ? $ : "", J = b && b.length > 0 ? b : S || "", P = A(void 0);
  _(() => {
    c === h && P.current === void 0 && o !== void 0 && (P.current = o);
  }, [c, o]);
  const Q = c === h ? o != null && o !== P.current : L && L.length > 0;
  return (
    // @ts-ignore
    /* @__PURE__ */ l(
      v,
      {
        ...B,
        ref: f,
        fluid: !0,
        text: J,
        scrolling: !1,
        button: !0,
        icon: "angle down",
        multiple: u,
        search: !0,
        floating: !1,
        value: o,
        closeOnChange: I,
        ...u ? { renderLabel: () => null } : {},
        className: `multiple ${Q ? "applied " : ""}`,
        children: /* @__PURE__ */ g(v.Menu, { children: [
          c != h && /* @__PURE__ */ g(D, { children: [
            /* @__PURE__ */ l(Z, { children: /* @__PURE__ */ g(v.Item, { children: [
              /* @__PURE__ */ l(K, { basic: !0, onClick: q, children: U }),
              " ",
              "|",
              " ",
              /* @__PURE__ */ l(K, { basic: !0, onClick: z, children: j })
            ] }) }),
            C && /* @__PURE__ */ g(D, { children: [
              /* @__PURE__ */ l(G, { children: /* @__PURE__ */ l(v.Item, { children: /* @__PURE__ */ l("div", { className: "ui action input", children: /* @__PURE__ */ g("div", { className: "ui input", children: [
                /* @__PURE__ */ l(
                  "input",
                  {
                    className: "filter-search",
                    value: a,
                    placeholder: "Search...",
                    onChange: (e) => {
                      e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation(), O(e.target.value);
                    },
                    onKeyDown: (e) => {
                      e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation();
                    },
                    onKeyUp: (e) => {
                      e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation();
                    },
                    onKeyPress: (e) => {
                      e && e.nativeEvent && e.nativeEvent.stopImmediatePropagation && e.nativeEvent.stopImmediatePropagation();
                    },
                    onMouseDown: (e) => {
                      e.stopPropagation();
                    },
                    onClick: (e) => {
                      e.stopPropagation();
                    },
                    onFocus: (e) => {
                      e.stopPropagation();
                    },
                    type: "text",
                    autoComplete: "off"
                  }
                ),
                /* @__PURE__ */ l(
                  ee,
                  {
                    name: "remove",
                    link: !0,
                    className: "clear-icon ignore",
                    onClick: (e) => {
                      O("");
                    }
                  }
                )
              ] }) }) }) }),
              /* @__PURE__ */ l(te, {})
            ] })
          ] }),
          /* @__PURE__ */ l("br", {}),
          /* @__PURE__ */ l(G, { className: k ? "dropdown-single-column" : "", children: n == null ? void 0 : n.filter((e) => {
            var t;
            return C && a && a.trim().length > 0 && e.text ? (t = e.text) == null ? void 0 : t.toString().toLowerCase().includes(a.toLowerCase()) : !0;
          }).map(({ value: e, text: t }, r) => /* @__PURE__ */ g(
            v.Item,
            {
              className: k ? "dropdown-item-single-column" : "",
              children: [
                c === h && /* @__PURE__ */ l(
                  ne,
                  {
                    checked: o == e,
                    onChange: (s) => {
                      M(s, e);
                    },
                    label: t
                  }
                ),
                c === F && /* @__PURE__ */ l(
                  re,
                  {
                    checked: d && d.some(
                      (s) => E(s) === E(e)
                    ),
                    onChange: () => M(null, e),
                    label: t
                  }
                )
              ]
            },
            r
          )) })
        ] })
      }
    )
  );
};
export {
  de as default
};
