import T from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import l from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import it from "../../../@babel/runtime/helpers/esm/objectWithoutProperties.mjs";
import xe from "../../portal/es/Portal.mjs";
import G from "../../../classnames/index.mjs";
import ut from "../../../rc-resize-observer/es/index.mjs";
import { isDOM as Ne } from "../../../rc-util/es/Dom/findDOMNode.mjs";
import { getShadowRoot as Ee } from "../../../rc-util/es/Dom/shadow.mjs";
import M from "../../../rc-util/es/hooks/useEvent.mjs";
import st from "../../../rc-util/es/hooks/useId.mjs";
import R from "../../../rc-util/es/hooks/useLayoutEffect.mjs";
import lt from "../../../rc-util/es/isMobile.mjs";
import * as n from "react";
import ct from "./Popup/index.mjs";
import ft from "./TriggerWrapper.mjs";
import _e from "./context.mjs";
import pt from "./hooks/useAction.mjs";
import mt from "./hooks/useAlign.mjs";
import gt from "./hooks/useWatch.mjs";
import vt from "./hooks/useWinClick.mjs";
import { getAlignPopupClassName as dt, getMotion as ke } from "./util.mjs";
var ht = ["prefixCls", "children", "action", "showAction", "hideAction", "popupVisible", "defaultPopupVisible", "onPopupVisibleChange", "afterPopupVisibleChange", "mouseEnterDelay", "mouseLeaveDelay", "focusDelay", "blurDelay", "mask", "maskClosable", "getPopupContainer", "forceRender", "autoDestroy", "destroyPopupOnHide", "popup", "popupClassName", "popupStyle", "popupPlacement", "builtinPlacements", "popupAlign", "zIndex", "stretch", "getPopupClassNameFromAlign", "fresh", "alignPoint", "onPopupClick", "onPopupAlign", "arrow", "popupMotion", "maskMotion", "popupTransitionName", "popupAnimation", "maskTransitionName", "maskAnimation", "className", "getTriggerDOMNode"];
function Pt() {
  var Oe = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : xe, K = /* @__PURE__ */ n.forwardRef(function(t, Ve) {
    var Q = t.prefixCls, S = Q === void 0 ? "rc-trigger-popup" : Q, Le = t.children, U = t.action, ze = U === void 0 ? "hover" : U, He = t.showAction, Ie = t.hideAction, v = t.popupVisible, Be = t.defaultPopupVisible, V = t.onPopupVisibleChange, L = t.afterPopupVisibleChange, z = t.mouseEnterDelay, Z = t.mouseLeaveDelay, H = Z === void 0 ? 0.1 : Z, Fe = t.focusDelay, We = t.blurDelay, ee = t.mask, oe = t.maskClosable, Xe = oe === void 0 ? !0 : oe, Ye = t.getPopupContainer, je = t.forceRender, Je = t.autoDestroy, qe = t.destroyPopupOnHide, Ge = t.popup, Ke = t.popupClassName, Qe = t.popupStyle, I = t.popupPlacement, te = t.builtinPlacements, d = te === void 0 ? {} : te, ne = t.popupAlign, Ue = t.zIndex, re = t.stretch, D = t.getPopupClassNameFromAlign, Ze = t.fresh, A = t.alignPoint, eo = t.onPopupClick, oo = t.onPopupAlign, B = t.arrow, to = t.popupMotion, no = t.maskMotion, ro = t.popupTransitionName, ao = t.popupAnimation, io = t.maskTransitionName, uo = t.maskAnimation, ae = t.className, so = t.getTriggerDOMNode, F = it(t, ht), lo = Je || qe || !1, co = n.useState(!1), ie = l(co, 2), fo = ie[0], po = ie[1];
    R(function() {
      po(lt());
    }, []);
    var ue = n.useRef({}), g = n.useContext(_e), mo = n.useMemo(function() {
      return {
        registerSubPopup: function(o, r) {
          ue.current[o] = r, g == null || g.registerSubPopup(o, r);
        }
      };
    }, [g]), go = st(), vo = n.useState(null), se = l(vo, 2), f = se[0], ho = se[1], Po = M(function(e) {
      Ne(e) && f !== e && ho(e), g == null || g.registerSubPopup(go, e);
    }), Co = n.useState(null), le = l(Co, 2), m = le[0], Mo = le[1], ce = n.useRef(null), Ro = M(function(e) {
      Ne(e) && m !== e && (Mo(e), ce.current = e);
    }), $ = n.Children.only(Le), c = ($ == null ? void 0 : $.props) || {}, h = {}, So = M(function(e) {
      var o, r, i = m;
      return (i == null ? void 0 : i.contains(e)) || ((o = Ee(i)) === null || o === void 0 ? void 0 : o.host) === e || e === i || (f == null ? void 0 : f.contains(e)) || ((r = Ee(f)) === null || r === void 0 ? void 0 : r.host) === e || e === f || Object.values(ue.current).some(function(a) {
        return (a == null ? void 0 : a.contains(e)) || e === a;
      });
    }), Ao = ke(S, to, ao, ro), yo = ke(S, no, uo, io), bo = n.useState(Be || !1), fe = l(bo, 2), To = fe[0], pe = fe[1], u = v ?? To, Do = M(function(e) {
      v === void 0 && pe(e);
    });
    R(function() {
      pe(v || !1);
    }, [v]);
    var y = n.useRef(u);
    y.current = u;
    var w = n.useRef([]);
    w.current = [];
    var me = M(function(e) {
      var o;
      Do(e), ((o = w.current[w.current.length - 1]) !== null && o !== void 0 ? o : u) !== e && (w.current.push(e), V == null || V(e));
    }), ge = n.useRef(), ve = function() {
      clearTimeout(ge.current);
    }, p = function(o) {
      var r = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0;
      ve(), r === 0 ? me(o) : ge.current = setTimeout(function() {
        me(o);
      }, r * 1e3);
    };
    n.useEffect(function() {
      return ve;
    }, []);
    var $o = n.useState(!1), de = l($o, 2), W = de[0], he = de[1];
    R(function(e) {
      (!e || u) && he(!0);
    }, [u]);
    var wo = n.useState(null), Pe = l(wo, 2), X = Pe[0], Ce = Pe[1], No = n.useState([0, 0]), Me = l(No, 2), Re = Me[0], Eo = Me[1], N = function(o) {
      Eo([o.clientX, o.clientY]);
    }, _o = mt(u, f, A ? Re : m, I, d, ne, oo), s = l(_o, 11), ko = s[0], xo = s[1], Oo = s[2], Vo = s[3], Lo = s[4], zo = s[5], Ho = s[6], Io = s[7], Bo = s[8], E = s[9], Y = s[10], Fo = pt(fo, ze, He, Ie), Se = l(Fo, 2), _ = Se[0], b = Se[1], Ae = _.has("click"), k = b.has("click") || b.has("contextMenu"), P = M(function() {
      W || Y();
    }), Wo = function() {
      y.current && A && k && p(!1);
    };
    gt(u, m, f, P, Wo), R(function() {
      P();
    }, [Re, I]), R(function() {
      u && !(d != null && d[I]) && P();
    }, [JSON.stringify(ne)]);
    var Xo = n.useMemo(function() {
      var e = dt(d, S, E, A);
      return G(e, D == null ? void 0 : D(E));
    }, [E, D, d, S, A]);
    n.useImperativeHandle(Ve, function() {
      return {
        nativeElement: ce.current,
        forceAlign: P
      };
    });
    var Yo = n.useState(0), ye = l(Yo, 2), jo = ye[0], Jo = ye[1], qo = n.useState(0), be = l(qo, 2), Go = be[0], Ko = be[1], Te = function() {
      if (re && m) {
        var o = m.getBoundingClientRect();
        Jo(o.width), Ko(o.height);
      }
    }, Qo = function() {
      Te(), P();
    }, Uo = function(o) {
      he(!1), Y(), L == null || L(o);
    }, Zo = function() {
      return new Promise(function(o) {
        Te(), Ce(function() {
          return o;
        });
      });
    };
    R(function() {
      X && (Y(), X(), Ce(null));
    }, [X]);
    function C(e, o, r, i) {
      h[e] = function(a) {
        var x;
        i == null || i(a), p(o, r);
        for (var q = arguments.length, we = new Array(q > 1 ? q - 1 : 0), O = 1; O < q; O++)
          we[O - 1] = arguments[O];
        (x = c[e]) === null || x === void 0 || x.call.apply(x, [c, a].concat(we));
      };
    }
    (Ae || k) && (h.onClick = function(e) {
      var o;
      y.current && k ? p(!1) : !y.current && Ae && (N(e), p(!0));
      for (var r = arguments.length, i = new Array(r > 1 ? r - 1 : 0), a = 1; a < r; a++)
        i[a - 1] = arguments[a];
      (o = c.onClick) === null || o === void 0 || o.call.apply(o, [c, e].concat(i));
    }), vt(u, k, m, f, ee, Xe, So, p);
    var et = _.has("hover"), ot = b.has("hover"), j, De;
    et && (C("onMouseEnter", !0, z, function(e) {
      N(e);
    }), C("onPointerEnter", !0, z, function(e) {
      N(e);
    }), j = function() {
      (u || W) && p(!0, z);
    }, A && (h.onMouseMove = function(e) {
      var o;
      (o = c.onMouseMove) === null || o === void 0 || o.call(c, e);
    })), ot && (C("onMouseLeave", !1, H), C("onPointerLeave", !1, H), De = function() {
      p(!1, H);
    }), _.has("focus") && C("onFocus", !0, Fe), b.has("focus") && C("onBlur", !1, We), _.has("contextMenu") && (h.onContextMenu = function(e) {
      var o;
      y.current && b.has("contextMenu") ? p(!1) : (N(e), p(!0)), e.preventDefault();
      for (var r = arguments.length, i = new Array(r > 1 ? r - 1 : 0), a = 1; a < r; a++)
        i[a - 1] = arguments[a];
      (o = c.onContextMenu) === null || o === void 0 || o.call.apply(o, [c, e].concat(i));
    }), ae && (h.className = G(c.className, ae));
    var J = T(T({}, c), h), $e = {}, tt = ["onContextMenu", "onClick", "onMouseDown", "onTouchStart", "onMouseEnter", "onMouseLeave", "onFocus", "onBlur"];
    tt.forEach(function(e) {
      F[e] && ($e[e] = function() {
        for (var o, r = arguments.length, i = new Array(r), a = 0; a < r; a++)
          i[a] = arguments[a];
        (o = J[e]) === null || o === void 0 || o.call.apply(o, [J].concat(i)), F[e].apply(F, i);
      });
    });
    var nt = /* @__PURE__ */ n.cloneElement($, T(T({}, J), $e)), rt = {
      x: zo,
      y: Ho
    }, at = B ? T({}, B !== !0 ? B : {}) : null;
    return /* @__PURE__ */ n.createElement(n.Fragment, null, /* @__PURE__ */ n.createElement(ut, {
      disabled: !u,
      ref: Ro,
      onResize: Qo
    }, /* @__PURE__ */ n.createElement(ft, {
      getTriggerDOMNode: so
    }, nt)), /* @__PURE__ */ n.createElement(_e.Provider, {
      value: mo
    }, /* @__PURE__ */ n.createElement(ct, {
      portal: Oe,
      ref: Po,
      prefixCls: S,
      popup: Ge,
      className: G(Ke, Xo),
      style: Qe,
      target: m,
      onMouseEnter: j,
      onMouseLeave: De,
      onPointerEnter: j,
      zIndex: Ue,
      open: u,
      keepDom: W,
      fresh: Ze,
      onClick: eo,
      mask: ee,
      motion: Ao,
      maskMotion: yo,
      onVisibleChanged: Uo,
      onPrepare: Zo,
      forceRender: je,
      autoDestroy: lo,
      getPopupContainer: Ye,
      align: E,
      arrow: at,
      arrowPos: rt,
      ready: ko,
      offsetX: xo,
      offsetY: Oo,
      offsetR: Vo,
      offsetB: Lo,
      onAlign: P,
      stretch: re,
      targetWidth: jo / Io,
      targetHeight: Go / Bo
    })));
  });
  return process.env.NODE_ENV !== "production" && (K.displayName = "Trigger"), K;
}
const Ht = Pt(xe);
export {
  Ht as default,
  Pt as generateTrigger
};
