import j from "../../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { warning as O } from "../../../../rc-util/es/warning.mjs";
import { getShadowRoot as Q } from "../../../../rc-util/es/Dom/shadow.mjs";
import q from "../../../../rc-util/es/raf.mjs";
import * as c from "react";
import { getWin as T } from "../util.mjs";
function P(u, x, r, t, l, W, y, z) {
  var s = c.useRef(u), m = c.useRef(!1);
  s.current !== u && (m.current = !0, s.current = u), c.useEffect(function() {
    var i = q(function() {
      m.current = !1;
    });
    return function() {
      q.cancel(i);
    };
  }, [u]), c.useEffect(function() {
    if (x && t && (!l || W)) {
      var i = function() {
        var V = !1, K = function(o) {
          var L = o.target;
          V = y(L);
        }, M = function(o) {
          var L = o.target;
          !m.current && s.current && !V && !y(L) && z(!1);
        };
        return [K, M];
      }, B = i(), A = j(B, 2), D = A[0], v = A[1], F = i(), E = j(F, 2), I = E[0], f = E[1], n = T(t);
      n.addEventListener("mousedown", D, !0), n.addEventListener("click", v, !0), n.addEventListener("contextmenu", v, !0);
      var e = Q(r);
      if (e && (e.addEventListener("mousedown", I, !0), e.addEventListener("click", f, !0), e.addEventListener("contextmenu", f, !0)), process.env.NODE_ENV !== "production") {
        var d, a, G = r == null || (d = r.getRootNode) === null || d === void 0 ? void 0 : d.call(r), J = (a = t.getRootNode) === null || a === void 0 ? void 0 : a.call(t);
        O(G === J, "trigger element and popup element should in same shadow root.");
      }
      return function() {
        n.removeEventListener("mousedown", D, !0), n.removeEventListener("click", v, !0), n.removeEventListener("contextmenu", v, !0), e && (e.removeEventListener("mousedown", I, !0), e.removeEventListener("click", f, !0), e.removeEventListener("contextmenu", f, !0));
      };
    }
  }, [x, r, t, l, W]);
}
export {
  P as default
};
