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