import constant_default from "./constant.js";
import { abs, acos, asin, atan2, cos, epsilon, halfPi, max, min, pi, sin, sqrt, tau } from "./math.js";
import { withPath } from "./path.js";
function arcInnerRadius(e) {
	return e.innerRadius;
}
function arcOuterRadius(e) {
	return e.outerRadius;
}
function arcStartAngle(e) {
	return e.startAngle;
}
function arcEndAngle(e) {
	return e.endAngle;
}
function arcPadAngle(e) {
	return e && e.padAngle;
}
function intersect(e, t, n, r, i, a, o, s) {
	var c = n - e, l = r - t, u = o - i, d = s - a, f = d * c - u * l;
	if (!(f * f < 1e-12)) return f = (u * (t - a) - d * (e - i)) / f, [e + f * c, t + f * l];
}
function cornerTangents(e, t, n, r, i, a, o) {
	var s = e - n, l = t - r, u = (o ? a : -a) / sqrt(s * s + l * l), d = u * l, p = -u * s, m = e + d, h = t + p, g = n + d, _ = r + p, v = (m + g) / 2, y = (h + _) / 2, b = g - m, x = _ - h, S = b * b + x * x, C = i - a, w = m * _ - g * h, T = (x < 0 ? -1 : 1) * sqrt(max(0, C * C * S - w * w)), E = (w * x - b * T) / S, D = (-w * b - x * T) / S, O = (w * x + b * T) / S, k = (-w * b + x * T) / S, A = E - v, j = D - y, M = O - v, N = k - y;
	return A * A + j * j > M * M + N * N && (E = O, D = k), {
		cx: E,
		cy: D,
		x01: -d,
		y01: -p,
		x11: E * (i / C - 1),
		y11: D * (i / C - 1)
	};
}
function arc_default() {
	var o = arcInnerRadius, c = arcOuterRadius, S = constant_default(0), C = null, w = arcStartAngle, T = arcEndAngle, E = arcPadAngle, D = null, O = withPath(k);
	function k() {
		var e, m, h = +o.apply(this, arguments), g = +c.apply(this, arguments), _ = w.apply(this, arguments) - halfPi, v = T.apply(this, arguments) - halfPi, y = abs(v - _), k = v > _;
		if (D ||= e = O(), g < h && (m = g, g = h, h = m), !(g > 1e-12)) D.moveTo(0, 0);
		else if (y > tau - 1e-12) D.moveTo(g * cos(_), g * sin(_)), D.arc(0, 0, g, _, v, !k), h > 1e-12 && (D.moveTo(h * cos(v), h * sin(v)), D.arc(0, 0, h, v, _, k));
		else {
			var A = _, j = v, M = _, N = v, P = y, F = y, I = E.apply(this, arguments) / 2, L = I > 1e-12 && (C ? +C.apply(this, arguments) : sqrt(h * h + g * g)), R = min(abs(g - h) / 2, +S.apply(this, arguments)), z = R, B = R, V, H;
			if (L > 1e-12) {
				var U = asin(L / h * sin(I)), W = asin(L / g * sin(I));
				(P -= U * 2) > 1e-12 ? (U *= k ? 1 : -1, M += U, N -= U) : (P = 0, M = N = (_ + v) / 2), (F -= W * 2) > 1e-12 ? (W *= k ? 1 : -1, A += W, j -= W) : (F = 0, A = j = (_ + v) / 2);
			}
			var G = g * cos(A), K = g * sin(A), q = h * cos(N), J = h * sin(N);
			if (R > 1e-12) {
				var Y = g * cos(j), X = g * sin(j), Z = h * cos(M), Q = h * sin(M), $;
				if (y < pi) if ($ = intersect(G, K, Z, Q, Y, X, q, J)) {
					var ee = G - $[0], te = K - $[1], ne = Y - $[0], re = X - $[1], ie = 1 / sin(acos((ee * ne + te * re) / (sqrt(ee * ee + te * te) * sqrt(ne * ne + re * re))) / 2), ae = sqrt($[0] * $[0] + $[1] * $[1]);
					z = min(R, (h - ae) / (ie - 1)), B = min(R, (g - ae) / (ie + 1));
				} else z = B = 0;
			}
			F > 1e-12 ? B > 1e-12 ? (V = cornerTangents(Z, Q, G, K, g, B, k), H = cornerTangents(Y, X, q, J, g, B, k), D.moveTo(V.cx + V.x01, V.cy + V.y01), B < R ? D.arc(V.cx, V.cy, B, atan2(V.y01, V.x01), atan2(H.y01, H.x01), !k) : (D.arc(V.cx, V.cy, B, atan2(V.y01, V.x01), atan2(V.y11, V.x11), !k), D.arc(0, 0, g, atan2(V.cy + V.y11, V.cx + V.x11), atan2(H.cy + H.y11, H.cx + H.x11), !k), D.arc(H.cx, H.cy, B, atan2(H.y11, H.x11), atan2(H.y01, H.x01), !k))) : (D.moveTo(G, K), D.arc(0, 0, g, A, j, !k)) : D.moveTo(G, K), !(h > 1e-12) || !(P > 1e-12) ? D.lineTo(q, J) : z > 1e-12 ? (V = cornerTangents(q, J, Y, X, h, -z, k), H = cornerTangents(G, K, Z, Q, h, -z, k), D.lineTo(V.cx + V.x01, V.cy + V.y01), z < R ? D.arc(V.cx, V.cy, z, atan2(V.y01, V.x01), atan2(H.y01, H.x01), !k) : (D.arc(V.cx, V.cy, z, atan2(V.y01, V.x01), atan2(V.y11, V.x11), !k), D.arc(0, 0, h, atan2(V.cy + V.y11, V.cx + V.x11), atan2(H.cy + H.y11, H.cx + H.x11), k), D.arc(H.cx, H.cy, z, atan2(H.y11, H.x11), atan2(H.y01, H.x01), !k))) : D.arc(0, 0, h, N, M, k);
		}
		if (D.closePath(), e) return D = null, e + "" || null;
	}
	return k.centroid = function() {
		var e = (+o.apply(this, arguments) + +c.apply(this, arguments)) / 2, t = (+w.apply(this, arguments) + +T.apply(this, arguments)) / 2 - pi / 2;
		return [cos(t) * e, sin(t) * e];
	}, k.innerRadius = function(t) {
		return arguments.length ? (o = typeof t == "function" ? t : constant_default(+t), k) : o;
	}, k.outerRadius = function(t) {
		return arguments.length ? (c = typeof t == "function" ? t : constant_default(+t), k) : c;
	}, k.cornerRadius = function(t) {
		return arguments.length ? (S = typeof t == "function" ? t : constant_default(+t), k) : S;
	}, k.padRadius = function(t) {
		return arguments.length ? (C = t == null ? null : typeof t == "function" ? t : constant_default(+t), k) : C;
	}, k.startAngle = function(t) {
		return arguments.length ? (w = typeof t == "function" ? t : constant_default(+t), k) : w;
	}, k.endAngle = function(t) {
		return arguments.length ? (T = typeof t == "function" ? t : constant_default(+t), k) : T;
	}, k.padAngle = function(t) {
		return arguments.length ? (E = typeof t == "function" ? t : constant_default(+t), k) : E;
	}, k.context = function(e) {
		return arguments.length ? (D = e ?? null, k) : D;
	}, k;
}
export { arc_default as default };
