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