import { jsxs as J, jsx as n, Fragment as q } from "react/jsx-runtime";
import { useMemo as rr, useState as E, useEffect as H, useCallback as T } from "react";
import { c as M } from "./index.CfM2L_w0.js";
import { a as G } from "./index.CfQlFjZU.js";
import { n as C } from "./index.B0wX8xO7.js";
import { a as X, c as er } from "./DropdownMenu.CkFSTgCs.js";
import { u as or } from "./useOutsideClick.DRN0ZU0B.js";
import { j as nr, k as ar } from "./index.dWlEA7uO.js";
import { u as Q } from "./useId.7P31viIO.js";
import { a as Z } from "./Tag.Bk_lQM4l.js";
let lr = (e = 21) => crypto.getRandomValues(new Uint8Array(e)).reduce((r, l) => (l &= 63, l < 36 ? r += l.toString(36) : l < 62 ? r += (l - 26).toString(36).toUpperCase() : l > 62 ? r += "-" : r += "_", r), "");
const dr = "creator_container_h69kJ", sr = "creator_dropdownWrapper_m7ZNL", tr = "creator_dropdownWrapper__up_pPNsr", cr = "creator_label_puFDc", pr = "creator_helper_rDpe-", _r = "creator_helper__error_-ATFe", $ = { container: dr, dropdownWrapper: sr, dropdownWrapper__up: tr, label: cr, helper: pr, helper__error: _r }, ir = "creator_dropdownField_jOwae", fr = "creator_dropdownField__disabled_Y-JhL", ur = "creator_dropdownField__readonly_366dA", wr = "creator_dropdownField__error_n1QCw", hr = "creator_dropdownField__expanded_zYVUj", br = "creator_label_FU0Re", Fr = "creator_label__keyword_ibYpr", yr = "creator_label__placeholder_qCvSP", y = { dropdownField: ir, dropdownField__disabled: fr, dropdownField__readonly: ur, dropdownField__error: wr, dropdownField__expanded: hr, label: br, label__keyword: Fr, label__placeholder: yr }, gr = (e) => {
  const r = e[0];
  return typeof r == "string" ? `${r} + ${e.length - 1} items` : `${r.label} + ${e.length - 1} items`;
}, xr = ({
  keyword: e,
  selectedItems: r,
  placeholder: l,
  disabled: t,
  readOnly: c,
  hasError: o,
  dropdownFieldClassName: i,
  multiLabel: f = gr,
  ...u
}) => {
  const d = rr(() => r && r?.length === 1 ? r[0] : null, [r]);
  return {
    placeholder: l,
    label: r && (r.length === 1 ? Cr(r[0]) : r.length > 1 ? f(r) : "") || e || l,
    fieldClassName: M(y.dropdownField, i, {
      [y.dropdownField__disabled]: t,
      [y.dropdownField__readonly]: c,
      [y.dropdownField__error]: o
    }),
    labelClassName: M(y.label, {
      [y.label__placeholder]: r && !r?.length,
      [y.label__keyword]: !!r?.length
    }),
    disabled: t,
    prefixTagProps: d && d.prefixTagProps || void 0,
    suffixTagProps: d && d.suffixTagProps || void 0,
    optionPrefixIcon: d && d.prefixIcon || void 0,
    optionSuffixIcon: d && d.suffixIcon || void 0,
    ...u
  };
}, Ar = ({
  label: e,
  expanded: r,
  disabled: l,
  suffixIcon: t,
  fieldClassName: c,
  labelClassName: o,
  prefixTagProps: i,
  suffixTagProps: f,
  optionPrefixIcon: u,
  optionSuffixIcon: d,
  showIconsInField: w,
  showTagInField: h,
  ...b
}) => /* @__PURE__ */ J(
  "button",
  {
    className: M(c, {
      [y.dropdownField__expanded]: r
    }),
    disabled: l,
    "aria-labelledby": "select button",
    "aria-haspopup": "listbox",
    "aria-expanded": r,
    type: "button",
    ...b,
    children: [
      /* @__PURE__ */ n(
        C,
        {
          value: h && i,
          then: /* @__PURE__ */ n(Z, { ...i })
        }
      ),
      /* @__PURE__ */ n(
        C,
        {
          value: w && u,
          then: /* @__PURE__ */ n(q, { children: u })
        }
      ),
      /* @__PURE__ */ n("span", { className: o, children: e }),
      /* @__PURE__ */ n(
        C,
        {
          value: w && d,
          then: /* @__PURE__ */ n(q, { children: d })
        }
      ),
      /* @__PURE__ */ n(
        C,
        {
          value: h && f,
          then: /* @__PURE__ */ n(Z, { ...f })
        }
      ),
      t
    ]
  }
), Nr = G(Ar, xr);
function Dr(e) {
  return typeof e == "string" ? e : e.value;
}
function Cr(e) {
  return typeof e == "string" ? e : e.label || e.value;
}
function B(e) {
  return Array.isArray(e) ? (r) => typeof r == "string" ? e.includes(r) : e.includes(r.value) : (r) => typeof r == "string" ? r === e : r.value === e;
}
function Ur({
  emptyLabel: e = X,
  hasError: r,
  name: l,
  multiple: t,
  value: c,
  items: o,
  showTagInField: i,
  showIconsInField: f,
  noHelper: u,
  noLabel: d,
  onClick: w,
  onChange: h,
  onSelect: b,
  className: V,
  ...K
}) {
  const [O] = E(l || lr()), [p, A] = E(!1), [U, W] = E(c), [a, L] = E(), [g, j] = E(
    o.filter(B(c))
  ), S = or(() => A(!1)), m = Q(), k = Q();
  H(() => {
    j(o.filter(B(U)));
  }, [U, a, o]), H(() => {
    W(c);
  }, [c]);
  const N = T(() => A(!p), [p]), v = T(
    (_, s) => {
      W((F) => {
        let x;
        const z = Dr(_);
        if (t) {
          const R = Array.isArray(F) ? F : F ? [F] : [];
          x = s ? R.filter((I) => I !== z) : R.concat([z]);
        } else s || (x = z);
        return h && h(x), b && b(o.filter(B(x))), t || A(!1), x;
      });
    },
    [t, h, b, o]
  ), D = T(() => {
    if (!a) return;
    const _ = g.includes(a);
    v(a, _);
    const s = _ ? g.filter((F) => F !== a) : t ? g.concat([a]) : [a];
    j(s);
  }, [v, a, t, g]), P = T(
    (_) => {
      p && (D(), L(void 0)), N(), w && w(_);
    },
    [p, N, w, D]
  ), Y = T(
    (_) => {
      let s;
      switch (_.code) {
        case "Escape":
          return A(!1);
        case "Enter":
        case "Space":
          p && D();
          return;
        case "ArrowDown":
          s = (a ? o.indexOf(a) + 1 : 0) % o.length, L(o[s]);
          break;
        case "ArrowUp":
          s = (a ? o.indexOf(a) - 1 : 0) % o.length, s = s < 0 ? o.length - 1 : s, L(o[s]);
          break;
        default:
          return;
      }
      p || N();
    },
    [a, p, o, D, N]
  );
  return {
    emptyLabel: e,
    name: O,
    ref: S,
    expanded: p,
    value: U,
    multiple: t,
    current: a,
    items: o,
    selectedItems: g,
    showTagInField: i,
    showIconsInField: f,
    showHelper: !u,
    showLabel: !d,
    click: P,
    change: v,
    keyUp: Y,
    containerClassName: M([$.container, V]),
    labelClassName: $.label,
    dropdownWrapperClass: $.dropdownWrapper,
    helperClassName: M($.helper, {
      [$.helper__error]: r
    }),
    hasError: r,
    labelId: m,
    helperId: k,
    labelledby: `${m} ${k}`,
    ...K
  };
}
const Lr = ({
  emptyLabel: e = X,
  label: r,
  ref: l,
  current: t,
  showTagInField: c,
  showIconsInField: o,
  showLabel: i,
  showHelper: f,
  name: u,
  helper: d,
  disabled: w,
  readOnly: h,
  expanded: b,
  containerClassName: V,
  labelClassName: K,
  dropdownWrapperClass: O,
  keyword: p,
  helperClassName: A,
  placeholder: U,
  hasSearch: W,
  searchOption: a = "default",
  searchPlaceholder: L,
  multiple: g,
  items: j,
  value: S,
  hasError: m,
  selectedItems: k,
  openDirection: N,
  click: v,
  change: D,
  keyUp: P,
  labelId: Y,
  helperId: _,
  labelledby: s,
  fieldClassName: F = "",
  removeItemOnReselect: x
}) => /* @__PURE__ */ J("div", { ref: l, className: V, children: [
  /* @__PURE__ */ n(
    C,
    {
      value: i,
      then: /* @__PURE__ */ n("span", { id: Y, className: K, children: r })
    }
  ),
  /* @__PURE__ */ J("div", { className: O, children: [
    /* @__PURE__ */ n(
      Nr,
      {
        dropdownFieldClassName: F,
        keyword: p,
        placeholder: U,
        disabled: w,
        readOnly: h,
        expanded: b,
        showTagInField: c,
        showIconsInField: o,
        value: S,
        onClick: v,
        onKeyUp: P,
        hasError: m,
        selectedItems: k,
        suffixIcon: b ? /* @__PURE__ */ n(nr, { size: 24 }) : /* @__PURE__ */ n(ar, { size: 24 }),
        "aria-labelledby": s
      }
    ),
    /* @__PURE__ */ n(
      er,
      {
        multiple: g,
        name: u,
        onChange: D,
        onKeyUp: P,
        current: t,
        items: j,
        emptyLabel: e,
        selectedItems: k,
        hasSearch: W,
        searchOption: a,
        searchPlaceholder: L,
        removeItemOnReselect: x,
        openDirection: N
      }
    )
  ] }),
  /* @__PURE__ */ n(
    C,
    {
      value: f,
      then: /* @__PURE__ */ n("span", { id: _, className: A, children: d })
    }
  )
] }), Vr = G(Lr, Ur);
export {
  gr as D,
  Ar as a,
  Nr as b,
  Vr as c,
  Cr as d,
  Ur as e,
  Lr as f,
  Dr as i,
  xr as u
};
