import { jsxs as F, jsx as i } from "react/jsx-runtime";
import { useState as o, useRef as U } from "react";
import { isEqual as V } from "es-toolkit/predicate";
import { noop as Oe, isEmpty as ve } from "es-toolkit/compat";
import K from "../../utils/classNames.js";
import xe, { filterOptions as we } from "./BaseSelectDropdown.js";
import De from "../../hooks/useClickOutside.js";
import Me from "../../hooks/useEffectOnce.js";
import Ne from "../../hooks/useMergeRefs.js";
import Ee from "./ClearButton.js";
import ke from "./SelectFilter.js";
import Ae from "./SelectedOption.js";
import Te from "./WithFeedbackAndAddon.js";
const u = -1, Xe = (R) => {
  const {
    name: b,
    id: B = b,
    label: _,
    options: t = [],
    value: n,
    onChange: O = Oe,
    placeholder: j,
    isLoading: L = !1,
    dropup: W = !1,
    pullRight: q = !1,
    autoDropDirection: z = !0,
    bsSize: g = "md",
    variant: X = "default",
    disabled: v = !1,
    tabIndex: G = 0,
    hasError: H = !1,
    useFilter: J = !1,
    useClear: Q = !1,
    noItemMessage: Y,
    selectedOptionText: x,
    showSelectedItemIcon: Z = !1,
    showUnselectedItemIcons: $ = !1,
    dropdownClassName: ee,
    btnClassName: w,
    className: D,
    inputAddon: M,
    errorMessage: N,
    warningMessage: E,
    messageWhiteSpace: te = "normal",
    ...se
  } = R, [l, k] = o(!1), [h, p] = o(null), [I, c] = o(!1), [A, d] = o(""), [oe, r] = o(t), [T, ne] = o([]), [le, m] = o(u), [y, f] = o(!1), re = U(), P = U(null), ae = De(() => S()), ie = Ne(re, ae), C = (e, s) => {
    s && !ve(s) ? p(e.find((a) => a.id === s[0]) || null) : e && p(e.find((a) => a.selected) || null);
  };
  Me(() => {
    C(t, n);
  });
  const [ce, de] = o(t);
  V(t, ce) || (C(t, n), r(t), de(t));
  const [fe, ue] = o(n);
  V(n, fe) || (C(t, n), ue(n));
  const pe = (e = []) => {
    !e.length || (I || A) && T.length > 0 || ne(e);
  }, me = () => {
    p(null), c(!1), d(""), r(t), f(!1), m(u), O(void 0);
  }, ge = () => {
    const e = K(
      "dropdown-toggle",
      "form-control",
      "text-left",
      w && w,
      g === "sm" && "input-sm",
      g === "lg" && "input-lg",
      v && "disabled"
    ), s = /* @__PURE__ */ F(
      "button",
      {
        type: "button",
        id: B,
        name: b,
        className: e,
        "data-toggle": "dropdown",
        tabIndex: G,
        "aria-haspopup": "true",
        "aria-expanded": l,
        onClick: Se,
        ref: P,
        children: [
          J && l && /* @__PURE__ */ i(
            ke,
            {
              isFilterActive: I,
              filterValue: A,
              onChange: Ie
            }
          ),
          x || /* @__PURE__ */ i(
            Ae,
            {
              label: _,
              placeholder: j,
              selectedItem: h,
              options: t,
              showSelectedItemIcon: Z,
              showUnselectedItemIcons: $
            }
          ),
          /* @__PURE__ */ F("span", { className: "select-toggle-actions", children: [
            /* @__PURE__ */ i(Ee, { showClear: Q, selectedItem: h, onClear: me }),
            /* @__PURE__ */ i("span", { className: "caret" })
          ] })
        ]
      }
    );
    return !M && !N && !E ? s : /* @__PURE__ */ i(
      Te,
      {
        bsSize: g,
        inputAddon: M,
        errorMessage: N,
        warningMessage: E,
        messageWhiteSpace: te,
        children: s
      }
    );
  }, he = () => {
    let e = le;
    return y && !h && (e = 0), /* @__PURE__ */ i(
      xe,
      {
        isOpen: l,
        isLoading: L,
        options: oe,
        focusedItemIndex: e,
        keyboardUsed: y,
        updateDOMValues: pe,
        onSelect: Ce,
        onClose: S,
        noItemMessage: Y,
        pullRight: q,
        dropup: W,
        autoDropDirection: z,
        dropdownClassName: ee
      }
    );
  }, Ie = (e) => {
    e.preventDefault();
    const s = e.currentTarget.value;
    if (s === "") {
      c(!1), d(""), r(t), f(!1), m(u);
      return;
    }
    const a = we(T, s, t), be = a.length > 0 ? 0 : u;
    c(!0), d(s), r(a), f(!0), m(be);
  }, Ce = (e) => {
    p(e || null), c(!1), d(""), r(t), O(e), S();
  }, Se = (e) => {
    if (v || I)
      return;
    const s = e.detail === 0;
    k(!l), f(s);
  }, S = () => {
    l && (k(!1), c(!1), d(""), r(t), f(!1), m(u), P.current?.focus());
  }, Fe = K(
    "select",
    "dropdown",
    l && "open",
    H && "has-error",
    X === "blank" && "select-blank",
    D && D
  );
  return /* @__PURE__ */ F("div", { ...se, className: Fe, ref: ie, children: [
    ge(),
    he()
  ] });
};
export {
  Xe as default
};
//# sourceMappingURL=Select.js.map
