import dispatch_default from "../../d3-dispatch/src/dispatch.js";
import select_default from "../../d3-selection/src/select.js";
import pointer_default from "../../d3-selection/src/pointer.js";
import nodrag_default, { yesdrag } from "../../d3-drag/src/nodrag.js";
import zoom_default from "../../d3-interpolate/src/zoom.js";
import interrupt_default from "../../d3-transition/src/interrupt.js";
import "../../d3-transition/src/index.js";
import constant_default from "./constant.js";
import ZoomEvent from "./event.js";
import { Transform, identity } from "./transform.js";
import noevent_default, { nopropagation } from "./noevent.js";
function defaultFilter(e) {
	return (!e.ctrlKey || e.type === "wheel") && !e.button;
}
function defaultExtent() {
	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 defaultTransform() {
	return this.__zoom || identity;
}
function defaultWheelDelta(e) {
	return -e.deltaY * (e.deltaMode === 1 ? .05 : e.deltaMode ? 1 : .002) * (e.ctrlKey ? 10 : 1);
}
function defaultTouchable() {
	return navigator.maxTouchPoints || "ontouchstart" in this;
}
function defaultConstrain(e, r, i) {
	var a = e.invertX(r[0][0]) - i[0][0], o = e.invertX(r[1][0]) - i[1][0], s = e.invertY(r[0][1]) - i[0][1], c = e.invertY(r[1][1]) - i[1][1];
	return e.translate(o > a ? (a + o) / 2 : Math.min(0, a) || Math.max(0, o), c > s ? (s + c) / 2 : Math.min(0, s) || Math.max(0, c));
}
function zoom_default$1() {
	var y = defaultFilter, b = defaultExtent, x = defaultConstrain, S = defaultWheelDelta, C = defaultTouchable, w = [0, Infinity], T = [[-Infinity, -Infinity], [Infinity, Infinity]], E = 250, D = zoom_default, O = dispatch_default("start", "zoom", "end"), k, A, j, M = 500, N = 150, P = 0, F = 10;
	function I(e) {
		e.property("__zoom", defaultTransform).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, r, i, a) {
		var o = e.selection ? e.selection() : e;
		o.property("__zoom", defaultTransform), e === o ? o.interrupt().each(function() {
			V(this, arguments).event(a).start().zoom(null, typeof r == "function" ? r.apply(this, arguments) : r).end();
		}) : B(e, r, i, a);
	}, I.scaleBy = function(e, r, i, a) {
		I.scaleTo(e, function() {
			return this.__zoom.k * (typeof r == "function" ? r.apply(this, arguments) : r);
		}, i, a);
	}, I.scaleTo = function(e, r, i, a) {
		I.transform(e, function() {
			var e = b.apply(this, arguments), a = this.__zoom, o = i == null ? z(e) : typeof i == "function" ? i.apply(this, arguments) : i, s = a.invert(o), c = typeof r == "function" ? r.apply(this, arguments) : r;
			return x(R(L(a, c), o, s), e, T);
		}, i, a);
	}, I.translateBy = function(e, r, i, a) {
		I.transform(e, function() {
			return x(this.__zoom.translate(typeof r == "function" ? r.apply(this, arguments) : r, typeof i == "function" ? i.apply(this, arguments) : i), b.apply(this, arguments), T);
		}, null, a);
	}, I.translateTo = function(e, r, i, a, o) {
		I.transform(e, function() {
			var e = b.apply(this, arguments), o = this.__zoom, s = a == null ? z(e) : typeof a == "function" ? a.apply(this, arguments) : a;
			return x(identity.translate(s[0], s[1]).scale(o.k).translate(typeof r == "function" ? -r.apply(this, arguments) : -r, typeof i == "function" ? -i.apply(this, arguments) : -i), e, T);
		}, a, o);
	};
	function L(e, r) {
		return r = Math.max(w[0], Math.min(w[1], r)), r === e.k ? e : new Transform(r, e.x, e.y);
	}
	function R(e, r, i) {
		var a = r[0] - i[0] * e.k, o = r[1] - i[1] * e.k;
		return a === e.x && o === e.y ? e : new Transform(e.k, a, o);
	}
	function z(e) {
		return [(+e[0][0] + +e[1][0]) / 2, (+e[0][1] + +e[1][1]) / 2];
	}
	function B(e, r, i, a) {
		e.on("start.zoom", function() {
			V(this, arguments).event(a).start();
		}).on("interrupt.zoom end.zoom", function() {
			V(this, arguments).event(a).end();
		}).tween("zoom", function() {
			var e = this, o = arguments, s = V(e, o).event(a), c = b.apply(e, o), l = i == null ? z(c) : typeof i == "function" ? i.apply(e, o) : i, u = Math.max(c[1][0] - c[0][0], c[1][1] - c[0][1]), f = e.__zoom, p = typeof r == "function" ? r.apply(e, o) : r, m = D(f.invert(l).concat(u / f.k), p.invert(l).concat(u / p.k));
			return function(e) {
				if (e === 1) e = p;
				else {
					var r = m(e), i = u / r[2];
					e = new Transform(i, l[0] - r[0] * i, l[1] - r[1] * i);
				}
				s.zoom(null, e);
			};
		});
	}
	function V(e, r, i) {
		return !i && e.__zooming || new H(e, r);
	}
	function H(e, r) {
		this.that = e, this.args = r, this.active = 0, this.sourceEvent = null, this.extent = b.apply(e, r), 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, r) {
			return this.mouse && e !== "mouse" && (this.mouse[1] = r.invert(this.mouse[0])), this.touch0 && e !== "touch" && (this.touch0[1] = r.invert(this.touch0[0])), this.touch1 && e !== "touch" && (this.touch1[1] = r.invert(this.touch1[0])), this.that.__zoom = r, this.emit("zoom"), this;
		},
		end: function() {
			return --this.active === 0 && (delete this.that.__zooming, this.emit("end")), this;
		},
		emit: function(e) {
			var i = select_default(this.that).datum();
			O.call(e, this.that, new ZoomEvent(e, {
				sourceEvent: this.sourceEvent,
				target: I,
				type: e,
				transform: this.that.__zoom,
				dispatch: O
			}), i);
		}
	};
	function U(e, ...r) {
		if (!y.apply(this, arguments)) return;
		var a = V(this, r).event(e), o = this.__zoom, s = Math.max(w[0], Math.min(w[1], o.k * 2 ** S.apply(this, arguments))), l = pointer_default(e);
		if (a.wheel) (a.mouse[0][0] !== l[0] || a.mouse[0][1] !== l[1]) && (a.mouse[1] = o.invert(a.mouse[0] = l)), clearTimeout(a.wheel);
		else if (o.k === s) return;
		else a.mouse = [l, o.invert(l)], interrupt_default(this), a.start();
		noevent_default(e), a.wheel = setTimeout(u, N), a.zoom("mouse", x(R(L(o, s), a.mouse[0], a.mouse[1]), a.extent, T));
		function u() {
			a.wheel = null, a.end();
		}
	}
	function W(e, ...s) {
		if (j || !y.apply(this, arguments)) return;
		var l = e.currentTarget, u = V(this, s, !0).event(e), d = select_default(e.view).on("mousemove.zoom", _, !0).on("mouseup.zoom", v, !0), f = pointer_default(e, l), h = e.clientX, g = e.clientY;
		nodrag_default(e.view), nopropagation(e), u.mouse = [f, this.__zoom.invert(f)], interrupt_default(this), u.start();
		function _(e) {
			if (noevent_default(e), !u.moved) {
				var r = e.clientX - h, a = e.clientY - g;
				u.moved = r * r + a * a > P;
			}
			u.event(e).zoom("mouse", x(R(u.that.__zoom, u.mouse[0] = pointer_default(e, l), u.mouse[1]), u.extent, T));
		}
		function v(e) {
			d.on("mousemove.zoom mouseup.zoom", null), yesdrag(e.view, u.moved), noevent_default(e), u.event(e).end();
		}
	}
	function G(e, ...a) {
		if (y.apply(this, arguments)) {
			var o = this.__zoom, s = pointer_default(e.changedTouches ? e.changedTouches[0] : e, this), c = o.invert(s), l = o.k * (e.shiftKey ? .5 : 2), u = x(R(L(o, l), s, c), b.apply(this, a), T);
			noevent_default(e), E > 0 ? select_default(this).transition().duration(E).call(B, u, s, e) : select_default(this).call(I.transform, u, s, e);
		}
	}
	function K(e, ...r) {
		if (y.apply(this, arguments)) {
			var a = e.touches, o = a.length, s = V(this, r, e.changedTouches.length === o).event(e), l, u, d, f;
			for (nopropagation(e), u = 0; u < o; ++u) d = a[u], f = pointer_default(d, this), f = [
				f,
				this.__zoom.invert(f),
				d.identifier
			], s.touch0 ? !s.touch1 && s.touch0[2] !== f[2] && (s.touch1 = f, s.taps = 0) : (s.touch0 = f, l = !0, s.taps = 1 + !!k);
			k &&= clearTimeout(k), l && (s.taps < 2 && (A = f[0], k = setTimeout(function() {
				k = null;
			}, M)), interrupt_default(this), s.start());
		}
	}
	function q(e, ...r) {
		if (this.__zooming) {
			var a = V(this, r).event(e), o = e.changedTouches, s = o.length, c, l, u, d;
			for (noevent_default(e), c = 0; c < s; ++c) l = o[c], u = pointer_default(l, this), a.touch0 && a.touch0[2] === l.identifier ? a.touch0[0] = u : a.touch1 && a.touch1[2] === l.identifier && (a.touch1[0] = u);
			if (l = a.that.__zoom, a.touch1) {
				var f = a.touch0[0], m = a.touch0[1], h = a.touch1[0], g = a.touch1[1], _ = (_ = h[0] - f[0]) * _ + (_ = h[1] - f[1]) * _, v = (v = g[0] - m[0]) * v + (v = g[1] - m[1]) * v;
				l = L(l, Math.sqrt(_ / v)), u = [(f[0] + h[0]) / 2, (f[1] + h[1]) / 2], d = [(m[0] + g[0]) / 2, (m[1] + g[1]) / 2];
			} else if (a.touch0) u = a.touch0[0], d = a.touch0[1];
			else return;
			a.zoom("touch", x(R(l, u, d), a.extent, T));
		}
	}
	function J(e, ...a) {
		if (this.__zooming) {
			var o = V(this, a).event(e), s = e.changedTouches, c = s.length, l, u;
			for (nopropagation(e), j && clearTimeout(j), j = setTimeout(function() {
				j = null;
			}, M), l = 0; l < c; ++l) u = s[l], o.touch0 && o.touch0[2] === u.identifier ? delete o.touch0 : o.touch1 && o.touch1[2] === u.identifier && delete o.touch1;
			if (o.touch1 && !o.touch0 && (o.touch0 = o.touch1, delete o.touch1), o.touch0) o.touch0[1] = this.__zoom.invert(o.touch0[0]);
			else if (o.end(), o.taps === 2 && (u = pointer_default(u, this), Math.hypot(A[0] - u[0], A[1] - u[1]) < F)) {
				var d = select_default(this).on("dblclick.zoom");
				d && d.apply(this, arguments);
			}
		}
	}
	return I.wheelDelta = function(e) {
		return arguments.length ? (S = typeof e == "function" ? e : constant_default(+e), I) : S;
	}, I.filter = function(e) {
		return arguments.length ? (y = typeof e == "function" ? e : constant_default(!!e), I) : y;
	}, I.touchable = function(e) {
		return arguments.length ? (C = typeof e == "function" ? e : constant_default(!!e), I) : C;
	}, I.extent = function(e) {
		return arguments.length ? (b = typeof e == "function" ? e : constant_default([[+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;
}
export { zoom_default$1 as default };
