import e from "../../d3-dispatch/src/dispatch.js";
import t from "../../d3-selection/src/select.js";
import n from "../../d3-selection/src/pointer.js";
import r, { yesdrag as i } from "../../d3-drag/src/nodrag.js";
import a from "../../d3-interpolate/src/zoom.js";
import o from "../../d3-transition/src/interrupt.js";
import "../../d3-transition/src/index.js";
import s from "./constant.js";
import c from "./event.js";
import { Transform as l, identity as u } from "./transform.js";
import d, { nopropagation as f } from "./noevent.js";
//#region ../../node_modules/d3-zoom/src/zoom.js
function p(e) {
	return (!e.ctrlKey || e.type === "wheel") && !e.button;
}
function m() {
	var e = this;
	return e instanceof SVGElement ? (e = e.ownerSVGElement || e, e.hasAttribute("viewBox") ? (e = e.viewBox.baseVal, [[e.x, e.y], [e.x + e.width, e.y + e.height]]) : [[0, 0], [e.width.baseVal.value, e.height.baseVal.value]]) : [[0, 0], [e.clientWidth, e.clientHeight]];
}
function h() {
	return this.__zoom || u;
}
function g(e) {
	return -e.deltaY * (e.deltaMode === 1 ? .05 : e.deltaMode ? 1 : .002) * (e.ctrlKey ? 10 : 1);
}
function _() {
	return navigator.maxTouchPoints || "ontouchstart" in this;
}
function v(e, t, n) {
	var r = e.invertX(t[0][0]) - n[0][0], i = e.invertX(t[1][0]) - n[1][0], a = e.invertY(t[0][1]) - n[0][1], o = e.invertY(t[1][1]) - n[1][1];
	return e.translate(i > r ? (r + i) / 2 : Math.min(0, r) || Math.max(0, i), o > a ? (a + o) / 2 : Math.min(0, a) || Math.max(0, o));
}
function y() {
	var y = p, b = m, x = v, S = g, C = _, w = [0, Infinity], T = [[-Infinity, -Infinity], [Infinity, Infinity]], E = 250, D = a, O = e("start", "zoom", "end"), k, A, j, M = 500, N = 150, P = 0, F = 10;
	function I(e) {
		e.property("__zoom", h).on("wheel.zoom", U, { passive: !1 }).on("mousedown.zoom", W).on("dblclick.zoom", G).filter(C).on("touchstart.zoom", K).on("touchmove.zoom", q).on("touchend.zoom touchcancel.zoom", J).style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
	}
	I.transform = function(e, t, n, r) {
		var i = e.selection ? e.selection() : e;
		i.property("__zoom", h), e === i ? i.interrupt().each(function() {
			V(this, arguments).event(r).start().zoom(null, typeof t == "function" ? t.apply(this, arguments) : t).end();
		}) : B(e, t, n, r);
	}, I.scaleBy = function(e, t, n, r) {
		I.scaleTo(e, function() {
			return this.__zoom.k * (typeof t == "function" ? t.apply(this, arguments) : t);
		}, n, r);
	}, I.scaleTo = function(e, t, n, r) {
		I.transform(e, function() {
			var e = b.apply(this, arguments), r = this.__zoom, i = n == null ? z(e) : typeof n == "function" ? n.apply(this, arguments) : n, a = r.invert(i), o = typeof t == "function" ? t.apply(this, arguments) : t;
			return x(R(L(r, o), i, a), e, T);
		}, n, r);
	}, I.translateBy = function(e, t, n, r) {
		I.transform(e, function() {
			return x(this.__zoom.translate(typeof t == "function" ? t.apply(this, arguments) : t, typeof n == "function" ? n.apply(this, arguments) : n), b.apply(this, arguments), T);
		}, null, r);
	}, I.translateTo = function(e, t, n, r, i) {
		I.transform(e, function() {
			var e = b.apply(this, arguments), i = this.__zoom, a = r == null ? z(e) : typeof r == "function" ? r.apply(this, arguments) : r;
			return x(u.translate(a[0], a[1]).scale(i.k).translate(typeof t == "function" ? -t.apply(this, arguments) : -t, typeof n == "function" ? -n.apply(this, arguments) : -n), e, T);
		}, r, i);
	};
	function L(e, t) {
		return t = Math.max(w[0], Math.min(w[1], t)), t === e.k ? e : new l(t, e.x, e.y);
	}
	function R(e, t, n) {
		var r = t[0] - n[0] * e.k, i = t[1] - n[1] * e.k;
		return r === e.x && i === e.y ? e : new l(e.k, r, i);
	}
	function z(e) {
		return [(+e[0][0] + +e[1][0]) / 2, (+e[0][1] + +e[1][1]) / 2];
	}
	function B(e, t, n, r) {
		e.on("start.zoom", function() {
			V(this, arguments).event(r).start();
		}).on("interrupt.zoom end.zoom", function() {
			V(this, arguments).event(r).end();
		}).tween("zoom", function() {
			var e = this, i = arguments, a = V(e, i).event(r), o = b.apply(e, i), s = n == null ? z(o) : typeof n == "function" ? n.apply(e, i) : n, c = Math.max(o[1][0] - o[0][0], o[1][1] - o[0][1]), u = e.__zoom, d = typeof t == "function" ? t.apply(e, i) : t, f = D(u.invert(s).concat(c / u.k), d.invert(s).concat(c / d.k));
			return function(e) {
				if (e === 1) e = d;
				else {
					var t = f(e), n = c / t[2];
					e = new l(n, s[0] - t[0] * n, s[1] - t[1] * n);
				}
				a.zoom(null, e);
			};
		});
	}
	function V(e, t, n) {
		return !n && e.__zooming || new H(e, t);
	}
	function H(e, t) {
		this.that = e, this.args = t, this.active = 0, this.sourceEvent = null, this.extent = b.apply(e, t), this.taps = 0;
	}
	H.prototype = {
		event: function(e) {
			return e && (this.sourceEvent = e), this;
		},
		start: function() {
			return ++this.active === 1 && (this.that.__zooming = this, this.emit("start")), this;
		},
		zoom: function(e, t) {
			return this.mouse && e !== "mouse" && (this.mouse[1] = t.invert(this.mouse[0])), this.touch0 && e !== "touch" && (this.touch0[1] = t.invert(this.touch0[0])), this.touch1 && e !== "touch" && (this.touch1[1] = t.invert(this.touch1[0])), this.that.__zoom = t, this.emit("zoom"), this;
		},
		end: function() {
			return --this.active === 0 && (delete this.that.__zooming, this.emit("end")), this;
		},
		emit: function(e) {
			var n = t(this.that).datum();
			O.call(e, this.that, new c(e, {
				sourceEvent: this.sourceEvent,
				target: I,
				type: e,
				transform: this.that.__zoom,
				dispatch: O
			}), n);
		}
	};
	function U(e, ...t) {
		if (!y.apply(this, arguments)) return;
		var r = V(this, t).event(e), i = this.__zoom, a = Math.max(w[0], Math.min(w[1], i.k * 2 ** S.apply(this, arguments))), s = n(e);
		if (r.wheel) (r.mouse[0][0] !== s[0] || r.mouse[0][1] !== s[1]) && (r.mouse[1] = i.invert(r.mouse[0] = s)), clearTimeout(r.wheel);
		else if (i.k === a) return;
		else r.mouse = [s, i.invert(s)], o(this), r.start();
		d(e), r.wheel = setTimeout(c, N), r.zoom("mouse", x(R(L(i, a), r.mouse[0], r.mouse[1]), r.extent, T));
		function c() {
			r.wheel = null, r.end();
		}
	}
	function W(e, ...a) {
		if (j || !y.apply(this, arguments)) return;
		var s = e.currentTarget, c = V(this, a, !0).event(e), l = t(e.view).on("mousemove.zoom", h, !0).on("mouseup.zoom", g, !0), u = n(e, s), p = e.clientX, m = e.clientY;
		r(e.view), f(e), c.mouse = [u, this.__zoom.invert(u)], o(this), c.start();
		function h(e) {
			if (d(e), !c.moved) {
				var t = e.clientX - p, r = e.clientY - m;
				c.moved = t * t + r * r > P;
			}
			c.event(e).zoom("mouse", x(R(c.that.__zoom, c.mouse[0] = n(e, s), c.mouse[1]), c.extent, T));
		}
		function g(e) {
			l.on("mousemove.zoom mouseup.zoom", null), i(e.view, c.moved), d(e), c.event(e).end();
		}
	}
	function G(e, ...r) {
		if (y.apply(this, arguments)) {
			var i = this.__zoom, a = n(e.changedTouches ? e.changedTouches[0] : e, this), o = i.invert(a), s = i.k * (e.shiftKey ? .5 : 2), c = x(R(L(i, s), a, o), b.apply(this, r), T);
			d(e), E > 0 ? t(this).transition().duration(E).call(B, c, a, e) : t(this).call(I.transform, c, a, e);
		}
	}
	function K(e, ...t) {
		if (y.apply(this, arguments)) {
			var r = e.touches, i = r.length, a = V(this, t, e.changedTouches.length === i).event(e), s, c, l, u;
			for (f(e), c = 0; c < i; ++c) l = r[c], u = n(l, this), u = [
				u,
				this.__zoom.invert(u),
				l.identifier
			], a.touch0 ? !a.touch1 && a.touch0[2] !== u[2] && (a.touch1 = u, a.taps = 0) : (a.touch0 = u, s = !0, a.taps = 1 + !!k);
			k &&= clearTimeout(k), s && (a.taps < 2 && (A = u[0], k = setTimeout(function() {
				k = null;
			}, M)), o(this), a.start());
		}
	}
	function q(e, ...t) {
		if (this.__zooming) {
			var r = V(this, t).event(e), i = e.changedTouches, a = i.length, o, s, c, l;
			for (d(e), o = 0; o < a; ++o) s = i[o], c = n(s, this), r.touch0 && r.touch0[2] === s.identifier ? r.touch0[0] = c : r.touch1 && r.touch1[2] === s.identifier && (r.touch1[0] = c);
			if (s = r.that.__zoom, r.touch1) {
				var u = r.touch0[0], f = r.touch0[1], p = r.touch1[0], m = r.touch1[1], h = (h = p[0] - u[0]) * h + (h = p[1] - u[1]) * h, g = (g = m[0] - f[0]) * g + (g = m[1] - f[1]) * g;
				s = L(s, Math.sqrt(h / g)), c = [(u[0] + p[0]) / 2, (u[1] + p[1]) / 2], l = [(f[0] + m[0]) / 2, (f[1] + m[1]) / 2];
			} else if (r.touch0) c = r.touch0[0], l = r.touch0[1];
			else return;
			r.zoom("touch", x(R(s, c, l), r.extent, T));
		}
	}
	function J(e, ...r) {
		if (this.__zooming) {
			var i = V(this, r).event(e), a = e.changedTouches, o = a.length, s, c;
			for (f(e), j && clearTimeout(j), j = setTimeout(function() {
				j = null;
			}, M), s = 0; s < o; ++s) c = a[s], i.touch0 && i.touch0[2] === c.identifier ? delete i.touch0 : i.touch1 && i.touch1[2] === c.identifier && delete i.touch1;
			if (i.touch1 && !i.touch0 && (i.touch0 = i.touch1, delete i.touch1), i.touch0) i.touch0[1] = this.__zoom.invert(i.touch0[0]);
			else if (i.end(), i.taps === 2 && (c = n(c, this), Math.hypot(A[0] - c[0], A[1] - c[1]) < F)) {
				var l = t(this).on("dblclick.zoom");
				l && l.apply(this, arguments);
			}
		}
	}
	return I.wheelDelta = function(e) {
		return arguments.length ? (S = typeof e == "function" ? e : s(+e), I) : S;
	}, I.filter = function(e) {
		return arguments.length ? (y = typeof e == "function" ? e : s(!!e), I) : y;
	}, I.touchable = function(e) {
		return arguments.length ? (C = typeof e == "function" ? e : s(!!e), I) : C;
	}, I.extent = function(e) {
		return arguments.length ? (b = typeof e == "function" ? e : s([[+e[0][0], +e[0][1]], [+e[1][0], +e[1][1]]]), I) : b;
	}, I.scaleExtent = function(e) {
		return arguments.length ? (w[0] = +e[0], w[1] = +e[1], I) : [w[0], w[1]];
	}, I.translateExtent = function(e) {
		return arguments.length ? (T[0][0] = +e[0][0], T[1][0] = +e[1][0], T[0][1] = +e[0][1], T[1][1] = +e[1][1], I) : [[T[0][0], T[0][1]], [T[1][0], T[1][1]]];
	}, I.constrain = function(e) {
		return arguments.length ? (x = e, I) : x;
	}, I.duration = function(e) {
		return arguments.length ? (E = +e, I) : E;
	}, I.interpolate = function(e) {
		return arguments.length ? (D = e, I) : D;
	}, I.on = function() {
		var e = O.on.apply(O, arguments);
		return e === O ? I : e;
	}, I.clickDistance = function(e) {
		return arguments.length ? (P = (e = +e) * e, I) : Math.sqrt(P);
	}, I.tapDistance = function(e) {
		return arguments.length ? (F = +e, I) : F;
	}, I;
}
//#endregion
export { y as default };
