import _ from "../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import K from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import I from "../../../classnames/index.mjs";
import Z from "../../../rc-dropdown/es/Dropdown.mjs";
import ee from "../../../rc-menu/es/index.mjs";
import l from "../../../rc-util/es/KeyCode.mjs";
import * as o from "react";
import { useState as L, useEffect as O } from "react";
import { getRemovable as ae } from "../util.mjs";
import te from "./AddButton.mjs";
import oe from "../../../rc-menu/es/MenuItem.mjs";
var ne = /* @__PURE__ */ o.forwardRef(function(a, N) {
  var d = a.prefixCls, u = a.id, f = a.tabs, v = a.locale, B = a.mobile, g = a.moreIcon, M = g === void 0 ? "More" : g, V = a.moreTransitionName, G = a.style, W = a.className, b = a.editable, $ = a.tabBarGutter, x = a.rtl, U = a.removeAriaLabel, h = a.onTabClick, j = a.getPopupContainer, q = a.popupClassName, z = L(!1), w = K(z, 2), m = w[0], p = w[1], F = L(null), D = K(F, 2), i = D[0], P = D[1], y = "".concat(u, "-more-popup"), C = "".concat(d, "-dropdown"), S = i !== null ? "".concat(y, "-").concat(i) : null, T = v == null ? void 0 : v.dropdownAriaLabel;
  function H(e, t) {
    e.preventDefault(), e.stopPropagation(), b.onEdit("remove", {
      key: t,
      event: e
    });
  }
  var J = /* @__PURE__ */ o.createElement(ee, {
    onClick: function(t) {
      var n = t.key, r = t.domEvent;
      h(n, r), p(!1);
    },
    prefixCls: "".concat(C, "-menu"),
    id: y,
    tabIndex: -1,
    role: "listbox",
    "aria-activedescendant": S,
    selectedKeys: [i],
    "aria-label": T !== void 0 ? T : "expanded dropdown"
  }, f.map(function(e) {
    var t = e.closable, n = e.disabled, r = e.closeIcon, c = e.key, E = e.label, s = ae(t, r, b, n);
    return /* @__PURE__ */ o.createElement(oe, {
      key: c,
      id: "".concat(y, "-").concat(c),
      role: "option",
      "aria-controls": u && "".concat(u, "-panel-").concat(c),
      disabled: n
    }, /* @__PURE__ */ o.createElement("span", null, E), s && /* @__PURE__ */ o.createElement("button", {
      type: "button",
      "aria-label": U || "remove",
      tabIndex: 0,
      className: "".concat(C, "-menu-item-remove"),
      onClick: function(R) {
        R.stopPropagation(), H(R, c);
      }
    }, r || b.removeIcon || "×"));
  }));
  function A(e) {
    for (var t = f.filter(function(s) {
      return !s.disabled;
    }), n = t.findIndex(function(s) {
      return s.key === i;
    }) || 0, r = t.length, c = 0; c < r; c += 1) {
      n = (n + e + r) % r;
      var E = t[n];
      if (!E.disabled) {
        P(E.key);
        return;
      }
    }
  }
  function Q(e) {
    var t = e.which;
    if (!m) {
      [l.DOWN, l.SPACE, l.ENTER].includes(t) && (p(!0), e.preventDefault());
      return;
    }
    switch (t) {
      case l.UP:
        A(-1), e.preventDefault();
        break;
      case l.DOWN:
        A(1), e.preventDefault();
        break;
      case l.ESC:
        p(!1);
        break;
      case l.SPACE:
      case l.ENTER:
        i !== null && h(i, e);
        break;
    }
  }
  O(function() {
    var e = document.getElementById(S);
    e && e.scrollIntoView && e.scrollIntoView(!1);
  }, [i]), O(function() {
    m || P(null);
  }, [m]);
  var k = _({}, x ? "marginRight" : "marginLeft", $);
  f.length || (k.visibility = "hidden", k.order = 1);
  var X = I(_({}, "".concat(C, "-rtl"), x)), Y = B ? null : /* @__PURE__ */ o.createElement(Z, {
    prefixCls: C,
    overlay: J,
    trigger: ["hover"],
    visible: f.length ? m : !1,
    transitionName: V,
    onVisibleChange: p,
    overlayClassName: I(X, q),
    mouseEnterDelay: 0.1,
    mouseLeaveDelay: 0.1,
    getPopupContainer: j
  }, /* @__PURE__ */ o.createElement("button", {
    type: "button",
    className: "".concat(d, "-nav-more"),
    style: k,
    tabIndex: -1,
    "aria-hidden": "true",
    "aria-haspopup": "listbox",
    "aria-controls": y,
    id: "".concat(u, "-more"),
    "aria-expanded": m,
    onKeyDown: Q
  }, M));
  return /* @__PURE__ */ o.createElement("div", {
    className: I("".concat(d, "-nav-operations"), W),
    style: G,
    ref: N
  }, Y, /* @__PURE__ */ o.createElement(te, {
    prefixCls: d,
    locale: v,
    editable: b
  }));
});
const pe = /* @__PURE__ */ o.memo(ne, function(a, N) {
  return (
    // https://github.com/ant-design/ant-design/issues/32544
    // We'd better remove syntactic sugar in `rc-menu` since this has perf issue
    N.tabMoving
  );
});
export {
  pe as default
};
