import On from "../../@babel/runtime/helpers/esm/typeof.mjs";
import Ne from "../../@babel/runtime/helpers/esm/extends.mjs";
import v from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import Tn from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import I from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import oe from "../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import An from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import Ve from "../../classnames/index.mjs";
import Fn from "../../rc-util/es/hooks/useLayoutEffect.mjs";
import Kn from "../../rc-util/es/hooks/useMergedState.mjs";
import Ln from "../../rc-util/es/isMobile.mjs";
import xe from "../../rc-util/es/KeyCode.mjs";
import { useComposeRef as _n } from "../../rc-util/es/ref.mjs";
import * as t from "react";
import { useAllowClear as Bn } from "./hooks/useAllowClear.mjs";
import { BaseSelectContext as $n } from "./hooks/useBaseProps.mjs";
import Un from "./hooks/useDelayReset.mjs";
import kn from "./hooks/useLock.mjs";
import zn from "./hooks/useSelectTriggerControl.mjs";
import Wn from "./Selector/index.mjs";
import jn from "./SelectTrigger.mjs";
import Hn from "./TransBtn.mjs";
import { getSeparatedContent as qn } from "./utils/valueUtil.mjs";
var Gn = ["id", "prefixCls", "className", "showSearch", "tagRender", "direction", "omitDomProps", "displayValues", "onDisplayValuesChange", "emptyOptions", "notFoundContent", "onClear", "mode", "disabled", "loading", "getInputElement", "getRawInputElement", "open", "defaultOpen", "onDropdownVisibleChange", "activeValue", "onActiveValueChange", "activeDescendantId", "searchValue", "autoClearSearchValue", "onSearch", "onSearchSplit", "tokenSeparators", "allowClear", "suffixIcon", "clearIcon", "OptionList", "animation", "transitionName", "dropdownStyle", "dropdownClassName", "dropdownMatchSelectWidth", "dropdownRender", "dropdownAlign", "placement", "builtinPlacements", "getPopupContainer", "showAction", "onFocus", "onBlur", "onKeyUp", "onKeyDown", "onMouseDown"], Jn = ["value", "onChange", "removeIcon", "placeholder", "autoFocus", "maxTagCount", "maxTagTextLength", "maxTagPlaceholder", "choiceTransitionName", "onInputKeyDown", "onPopupScroll", "tabIndex"];
function Qn(e) {
  return e === "tags" || e === "multiple";
}
var Xn = /* @__PURE__ */ t.forwardRef(function(e, Pe) {
  var T, d, A = e.id, l = e.prefixCls, Oe = e.className, te = e.showSearch, Te = e.tagRender, Ae = e.direction, F = e.omitDomProps, g = e.displayValues, K = e.onDisplayValuesChange, ae = e.emptyOptions, re = e.notFoundContent, L = re === void 0 ? "Not Found" : re, _ = e.onClear, c = e.mode, f = e.disabled, x = e.loading, le = e.getInputElement, ie = e.getRawInputElement, Fe = e.open, Ke = e.defaultOpen, P = e.onDropdownVisibleChange, Le = e.activeValue, B = e.onActiveValueChange, _e = e.activeDescendantId, ue = e.searchValue, Be = e.autoClearSearchValue, D = e.onSearch, $ = e.onSearchSplit, U = e.tokenSeparators, ce = e.allowClear, se = e.suffixIcon, $e = e.clearIcon, Ue = e.OptionList, ke = e.animation, ze = e.transitionName, We = e.dropdownStyle, je = e.dropdownClassName, He = e.dropdownMatchSelectWidth, qe = e.dropdownRender, Ge = e.dropdownAlign, Je = e.placement, Qe = e.builtinPlacements, Xe = e.getPopupContainer, de = e.showAction, Ye = de === void 0 ? [] : de, fe = e.onFocus, me = e.onBlur, k = e.onKeyUp, z = e.onKeyDown, W = e.onMouseDown, Ze = An(e, Gn), b = Qn(c), M = (te !== void 0 ? te : b) || c === "combobox", j = oe({}, Ze);
  Jn.forEach(function(o) {
    delete j[o];
  }), F == null || F.forEach(function(o) {
    delete j[o];
  });
  var en = t.useState(!1), ve = I(en, 2), nn = ve[0], on = ve[1];
  t.useEffect(function() {
    on(Ln());
  }, []);
  var ge = t.useRef(null), H = t.useRef(null), q = t.useRef(null), N = t.useRef(null), R = t.useRef(null), G = t.useRef(!1), tn = Un(), J = I(tn, 3), Q = J[0], X = J[1], an = J[2];
  t.useImperativeHandle(Pe, function() {
    var o, n;
    return {
      focus: (o = N.current) === null || o === void 0 ? void 0 : o.focus,
      blur: (n = N.current) === null || n === void 0 ? void 0 : n.blur,
      scrollTo: function(u) {
        var a;
        return (a = R.current) === null || a === void 0 ? void 0 : a.scrollTo(u);
      }
    };
  });
  var h = t.useMemo(function() {
    var o;
    if (c !== "combobox")
      return ue;
    var n = (o = g[0]) === null || o === void 0 ? void 0 : o.value;
    return typeof n == "string" || typeof n == "number" ? String(n) : "";
  }, [ue, c, g]), pe = c === "combobox" && typeof le == "function" && le() || null, w = typeof ie == "function" && ie(), rn = _n(H, w == null || (T = w.props) === null || T === void 0 ? void 0 : T.ref), ln = t.useState(!1), he = I(ln, 2), un = he[0], cn = he[1];
  Fn(function() {
    cn(!0);
  }, []);
  var sn = Kn(!1, {
    defaultValue: Ke,
    value: Fe
  }), we = I(sn, 2), Se = we[0], Y = we[1], r = un ? Se : !1, Ce = !L && ae;
  (f || Ce && r && c === "combobox") && (r = !1);
  var O = Ce ? !1 : r, p = t.useCallback(function(o) {
    var n = o !== void 0 ? o : !r;
    f || (Y(n), r !== n && (P == null || P(n)));
  }, [f, r, Y, P]), dn = t.useMemo(function() {
    return (U || []).some(function(o) {
      return [`
`, `\r
`].includes(o);
    });
  }, [U]), Z = function(n, i, u) {
    var a = !0, s = n;
    B == null || B(null);
    var m = u ? null : qn(n, U);
    return c !== "combobox" && m && (s = "", $ == null || $(m), p(!1), a = !1), D && h !== s && D(s, {
      source: i ? "typing" : "effect"
    }), a;
  }, fn = function(n) {
    !n || !n.trim() || D(n, {
      source: "submit"
    });
  };
  t.useEffect(function() {
    !r && !b && c !== "combobox" && Z("", !1, !1);
  }, [r]), t.useEffect(function() {
    Se && f && Y(!1), f && !G.current && X(!1);
  }, [f]);
  var mn = kn(), be = I(mn, 2), vn = be[0], gn = be[1], pn = function(n) {
    var i = vn(), u = n.which;
    if (u === xe.ENTER && (c !== "combobox" && n.preventDefault(), r || p(!0)), gn(!!h), u === xe.BACKSPACE && !i && b && !h && g.length) {
      for (var a = Tn(g), s = null, m = a.length - 1; m >= 0; m -= 1) {
        var V = a[m];
        if (!V.disabled) {
          a.splice(m, 1), s = V;
          break;
        }
      }
      s && K(a, {
        type: "remove",
        values: [s]
      });
    }
    for (var S = arguments.length, E = new Array(S > 1 ? S - 1 : 0), C = 1; C < S; C++)
      E[C - 1] = arguments[C];
    if (r && R.current) {
      var Me;
      (Me = R.current).onKeyDown.apply(Me, [n].concat(E));
    }
    z == null || z.apply(void 0, [n].concat(E));
  }, hn = function(n) {
    for (var i = arguments.length, u = new Array(i > 1 ? i - 1 : 0), a = 1; a < i; a++)
      u[a - 1] = arguments[a];
    if (r && R.current) {
      var s;
      (s = R.current).onKeyUp.apply(s, [n].concat(u));
    }
    k == null || k.apply(void 0, [n].concat(u));
  }, wn = function(n) {
    var i = g.filter(function(u) {
      return u !== n;
    });
    K(i, {
      type: "remove",
      values: [n]
    });
  }, ee = t.useRef(!1), Sn = function() {
    X(!0), f || (fe && !ee.current && fe.apply(void 0, arguments), Ye.includes("focus") && p(!0)), ee.current = !0;
  }, Cn = function() {
    G.current = !0, X(!1, function() {
      ee.current = !1, G.current = !1, p(!1);
    }), !f && (h && (c === "tags" ? D(h, {
      source: "submit"
    }) : c === "multiple" && D("", {
      source: "blur"
    })), me && me.apply(void 0, arguments));
  }, y = [];
  t.useEffect(function() {
    return function() {
      y.forEach(function(o) {
        return clearTimeout(o);
      }), y.splice(0, y.length);
    };
  }, []);
  var bn = function(n) {
    var i, u = n.target, a = (i = q.current) === null || i === void 0 ? void 0 : i.getPopupElement();
    if (a && a.contains(u)) {
      var s = setTimeout(function() {
        var E = y.indexOf(s);
        if (E !== -1 && y.splice(E, 1), an(), !nn && !a.contains(document.activeElement)) {
          var C;
          (C = N.current) === null || C === void 0 || C.focus();
        }
      });
      y.push(s);
    }
    for (var m = arguments.length, V = new Array(m > 1 ? m - 1 : 0), S = 1; S < m; S++)
      V[S - 1] = arguments[S];
    W == null || W.apply(void 0, [n].concat(V));
  }, Rn = t.useState({}), yn = I(Rn, 2), En = yn[1];
  function In() {
    En({});
  }
  var Re;
  w && (Re = function(n) {
    p(n);
  }), zn(function() {
    var o;
    return [ge.current, (o = q.current) === null || o === void 0 ? void 0 : o.getPopupElement()];
  }, O, p, !!w);
  var Dn = t.useMemo(function() {
    return oe(oe({}, e), {}, {
      notFoundContent: L,
      open: r,
      triggerOpen: O,
      id: A,
      showSearch: M,
      multiple: b,
      toggleOpen: p
    });
  }, [e, L, O, r, A, M, b, p]), ye = !!se || x, Ee;
  ye && (Ee = /* @__PURE__ */ t.createElement(Hn, {
    className: Ve("".concat(l, "-arrow"), v({}, "".concat(l, "-arrow-loading"), x)),
    customizeIcon: se,
    customizeIconProps: {
      loading: x,
      searchValue: h,
      open: r,
      focused: Q,
      showSearch: M
    }
  }));
  var Mn = function() {
    var n;
    _ == null || _(), (n = N.current) === null || n === void 0 || n.focus(), K([], {
      type: "clear",
      values: g
    }), Z("", !1, !1);
  }, Ie = Bn(l, Mn, g, ce, $e, f, h, c), Nn = Ie.allowClear, Vn = Ie.clearIcon, xn = /* @__PURE__ */ t.createElement(Ue, {
    ref: R
  }), Pn = Ve(l, Oe, (d = {}, v(d, "".concat(l, "-focused"), Q), v(d, "".concat(l, "-multiple"), b), v(d, "".concat(l, "-single"), !b), v(d, "".concat(l, "-allow-clear"), ce), v(d, "".concat(l, "-show-arrow"), ye), v(d, "".concat(l, "-disabled"), f), v(d, "".concat(l, "-loading"), x), v(d, "".concat(l, "-open"), r), v(d, "".concat(l, "-customize-input"), pe), v(d, "".concat(l, "-show-search"), M), d)), De = /* @__PURE__ */ t.createElement(jn, {
    ref: q,
    disabled: f,
    prefixCls: l,
    visible: O,
    popupElement: xn,
    animation: ke,
    transitionName: ze,
    dropdownStyle: We,
    dropdownClassName: je,
    direction: Ae,
    dropdownMatchSelectWidth: He,
    dropdownRender: qe,
    dropdownAlign: Ge,
    placement: Je,
    builtinPlacements: Qe,
    getPopupContainer: Xe,
    empty: ae,
    getTriggerDOMNode: function() {
      return H.current;
    },
    onPopupVisibleChange: Re,
    onPopupMouseEnter: In
  }, w ? /* @__PURE__ */ t.cloneElement(w, {
    ref: rn
  }) : /* @__PURE__ */ t.createElement(Wn, Ne({}, e, {
    domRef: H,
    prefixCls: l,
    inputElement: pe,
    ref: N,
    id: A,
    showSearch: M,
    autoClearSearchValue: Be,
    mode: c,
    activeDescendantId: _e,
    tagRender: Te,
    values: g,
    open: r,
    onToggleOpen: p,
    activeValue: Le,
    searchValue: h,
    onSearch: Z,
    onSearchSubmit: fn,
    onRemove: wn,
    tokenWithEnter: dn
  }))), ne;
  return w ? ne = De : ne = /* @__PURE__ */ t.createElement("div", Ne({
    className: Pn
  }, j, {
    ref: ge,
    onMouseDown: bn,
    onKeyDown: pn,
    onKeyUp: hn,
    onFocus: Sn,
    onBlur: Cn
  }), Q && !r && /* @__PURE__ */ t.createElement("span", {
    style: {
      width: 0,
      height: 0,
      position: "absolute",
      overflow: "hidden",
      opacity: 0
    },
    "aria-live": "polite"
  }, "".concat(g.map(function(o) {
    var n = o.label, i = o.value;
    return ["number", "string"].includes(On(n)) ? n : i;
  }).join(", "))), De, Ee, Nn && Vn), /* @__PURE__ */ t.createElement($n.Provider, {
    value: Dn
  }, ne);
});
process.env.NODE_ENV !== "production" && (Xn.displayName = "BaseSelect");
export {
  Xn as default,
  Qn as isMultiple
};
