import R from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import H from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { useState as I, useRef as O, useEffect as P } from "react";
import c from "../../../rc-util/es/Dom/addEventListener.mjs";
import { warning as j } from "../../../rc-util/es/warning.mjs";
import B from "../getFixScaleEleTransPosition.mjs";
import { BASE_SCALE_RATIO as T, WHEEL_MAX_SCALE_RATIO as F } from "../previewConfig.mjs";
function Q(d, m, u, U, f, Y, h) {
  var g = f.rotate, _ = f.scale, p = f.x, v = f.y, W = I(!1), X = H(W, 2), l = X[0], E = X[1], M = O({
    diffX: 0,
    diffY: 0,
    transformX: 0,
    transformY: 0
  }), y = function(o) {
    !m || o.button !== 0 || (o.preventDefault(), o.stopPropagation(), M.current = {
      diffX: o.pageX - p,
      diffY: o.pageY - v,
      transformX: p,
      transformY: v
    }, E(!0));
  }, w = function(o) {
    u && l && Y({
      x: o.pageX - M.current.diffX,
      y: o.pageY - M.current.diffY
    }, "move");
  }, L = function() {
    if (u && l) {
      E(!1);
      var o = M.current, n = o.transformX, t = o.transformY, e = p !== n && v !== t;
      if (!e)
        return;
      var a = d.current.offsetWidth * _, i = d.current.offsetHeight * _, s = d.current.getBoundingClientRect(), D = s.left, C = s.top, S = g % 180 !== 0, A = B(S ? i : a, S ? a : i, D, C);
      A && Y(R({}, A), "dragRebound");
    }
  }, x = function(o) {
    if (!(!u || o.deltaY == 0)) {
      var n = Math.abs(o.deltaY / 100), t = Math.min(n, F), e = T + t * U;
      o.deltaY > 0 && (e = T / e), h(e, "wheel", o.clientX, o.clientY);
    }
  };
  return P(function() {
    var r, o, n, t;
    if (m) {
      n = c(window, "mouseup", L, !1), t = c(window, "mousemove", w, !1);
      try {
        window.top !== window.self && (r = c(window.top, "mouseup", L, !1), o = c(window.top, "mousemove", w, !1));
      } catch (e) {
        j(!1, "[rc-image] ".concat(e));
      }
    }
    return function() {
      var e, a, i, s;
      (e = n) === null || e === void 0 || e.remove(), (a = t) === null || a === void 0 || a.remove(), (i = r) === null || i === void 0 || i.remove(), (s = o) === null || s === void 0 || s.remove();
    };
  }, [u, l, p, v, g, m]), {
    isMoving: l,
    onMouseDown: y,
    onMouseMove: w,
    onMouseUp: L,
    onWheel: x
  };
}
export {
  Q as default
};
