import { jsx as v } from "react/jsx-runtime";
import { useRef as k, useMemo as C, useCallback as L } from "react";
import { COMMAND_VARIANT as S } from "../command/constants/index.js";
import { ComboBoxSelectMode as n } from "./types/ComboBoxMode.js";
import D from "../../hooks/useControlledState.js";
import F from "./components/combo-box-default/ComboBoxDefault.js";
import O from "../command/Command.js";
const P = (l, i) => {
  const c = i.toLowerCase();
  return l.label.toLowerCase().includes(c) || l.value.toLowerCase().includes(c);
}, _ = ({
  placeholder: l = "Search...",
  menuItems: i,
  isLoading: c,
  onSelect: b,
  value: g,
  noResultText: M = "No results found for",
  isDisabled: d = !1,
  triggerId: R,
  contentId: x,
  isContentLoading: I,
  isOpen: N,
  mode: e = "single",
  ref: E,
  renderItem: w,
  filter: p
}) => {
  const f = k(null), B = E ?? f, { value: r, setValue: s } = D(
    g,
    void 0,
    b
  ), u = C(() => i.map((t) => ({
    ...t,
    heading: t.heading || "",
    items: t.items.map((o) => ({
      ...o,
      ...e === n.SINGLE ? { selected: o.value === r } : {}
    }))
  })), [i, e, r]), G = C(() => {
    const t = p ?? P, o = new Map(u.flatMap((a) => a.items).map((a) => [String(a.value), a]));
    return (a, T) => {
      const m = o.get(a);
      return t({ label: (m == null ? void 0 : m.label) ?? a, value: a }, T) ? 1 : 0;
    };
  }, [p, u]), h = L(
    (t) => {
      var o;
      e === n.SINGLE && ((o = f.current) == null || o.click()), s(t);
    },
    [s, e, f]
  ), A = L(
    (t) => {
      e === n.MULTI && (s == null || s(r == null ? void 0 : r.filter((o) => o !== t)));
    },
    [s, r, e]
  );
  return /* @__PURE__ */ v(
    F,
    {
      value: r,
      placeholder: l,
      isDisabled: d,
      triggerId: R,
      contentId: x,
      isLoading: c,
      isOpen: N,
      handleRemoveValue: A,
      mode: e,
      triggerRef: B,
      groups: u,
      children: /* @__PURE__ */ v(
        O,
        {
          variant: e === n.MULTI ? S.CHECKBOX : S.SIMPLE,
          groups: u,
          placeholder: l,
          value: e === n.SINGLE ? void 0 : r,
          onItemSelect: h,
          isPopover: !0,
          noResultsLabel: M,
          isLoading: I,
          renderItem: w,
          filter: G
        }
      )
    }
  );
};
export {
  _ as default
};
//# sourceMappingURL=ComboBox.js.map
