import { jsxs as O, jsx as b } from "react/jsx-runtime";
import { useState as p, useEffect as f } from "react";
import { noop as W, compact as h, isEmpty as d } from "es-toolkit/compat";
import { isEqual as v } from "es-toolkit/predicate";
import N from "../../utils/classNames.js";
import { AutoSuggest as _ } from "../autosuggest/AutoSuggest.js";
import k from "./CustomSuggestionItem.js";
import z from "./TagManagerItemList.js";
const B = (e, t) => ({
  customSuggestion: /* @__PURE__ */ b(k, { value: e, placeholder: t }),
  label: e,
  disabled: d(e)
}), G = (e) => ({
  header: !0,
  label: e
}), R = (e) => (delete e.toAdd, delete e.toRemove, e), H = (e, t) => !!e.find((s) => s.label === t.label), J = (e) => {
  const t = e.customSuggestion ? { label: e.label } : e;
  return t.toAdd = !0, t;
}, K = (e, t, s) => (l, u) => {
  const g = u.map(R).filter((S) => S.label), i = typeof s == "string" && !d(s);
  return h([
    e && B(l, t),
    e && !d(g) && i && G(s),
    ...g
  ]);
}, Q = (e, t, s = "") => e.filter((l) => {
  const u = l.label;
  if (!u)
    return !1;
  const g = u.toLowerCase(), i = s.toLowerCase();
  return g.includes(i) && !t.includes(u) && !l.header && !l.customSuggestion;
}), se = (e) => {
  const {
    tagList: t,
    tagSuggestions: s,
    placeholder: l,
    className: u,
    customTagPlaceholder: g,
    dropdownSeparatorText: i,
    onTagListChange: S = W,
    useCustomTags: I = !0,
    noItemMessage: A,
    showInput: x = !0,
    ...y
  } = e, [a, C] = p(t), [c, T] = p(""), [F, L] = p([]), w = K(I, g, i);
  f(() => C(h(t.map(R))), [t]), f(() => L(w(c, s)), [s]), f(() => {
    const o = h(a.map((D) => D.label)), r = c || "", n = Q(
      s,
      o,
      r
    );
    L(w(r, n));
  }, [a, c, s]);
  const m = (o) => {
    C(o), console.log(o), S(o);
  }, M = ({ value: o }) => T(o), E = (o, { suggestion: r }) => {
    T("");
    const n = J(r);
    if (H(a, n)) {
      m([...a]);
      return;
    }
    m([...a, n]);
  }, P = (o, r) => o.map((n) => (v(n.label, r.label) && (n.toRemove ? delete n.toRemove : n.toRemove = !0), n)), V = (o) => {
    o.toAdd ? m(a.filter((r) => !v(r, o))) : m(P(a, o));
  }, j = {
    className: "",
    placeholder: l || "Start typing ...",
    value: c,
    tabIndex: 1
  }, q = N(!d(a) && "margin-bottom-10");
  return /* @__PURE__ */ O("div", { ...y, className: N("TagManager", u), children: [
    x && /* @__PURE__ */ b(
      _,
      {
        className: q,
        inputProps: j,
        suggestions: F,
        onSuggestionSelected: E,
        onSuggestionsFetchRequested: M,
        openOnFocus: !0,
        noItemMessage: A
      }
    ),
    /* @__PURE__ */ b(z, { tags: a, onRemoveFromTagList: V })
  ] });
};
export {
  se as default
};
//# sourceMappingURL=TagManager.js.map
