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 noevent_default, { nonpassive, nonpassivecapture, nopropagation } from "./noevent.js";
import nodrag_default, { yesdrag } from "./nodrag.js";
import constant_default from "./constant.js";
import DragEvent from "./event.js";
function defaultFilter(t) {
	return !t.ctrlKey && !t.button;
}
function defaultContainer() {
	return this.parentNode;
}
function defaultSubject(t, n) {
	return n ?? {
		x: t.x,
		y: t.y
	};
}
function defaultTouchable() {
	return navigator.maxTouchPoints || "ontouchstart" in this;
}
function drag_default() {
	var h = defaultFilter, g = defaultContainer, _ = defaultSubject, v = defaultTouchable, y = {}, b = dispatch_default("start", "drag", "end"), x = 0, S, C, w, T, E = 0;
	function D(t) {
		t.on("mousedown.drag", O).filter(v).on("touchstart.drag", j).on("touchmove.drag", M, nonpassive).on("touchend.drag touchcancel.drag", N).style("touch-action", "none").style("-webkit-tap-highlight-color", "rgba(0,0,0,0)");
	}
	function O(t, r) {
		if (!(T || !h.call(this, t, r))) {
			var i = P(this, g.call(this, t, r), t, r, "mouse");
			i && (select_default(t.view).on("mousemove.drag", k, nonpassivecapture).on("mouseup.drag", A, nonpassivecapture), nodrag_default(t.view), nopropagation(t), w = !1, S = t.clientX, C = t.clientY, i("start", t));
		}
	}
	function k(t) {
		if (noevent_default(t), !w) {
			var n = t.clientX - S, r = t.clientY - C;
			w = n * n + r * r > E;
		}
		y.mouse("drag", t);
	}
	function A(t) {
		select_default(t.view).on("mousemove.drag mouseup.drag", null), yesdrag(t.view, w), noevent_default(t), y.mouse("end", t);
	}
	function j(t, n) {
		if (h.call(this, t, n)) {
			var r = t.changedTouches, i = g.call(this, t, n), a = r.length, o, c;
			for (o = 0; o < a; ++o) (c = P(this, i, t, n, r[o].identifier, r[o])) && (nopropagation(t), c("start", t, r[o]));
		}
	}
	function M(t) {
		var n = t.changedTouches, r = n.length, a, o;
		for (a = 0; a < r; ++a) (o = y[n[a].identifier]) && (noevent_default(t), o("drag", t, n[a]));
	}
	function N(t) {
		var n = t.changedTouches, r = n.length, i, a;
		for (T && clearTimeout(T), T = setTimeout(function() {
			T = null;
		}, 500), i = 0; i < r; ++i) (a = y[n[i].identifier]) && (nopropagation(t), a("end", t, n[i]));
	}
	function P(t, n, i, a, o, s) {
		var c = b.copy(), l = pointer_default(s || i, n), u, d, f;
		if ((f = _.call(t, new DragEvent("beforestart", {
			sourceEvent: i,
			target: D,
			identifier: o,
			active: x,
			x: l[0],
			y: l[1],
			dx: 0,
			dy: 0,
			dispatch: c
		}), a)) != null) return u = f.x - l[0] || 0, d = f.y - l[1] || 0, function i(s, p, m) {
			var h = l, g;
			switch (s) {
				case "start":
					y[o] = i, g = x++;
					break;
				case "end": delete y[o], --x;
				case "drag":
					l = pointer_default(m || p, n), g = x;
					break;
			}
			c.call(s, t, new DragEvent(s, {
				sourceEvent: p,
				subject: f,
				target: D,
				identifier: o,
				active: g,
				x: l[0] + u,
				y: l[1] + d,
				dx: l[0] - h[0],
				dy: l[1] - h[1],
				dispatch: c
			}), a);
		};
	}
	return D.filter = function(t) {
		return arguments.length ? (h = typeof t == "function" ? t : constant_default(!!t), D) : h;
	}, D.container = function(t) {
		return arguments.length ? (g = typeof t == "function" ? t : constant_default(t), D) : g;
	}, D.subject = function(t) {
		return arguments.length ? (_ = typeof t == "function" ? t : constant_default(t), D) : _;
	}, D.touchable = function(t) {
		return arguments.length ? (v = typeof t == "function" ? t : constant_default(!!t), D) : v;
	}, D.on = function() {
		var t = b.on.apply(b, arguments);
		return t === b ? D : t;
	}, D.clickDistance = function(t) {
		return arguments.length ? (E = (t = +t) * t, D) : Math.sqrt(E);
	}, D;
}
export { drag_default as default };
