import w from "../../../@babel/runtime/helpers/esm/extends.mjs";
import A from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import * as i from "react";
import { useRef as p } from "react";
import v from "../../../rc-util/es/KeyCode.mjs";
import F from "./MultipleSelector.mjs";
import H from "./SingleSelector.mjs";
import U from "../hooks/useLock.mjs";
import { isValidateOpenKey as $ } from "../utils/keyUtil.mjs";
var j = function(t, D) {
  var r = p(null), l = p(!1), C = t.prefixCls, f = t.open, a = t.mode, b = t.showSearch, y = t.tokenWithEnter, E = t.autoClearSearchValue, d = t.onSearch, s = t.onSearchSubmit, m = t.onToggleOpen, I = t.onInputKeyDown, R = t.domRef;
  i.useImperativeHandle(D, function() {
    return {
      focus: function() {
        r.current.focus();
      },
      blur: function() {
        r.current.blur();
      }
    };
  });
  var x = U(0), g = A(x, 2), M = g[0], k = g[1], K = function(e) {
    var n = e.which;
    (n === v.UP || n === v.DOWN) && e.preventDefault(), I && I(e), n === v.ENTER && a === "tags" && !l.current && !f && (s == null || s(e.target.value)), $(n) && m(!0);
  }, T = function() {
    k(!0);
  }, u = p(null), S = function(e) {
    d(e, !0, l.current) !== !1 && m(!0);
  }, O = function() {
    l.current = !0;
  }, N = function(e) {
    l.current = !1, a !== "combobox" && S(e.target.value);
  }, P = function(e) {
    var n = e.target.value;
    if (y && u.current && /[\r\n]/.test(u.current)) {
      var c = u.current.replace(/[\r\n]+$/, "").replace(/\r\n/g, " ").replace(/[\r\n]/g, " ");
      n = n.replace(c, u.current);
    }
    u.current = null, S(n);
  }, _ = function(e) {
    var n = e.clipboardData, c = n.getData("text");
    u.current = c;
  }, L = function(e) {
    var n = e.target;
    if (n !== r.current) {
      var c = document.body.style.msTouchAction !== void 0;
      c ? setTimeout(function() {
        r.current.focus();
      }) : r.current.focus();
    }
  }, V = function(e) {
    var n = M();
    e.target !== r.current && !n && a !== "combobox" && e.preventDefault(), (a !== "combobox" && (!b || !n) || !f) && (f && E !== !1 && d("", !0, !1), m());
  }, h = {
    inputRef: r,
    onInputKeyDown: K,
    onInputMouseDown: T,
    onInputChange: P,
    onInputPaste: _,
    onInputCompositionStart: O,
    onInputCompositionEnd: N
  }, W = a === "multiple" || a === "tags" ? /* @__PURE__ */ i.createElement(F, w({}, t, h)) : /* @__PURE__ */ i.createElement(H, w({}, t, h));
  return /* @__PURE__ */ i.createElement("div", {
    ref: R,
    className: "".concat(C, "-selector"),
    onClick: L,
    onMouseDown: V
  }, W);
}, q = /* @__PURE__ */ i.forwardRef(j);
q.displayName = "Selector";
export {
  q as default
};
