import v from "../../../@babel/runtime/helpers/esm/defineProperty.mjs";
import { getFocusNodeList as K } from "../../../rc-util/es/Dom/focus.mjs";
import d from "../../../rc-util/es/KeyCode.mjs";
import q from "../../../rc-util/es/raf.mjs";
import * as G from "react";
import { getMenuId as rr } from "../context/IdContext.mjs";
var W = d.LEFT, B = d.RIGHT, C = d.UP, z = d.DOWN, H = d.ENTER, Q = d.ESC, b = d.HOME, w = d.END, $ = [C, z, W, B];
function er(n, e, t, a) {
  var u, i, r, l, o = "prev", s = "next", c = "children", g = "parent";
  if (n === "inline" && a === H)
    return {
      inlineTrigger: !0
    };
  var h = (u = {}, v(u, C, o), v(u, z, s), u), m = (i = {}, v(i, W, t ? s : o), v(i, B, t ? o : s), v(i, z, c), v(i, H, c), i), f = (r = {}, v(r, C, o), v(r, z, s), v(r, H, c), v(r, Q, g), v(r, W, t ? c : g), v(r, B, t ? g : c), r), E = {
    inline: h,
    horizontal: m,
    vertical: f,
    inlineSub: h,
    horizontalSub: f,
    verticalSub: f
  }, N = (l = E["".concat(n).concat(e ? "" : "Sub")]) === null || l === void 0 ? void 0 : l[a];
  switch (N) {
    case o:
      return {
        offset: -1,
        sibling: !0
      };
    case s:
      return {
        offset: 1,
        sibling: !0
      };
    case g:
      return {
        offset: -1,
        sibling: !1
      };
    case c:
      return {
        offset: 1,
        sibling: !1
      };
    default:
      return null;
  }
}
function tr(n) {
  for (var e = n; e; ) {
    if (e.getAttribute("data-menu-list"))
      return e;
    e = e.parentElement;
  }
  return null;
}
function nr(n, e) {
  for (var t = n || document.activeElement; t; ) {
    if (e.has(t))
      return t;
    t = t.parentElement;
  }
  return null;
}
function O(n, e) {
  var t = K(n, !0);
  return t.filter(function(a) {
    return e.has(a);
  });
}
function M(n, e, t) {
  var a = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 1;
  if (!n)
    return null;
  var u = O(n, e), i = u.length, r = u.findIndex(function(l) {
    return t === l;
  });
  return a < 0 ? r === -1 ? r = i - 1 : r -= 1 : a > 0 && (r += 1), r = (r + i) % i, u[r];
}
var k = function(e, t) {
  var a = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
  return e.forEach(function(r) {
    var l = document.querySelector("[data-menu-id='".concat(rr(t, r), "']"));
    l && (a.add(l), i.set(l, r), u.set(r, l));
  }), {
    elements: a,
    key2element: u,
    element2key: i
  };
};
function vr(n, e, t, a, u, i, r, l, o, s) {
  var c = G.useRef(), g = G.useRef();
  g.current = e;
  var h = function() {
    q.cancel(c.current);
  };
  return G.useEffect(function() {
    return function() {
      h();
    };
  }, []), function(m) {
    var f = m.which;
    if ([].concat($, [H, Q, b, w]).includes(f)) {
      var E = i(), N = k(E, a), A = N, D = A.elements, P = A.key2element, J = A.element2key, j = P.get(e), p = nr(j, D), I = J.get(p), R = er(n, r(I, !0).length === 1, t, f);
      if (!R && f !== b && f !== w)
        return;
      ($.includes(f) || [b, w].includes(f)) && m.preventDefault();
      var U = function(S) {
        if (S) {
          var F = S, L = S.querySelector("a");
          L != null && L.getAttribute("href") && (F = L);
          var Z = J.get(S);
          l(Z), h(), c.current = q(function() {
            g.current === Z && F.focus();
          });
        }
      };
      if ([b, w].includes(f) || R.sibling || !p) {
        var T;
        !p || n === "inline" ? T = u.current : T = tr(p);
        var x, _ = O(T, D);
        f === b ? x = _[0] : f === w ? x = _[_.length - 1] : x = M(T, D, p, R.offset), U(x);
      } else if (R.inlineTrigger)
        o(I);
      else if (R.offset > 0)
        o(I, !0), h(), c.current = q(function() {
          N = k(E, a);
          var Y = p.getAttribute("aria-controls"), S = document.getElementById(Y), F = M(S, N.elements);
          U(F);
        }, 5);
      else if (R.offset < 0) {
        var V = r(I, !0), X = V[V.length - 2], y = P.get(X);
        o(X, !1), U(y);
      }
    }
    s == null || s(m);
  };
}
export {
  O as getFocusableElements,
  k as refreshElements,
  vr as useAccessibility
};
