import v from "../../../rc-util/es/KeyCode.mjs";
import y from "../../../rc-util/es/raf.mjs";
import * as c from "react";
var C = v.ESC, b = v.TAB;
function p(o) {
  var a = o.visible, d = o.triggerRef, i = o.onVisibleChange, w = o.autoFocus, r = o.overlayRef, u = c.useRef(!1), s = function() {
    if (a) {
      var e, n;
      (e = d.current) === null || e === void 0 || (n = e.focus) === null || n === void 0 || n.call(e), i == null || i(!1);
    }
  }, l = function() {
    var e;
    return (e = r.current) !== null && e !== void 0 && e.focus ? (r.current.focus(), u.current = !0, !0) : !1;
  }, t = function(e) {
    switch (e.keyCode) {
      case C:
        s();
        break;
      case b: {
        var n = !1;
        u.current || (n = l()), n ? e.preventDefault() : s();
        break;
      }
    }
  };
  c.useEffect(function() {
    return a ? (window.addEventListener("keydown", t), w && y(l, 3), function() {
      window.removeEventListener("keydown", t), u.current = !1;
    }) : function() {
      u.current = !1;
    };
  }, [a]);
}
export {
  p as default
};
