import _ from "../../../@babel/runtime/helpers/esm/objectSpread2.mjs";
import I from "../../../@babel/runtime/helpers/esm/slicedToArray.mjs";
import { useState as F, useRef as H, useEffect as k } from "react";
import q from "../../../rc-util/es/Dom/addEventListener.mjs";
import z from "../getFixScaleEleTransPosition.mjs";
function l(o, n) {
  var u = o.x - n.x, i = o.y - n.y;
  return Math.hypot(u, i);
}
function G(o, n, u, i) {
  var r = l(o, u), a = l(n, i);
  if (r === 0 && a === 0)
    return [o.x, o.y];
  var x = r / (r + a), g = o.x + x * (n.x - o.x), f = o.y + x * (n.y - o.y);
  return [g, f];
}
function U(o, n, u, i, r, a, x) {
  var g = r.rotate, f = r.scale, Z = r.x, $ = r.y, L = F(!1), E = I(L, 2), M = E[0], X = E[1], m = H({
    point1: {
      x: 0,
      y: 0
    },
    point2: {
      x: 0,
      y: 0
    },
    eventType: "none"
  }), p = function(t) {
    m.current = _(_({}, m.current), t);
  }, R = function(t) {
    if (n) {
      t.stopPropagation(), X(!0);
      var c = t.touches, e = c === void 0 ? [] : c;
      e.length > 1 ? p({
        point1: {
          x: e[0].clientX,
          y: e[0].clientY
        },
        point2: {
          x: e[1].clientX,
          y: e[1].clientY
        },
        eventType: "touchZoom"
      }) : p({
        point1: {
          x: e[0].clientX - Z,
          y: e[0].clientY - $
        },
        eventType: "move"
      });
    }
  }, B = function(t) {
    var c = t.touches, e = c === void 0 ? [] : c, s = m.current, h = s.point1, y = s.point2, T = s.eventType;
    if (e.length > 1 && T === "touchZoom") {
      var d = {
        x: e[0].clientX,
        y: e[0].clientY
      }, S = {
        x: e[1].clientX,
        y: e[1].clientY
      }, W = G(h, y, d, S), Y = I(W, 2), j = Y[0], A = Y[1], C = l(d, S) / l(h, y);
      x(C, "touchZoom", j, A, !0), p({
        point1: d,
        point2: S,
        eventType: "touchZoom"
      });
    } else
      T === "move" && (a({
        x: e[0].clientX - h.x,
        y: e[0].clientY - h.y
      }, "move"), p({
        eventType: "move"
      }));
  }, D = function() {
    if (u) {
      if (M && X(!1), p({
        eventType: "none"
      }), i > f)
        return a({
          x: 0,
          y: 0,
          scale: i
        }, "touchZoom");
      var t = o.current.offsetWidth * f, c = o.current.offsetHeight * f, e = o.current.getBoundingClientRect(), s = e.left, h = e.top, y = g % 180 !== 0, T = z(y ? c : t, y ? t : c, s, h);
      T && a(_({}, T), "dragRebound");
    }
  };
  return k(function() {
    var v;
    return u && n && (v = q(window, "touchmove", function(t) {
      return t.preventDefault();
    }, {
      passive: !1
    })), function() {
      var t;
      (t = v) === null || t === void 0 || t.remove();
    };
  }, [u, n]), {
    isTouching: M,
    onTouchStart: R,
    onTouchMove: B,
    onTouchEnd: D
  };
}
export {
  U as default
};
