import C from "../../@babel/runtime/helpers/esm/defineProperty.mjs";
import Se from "../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import M from "../../@babel/runtime/helpers/esm/extends.mjs";
import we from "../../@babel/runtime/helpers/esm/toConsumableArray.mjs";
import Ce from "../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import Z from "../../classnames/index.mjs";
import c from "../../rc-util/es/KeyCode.mjs";
import Ee from "../../rc-util/es/hooks/useMemo.mjs";
import Ie from "../../rc-util/es/omit.mjs";
import $ from "../../rc-util/es/pickAttrs.mjs";
import Oe from "../../rc-virtual-list/es/List.mjs";
import * as i from "react";
import { useEffect as ee } from "react";
import Ae from "./SelectContext.mjs";
import ke from "./TransBtn.mjs";
import Le from "./hooks/useBaseProps.mjs";
import { isPlatformMac as Te } from "./utils/platformUtil.mjs";
var Ve = ["disabled", "title", "children", "style", "className"];
function te(E) {
  return typeof E == "string" || typeof E == "number";
}
var De = function(Me, re) {
  var u = Le(), oe = u.prefixCls, I = u.id, p = u.open, K = u.multiple, R = u.mode, x = u.searchValue, H = u.toggleOpen, ne = u.notFoundContent, ae = u.onPopupScroll, s = i.useContext(Ae), F = s.flattenOptions, z = s.onActiveValue, ie = s.defaultActiveFirstOption, le = s.onSelect, N = s.menuItemSelectedIcon, v = s.rawValues, U = s.fieldNames, P = s.virtual, se = s.direction, ce = s.listHeight, ue = s.listItemHeight, B = s.optionRender, g = "".concat(oe, "-item"), f = Ee(function() {
    return F;
  }, [p, F], function(t, e) {
    return e[0] && t[1] !== e[1];
  }), S = i.useRef(null), W = function(e) {
    e.preventDefault();
  }, O = function(e) {
    S.current && S.current.scrollTo(typeof e == "number" ? {
      index: e
    } : e);
  }, A = function(e) {
    for (var r = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 1, a = f.length, o = 0; o < a; o += 1) {
      var n = (e + o * r + a) % a, l = f[n], d = l.group, T = l.data;
      if (!d && !T.disabled)
        return n;
    }
    return -1;
  }, fe = i.useState(function() {
    return A(0);
  }), j = Ce(fe, 2), m = j[0], de = j[1], w = function(e) {
    var r = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
    de(e);
    var a = {
      source: r ? "keyboard" : "mouse"
    }, o = f[e];
    if (!o) {
      z(null, -1, a);
      return;
    }
    z(o.value, e, a);
  };
  ee(function() {
    w(ie !== !1 ? A(0) : -1);
  }, [f.length, x]);
  var q = i.useCallback(function(t) {
    return v.has(t) && R !== "combobox";
  }, [R, we(v).toString(), v.size]);
  ee(function() {
    var t = setTimeout(function() {
      if (!K && p && v.size === 1) {
        var r = Array.from(v)[0], a = f.findIndex(function(o) {
          var n = o.data;
          return n.value === r;
        });
        a !== -1 && (w(a), O(a));
      }
    });
    if (p) {
      var e;
      (e = S.current) === null || e === void 0 || e.scrollTo(void 0);
    }
    return function() {
      return clearTimeout(t);
    };
  }, [p, x]);
  var k = function(e) {
    e !== void 0 && le(e, {
      selected: !v.has(e)
    }), K || H(!1);
  };
  if (i.useImperativeHandle(re, function() {
    return {
      onKeyDown: function(e) {
        var r = e.which, a = e.ctrlKey;
        switch (r) {
          case c.N:
          case c.P:
          case c.UP:
          case c.DOWN: {
            var o = 0;
            if (r === c.UP ? o = -1 : r === c.DOWN ? o = 1 : Te() && a && (r === c.N ? o = 1 : r === c.P && (o = -1)), o !== 0) {
              var n = A(m + o, o);
              O(n), w(n, !0);
            }
            break;
          }
          case c.ENTER: {
            var l = f[m];
            l && !l.data.disabled ? k(l.value) : k(void 0), p && e.preventDefault();
            break;
          }
          case c.ESC:
            H(!1), p && e.stopPropagation();
        }
      },
      onKeyUp: function() {
      },
      scrollTo: function(e) {
        O(e);
      }
    };
  }), f.length === 0)
    return /* @__PURE__ */ i.createElement("div", {
      role: "listbox",
      id: "".concat(I, "_list"),
      className: "".concat(g, "-empty"),
      onMouseDown: W
    }, ne);
  var me = Object.keys(U).map(function(t) {
    return U[t];
  }), G = function(e) {
    return e.label;
  };
  function J(t, e) {
    var r = t.group;
    return {
      role: r ? "presentation" : "option",
      id: "".concat(I, "_list_").concat(e)
    };
  }
  var L = function(e) {
    var r = f[e];
    if (!r)
      return null;
    var a = r.data || {}, o = a.value, n = r.group, l = $(a, !0), d = G(r);
    return r ? /* @__PURE__ */ i.createElement("div", M({
      "aria-label": typeof d == "string" && !n ? d : null
    }, l, {
      key: e
    }, J(r, e), {
      "aria-selected": q(o)
    }), o) : null;
  }, Q = {
    role: "listbox",
    id: "".concat(I, "_list")
  };
  return /* @__PURE__ */ i.createElement(i.Fragment, null, P && /* @__PURE__ */ i.createElement("div", M({}, Q, {
    style: {
      height: 0,
      width: 0,
      overflow: "hidden"
    }
  }), L(m - 1), L(m), L(m + 1)), /* @__PURE__ */ i.createElement(Oe, {
    itemKey: "key",
    ref: S,
    data: f,
    height: ce,
    itemHeight: ue,
    fullHeight: !1,
    onMouseDown: W,
    onScroll: ae,
    virtual: P,
    direction: se,
    innerProps: P ? null : Q
  }, function(t, e) {
    var r, a = t.group, o = t.groupOption, n = t.data, l = t.label, d = t.value, T = n.key;
    if (a) {
      var V, pe = (V = n.title) !== null && V !== void 0 ? V : te(l) ? l.toString() : void 0;
      return /* @__PURE__ */ i.createElement("div", {
        className: Z(g, "".concat(g, "-group")),
        title: pe
      }, l !== void 0 ? l : T);
    }
    var h = n.disabled, X = n.title;
    n.children;
    var ve = n.style, ge = n.className, ye = Se(n, Ve), be = Ie(ye, me), y = q(d), b = "".concat(g, "-option"), he = Z(g, b, ge, (r = {}, C(r, "".concat(b, "-grouped"), o), C(r, "".concat(b, "-active"), m === e && !h), C(r, "".concat(b, "-disabled"), h), C(r, "".concat(b, "-selected"), y), r)), D = G(t), Ne = !N || typeof N == "function" || y, _ = typeof D == "number" ? D : D || d, Y = te(_) ? _.toString() : void 0;
    return X !== void 0 && (Y = X), /* @__PURE__ */ i.createElement("div", M({}, $(be), P ? {} : J(t, e), {
      "aria-selected": y,
      className: he,
      title: Y,
      onMouseMove: function() {
        m === e || h || w(e);
      },
      onClick: function() {
        h || k(d);
      },
      style: ve
    }), /* @__PURE__ */ i.createElement("div", {
      className: "".concat(b, "-content")
    }, typeof B == "function" ? B(t, {
      index: e
    }) : _), /* @__PURE__ */ i.isValidElement(N) || y, Ne && /* @__PURE__ */ i.createElement(ke, {
      className: "".concat(g, "-option-state"),
      customizeIcon: N,
      customizeIconProps: {
        value: d,
        disabled: h,
        isSelected: y
      }
    }, y ? "✓" : null));
  }));
}, _e = /* @__PURE__ */ i.forwardRef(De);
_e.displayName = "OptionList";
export {
  _e as default
};
