class y {
  constructor(s, i) {
    this._line = 0, this._point = 0, this._x0 = 0, this._x1 = 0, this._y0 = 0, this._y1 = 0, this._t0 = 0, this._tension = 0, this._context = s, this._tension = i;
  }
  areaStart() {
    this._line = 0;
  }
  areaEnd() {
    this._line = NaN;
  }
  lineStart() {
    this._x0 = this._x1 = this._y0 = this._y1 = this._t0 = NaN, this._point = 0;
  }
  lineEnd() {
    switch (this._point) {
      case 2:
        this._context.lineTo(this._x1, this._y1);
        break;
      case 3:
        r(this, this._t0, x(this, this._t0));
        break;
    }
    (this._line || this._line !== 0 && this._point === 1) && this._context.closePath(), this._line = 1 - this._line;
  }
  point(s, i) {
    [s, i] = [i, s];
    let n = NaN;
    if (s = +s, i = +i, !(s === this._x1 && i === this._y1)) {
      switch (this._point) {
        case 0:
          this._point = 1, this._line ? this._context.lineTo(i, s) : this._context.moveTo(i, s);
          break;
        case 1:
          this._point = 2;
          break;
        case 2:
          this._point = 3, r(this, x(this, n = a(this, s, i)), n);
          break;
        default:
          r(this, this._t0, n = a(this, s, i));
          break;
      }
      this._x0 = this._x1, this._x1 = s, this._y0 = this._y1, this._y1 = i, this._t0 = n;
    }
  }
}
function f(t) {
  function s(i) {
    return new y(i, t);
  }
  return s.tension = function(i) {
    return f(+i);
  }, s;
}
function l(t) {
  return t < 0 ? -1 : 1;
}
function a(t, s, i) {
  let n = t._x1 - t._x0, _ = s - t._x1, e = (t._y1 - t._y0) / (n || _ < 0 && -0), h = (i - t._y1) / (_ || n < 0 && -0), o = (e * _ + h * n) / (n + _);
  return (l(e) + l(h)) * Math.min(Math.abs(e), Math.abs(h), 0.5 * Math.abs(o)) || 0;
}
function x(t, s) {
  let i = t._x1 - t._x0;
  return i ? (3 * (t._y1 - t._y0) / i - s) / 2 : s;
}
function r(t, s, i) {
  let n = t._x0, _ = t._y0, e = t._x1, h = t._y1, o = (e - n) / 1.5 * (1 - t._tension);
  t._context.bezierCurveTo(_ + o * s, n + o, h - o * i, e - o, h, e);
}
class T {
  constructor(s, i) {
    this._line = 0, this._point = 0, this._x0 = 0, this._x1 = 0, this._y0 = 0, this._y1 = 0, this._t0 = 0, this._tension = 0, this._context = s, this._tension = i;
  }
  areaStart() {
    this._line = 0;
  }
  areaEnd() {
    this._line = NaN;
  }
  lineStart() {
    this._x0 = this._x1 = this._y0 = this._y1 = this._t0 = NaN, this._point = 0;
  }
  lineEnd() {
    switch (this._point) {
      case 2:
        this._context.lineTo(this._x1, this._y1);
        break;
      case 3:
        c(this, this._t0, b(this, this._t0));
        break;
    }
    (this._line || this._line !== 0 && this._point === 1) && this._context.closePath(), this._line = 1 - this._line;
  }
  point(s, i) {
    let n = NaN;
    if (s = +s, i = +i, !(s === this._x1 && i === this._y1)) {
      switch (this._point) {
        case 0:
          this._point = 1, this._line ? this._context.lineTo(s, i) : this._context.moveTo(s, i);
          break;
        case 1:
          this._point = 2;
          break;
        case 2:
          this._point = 3, c(this, b(this, n = p(this, s, i)), n);
          break;
        default:
          c(this, this._t0, n = p(this, s, i));
          break;
      }
      this._x0 = this._x1, this._x1 = s, this._y0 = this._y1, this._y1 = i, this._t0 = n;
    }
  }
}
function k(t) {
  function s(i) {
    return new T(i, t);
  }
  return s;
}
function u(t) {
  return t < 0 ? -1 : 1;
}
function p(t, s, i) {
  let n = t._x1 - t._x0, _ = s - t._x1, e = (t._y1 - t._y0) / (n || _ < 0 && -0), h = (i - t._y1) / (_ || n < 0 && -0), o = (e * _ + h * n) / (n + _);
  return (u(e) + u(h)) * Math.min(Math.abs(e), Math.abs(h), 0.5 * Math.abs(o)) || 0;
}
function b(t, s) {
  let i = t._x1 - t._x0;
  return i ? (3 * (t._y1 - t._y0) / i - s) / 2 : s;
}
function c(t, s, i) {
  let n = t._x0, _ = t._y0, e = t._x1, h = t._y1, o = (e - n) / 1.5 * (1 - t._tension);
  t._context.bezierCurveTo(n + o, _ + o * s, e - o, h - o * i, e, h);
}
export {
  k as a,
  f as c
};
