import { _ as X, o as q, d4 as U, a7 as H, d5 as O, O as A, bh as j, d6 as Y, aA as J, d7 as K, d8 as Q, d9 as $, da as tt, db as S, dc as et, dd as it, bB as rt, ae as T, cd as M, an as st, de as D, d as nt } from "./index-Cf4hNI3Z.js";
function z(o, t, i) {
  var e = J.createCanvas(), n = t.getWidth(), s = t.getHeight(), r = e.style;
  return r && (r.position = "absolute", r.left = "0", r.top = "0", r.width = n + "px", r.height = s + "px", e.setAttribute("data-zr-dom-id", o)), e.width = n * i, e.height = s * i, e;
}
var V = function(o) {
  X(t, o);
  function t(i, e, n) {
    var s = o.call(this) || this;
    s.motionBlur = !1, s.lastFrameAlpha = 0.7, s.dpr = 1, s.virtual = !1, s.config = {}, s.incremental = !1, s.zlevel = 0, s.maxRepaintRectCount = 5, s.__dirty = !0, s.__firstTimePaint = !0, s.__used = !1, s.__drawIndex = 0, s.__startIndex = 0, s.__endIndex = 0, s.__prevStartIndex = null, s.__prevEndIndex = null;
    var r;
    n = n || Y, typeof i == "string" ? r = z(i, e, n) : q(i) && (r = i, i = r.id), s.id = i, s.dom = r;
    var _ = r.style;
    return _ && (U(r), r.onselectstart = function() {
      return !1;
    }, _.padding = "0", _.margin = "0", _.borderWidth = "0"), s.painter = e, s.dpr = n, s;
  }
  return t.prototype.getElementCount = function() {
    return this.__endIndex - this.__startIndex;
  }, t.prototype.afterBrush = function() {
    this.__prevStartIndex = this.__startIndex, this.__prevEndIndex = this.__endIndex;
  }, t.prototype.initContext = function() {
    this.ctx = this.dom.getContext("2d"), this.ctx.dpr = this.dpr;
  }, t.prototype.setUnpainted = function() {
    this.__firstTimePaint = !0;
  }, t.prototype.createBackBuffer = function() {
    var i = this.dpr;
    this.domBack = z("back-" + this.id, this.painter, i), this.ctxBack = this.domBack.getContext("2d"), i !== 1 && this.ctxBack.scale(i, i);
  }, t.prototype.createRepaintRects = function(i, e, n, s) {
    if (this.__firstTimePaint)
      return this.__firstTimePaint = !1, null;
    var r = [], _ = this.maxRepaintRectCount, h = !1, d = new H(0, 0, 0, 0);
    function f(p) {
      if (!(!p.isFinite() || p.isZero()))
        if (r.length === 0) {
          var y = new H(0, 0, 0, 0);
          y.copy(p), r.push(y);
        } else {
          for (var L = !1, E = 1 / 0, B = 0, w = 0; w < r.length; ++w) {
            var x = r[w];
            if (x.intersect(p)) {
              var b = new H(0, 0, 0, 0);
              b.copy(x), b.union(p), r[w] = b, L = !0;
              break;
            } else if (h) {
              d.copy(p), d.union(x);
              var C = p.width * p.height, P = x.width * x.height, I = d.width * d.height, k = I - C - P;
              k < E && (E = k, B = w);
            }
          }
          if (h && (r[B].union(p), L = !0), !L) {
            var y = new H(0, 0, 0, 0);
            y.copy(p), r.push(y);
          }
          h || (h = r.length >= _);
        }
    }
    for (var a = this.__startIndex; a < this.__endIndex; ++a) {
      var u = i[a];
      if (u) {
        var g = u.shouldBePainted(n, s, !0, !0), m = u.__isRendered && (u.__dirty & O || !g) ? u.getPrevPaintRect() : null;
        m && f(m);
        var c = g && (u.__dirty & O || !u.__isRendered) ? u.getPaintRect() : null;
        c && f(c);
      }
    }
    for (var a = this.__prevStartIndex; a < this.__prevEndIndex; ++a) {
      var u = e[a], g = u && u.shouldBePainted(n, s, !0, !0);
      if (u && (!g || !u.__zr) && u.__isRendered) {
        var m = u.getPrevPaintRect();
        m && f(m);
      }
    }
    var v;
    do {
      v = !1;
      for (var a = 0; a < r.length; ) {
        if (r[a].isZero()) {
          r.splice(a, 1);
          continue;
        }
        for (var l = a + 1; l < r.length; )
          r[a].intersect(r[l]) ? (v = !0, r[a].union(r[l]), r.splice(l, 1)) : l++;
        a++;
      }
    } while (v);
    return this._paintRects = r, r;
  }, t.prototype.debugGetPaintRects = function() {
    return (this._paintRects || []).slice();
  }, t.prototype.resize = function(i, e) {
    var n = this.dpr, s = this.dom, r = s.style, _ = this.domBack;
    r && (r.width = i + "px", r.height = e + "px"), s.width = i * n, s.height = e * n, _ && (_.width = i * n, _.height = e * n, n !== 1 && this.ctxBack.scale(n, n));
  }, t.prototype.clear = function(i, e, n) {
    var s = this.dom, r = this.ctx, _ = s.width, h = s.height;
    e = e || this.clearColor;
    var d = this.motionBlur && !i, f = this.lastFrameAlpha, a = this.dpr, u = this;
    d && (this.domBack || this.createBackBuffer(), this.ctxBack.globalCompositeOperation = "copy", this.ctxBack.drawImage(s, 0, 0, _ / a, h / a));
    var g = this.domBack;
    function m(c, v, l, p) {
      if (r.clearRect(c, v, l, p), e && e !== "transparent") {
        var y = void 0;
        if (K(e)) {
          var L = e.global || e.__width === l && e.__height === p;
          y = L && e.__canvasGradient || Q(r, e, {
            x: 0,
            y: 0,
            width: l,
            height: p
          }), e.__canvasGradient = y, e.__width = l, e.__height = p;
        } else $(e) && (e.scaleX = e.scaleX || a, e.scaleY = e.scaleY || a, y = tt(r, e, {
          dirty: function() {
            u.setUnpainted(), u.painter.refresh();
          }
        }));
        r.save(), r.fillStyle = y || e, r.fillRect(c, v, l, p), r.restore();
      }
      d && (r.save(), r.globalAlpha = f, r.drawImage(g, c, v, l, p), r.restore());
    }
    !n || d ? m(0, 0, _, h) : n.length && A(n, function(c) {
      m(c.x * a, c.y * a, c.width * a, c.height * a);
    });
  }, t;
}(j), W = 1e5, R = 314159, N = 0.01, at = 1e-3;
function ot(o) {
  return o ? o.__builtin__ ? !0 : !(typeof o.resize != "function" || typeof o.refresh != "function") : !1;
}
function ht(o, t) {
  var i = document.createElement("div");
  return i.style.cssText = [
    "position:relative",
    "width:" + o + "px",
    "height:" + t + "px",
    "padding:0",
    "margin:0",
    "border-width:0"
  ].join(";") + ";", i;
}
var _t = function() {
  function o(t, i, e, n) {
    this.type = "canvas", this._zlevelList = [], this._prevDisplayList = [], this._layers = {}, this._layerConfig = {}, this._needsManuallyCompositing = !1, this.type = "canvas";
    var s = !t.nodeName || t.nodeName.toUpperCase() === "CANVAS";
    this._opts = e = nt({}, e || {}), this.dpr = e.devicePixelRatio || Y, this._singleCanvas = s, this.root = t;
    var r = t.style;
    r && (U(t), t.innerHTML = ""), this.storage = i;
    var _ = this._zlevelList;
    this._prevDisplayList = [];
    var h = this._layers;
    if (s) {
      var f = t, a = f.width, u = f.height;
      e.width != null && (a = e.width), e.height != null && (u = e.height), this.dpr = e.devicePixelRatio || 1, f.width = a * this.dpr, f.height = u * this.dpr, this._width = a, this._height = u;
      var g = new V(f, this, this.dpr);
      g.__builtin__ = !0, g.initContext(), h[R] = g, g.zlevel = R, _.push(R), this._domRoot = t;
    } else {
      this._width = D(t, 0, e), this._height = D(t, 1, e);
      var d = this._domRoot = ht(this._width, this._height);
      t.appendChild(d);
    }
  }
  return o.prototype.getType = function() {
    return "canvas";
  }, o.prototype.isSingleCanvas = function() {
    return this._singleCanvas;
  }, o.prototype.getViewportRoot = function() {
    return this._domRoot;
  }, o.prototype.getViewportRootOffset = function() {
    var t = this.getViewportRoot();
    if (t)
      return {
        offsetLeft: t.offsetLeft || 0,
        offsetTop: t.offsetTop || 0
      };
  }, o.prototype.refresh = function(t) {
    var i = this.storage.getDisplayList(!0), e = this._prevDisplayList, n = this._zlevelList;
    this._redrawId = Math.random(), this._paintList(i, e, t, this._redrawId);
    for (var s = 0; s < n.length; s++) {
      var r = n[s], _ = this._layers[r];
      if (!_.__builtin__ && _.refresh) {
        var h = s === 0 ? this._backgroundColor : null;
        _.refresh(h);
      }
    }
    return this._opts.useDirtyRect && (this._prevDisplayList = i.slice()), this;
  }, o.prototype.refreshHover = function() {
    this._paintHoverList(this.storage.getDisplayList(!1));
  }, o.prototype._paintHoverList = function(t) {
    var i = t.length, e = this._hoverlayer;
    if (e && e.clear(), !!i) {
      for (var n = {
        inHover: !0,
        viewWidth: this._width,
        viewHeight: this._height
      }, s, r = 0; r < i; r++) {
        var _ = t[r];
        _.__inHover && (e || (e = this._hoverlayer = this.getLayer(W)), s || (s = e.ctx, s.save()), S(s, _, n, r === i - 1));
      }
      s && s.restore();
    }
  }, o.prototype.getHoverLayer = function() {
    return this.getLayer(W);
  }, o.prototype.paintOne = function(t, i) {
    et(t, i);
  }, o.prototype._paintList = function(t, i, e, n) {
    if (this._redrawId === n) {
      e = e || !1, this._updateLayerStatus(t);
      var s = this._doPaintList(t, i, e), r = s.finished, _ = s.needsRefreshHover;
      if (this._needsManuallyCompositing && this._compositeManually(), _ && this._paintHoverList(t), r)
        this.eachLayer(function(d) {
          d.afterBrush && d.afterBrush();
        });
      else {
        var h = this;
        it(function() {
          h._paintList(t, i, e, n);
        });
      }
    }
  }, o.prototype._compositeManually = function() {
    var t = this.getLayer(R).ctx, i = this._domRoot.width, e = this._domRoot.height;
    t.clearRect(0, 0, i, e), this.eachBuiltinLayer(function(n) {
      n.virtual && t.drawImage(n.dom, 0, 0, i, e);
    });
  }, o.prototype._doPaintList = function(t, i, e) {
    for (var n = this, s = [], r = this._opts.useDirtyRect, _ = 0; _ < this._zlevelList.length; _++) {
      var h = this._zlevelList[_], d = this._layers[h];
      d.__builtin__ && d !== this._hoverlayer && (d.__dirty || e) && s.push(d);
    }
    for (var f = !0, a = !1, u = function(c) {
      var v = s[c], l = v.ctx, p = r && v.createRepaintRects(t, i, g._width, g._height), y = e ? v.__startIndex : v.__drawIndex, L = !e && v.incremental && Date.now, E = L && Date.now(), B = v.zlevel === g._zlevelList[0] ? g._backgroundColor : null;
      if (v.__startIndex === v.__endIndex)
        v.clear(!1, B, p);
      else if (y === v.__startIndex) {
        var w = t[y];
        (!w.incremental || !w.notClear || e) && v.clear(!1, B, p);
      }
      y === -1 && (console.error("For some unknown reason. drawIndex is -1"), y = v.__startIndex);
      var x, b = function(k) {
        var F = {
          inHover: !1,
          allClipped: !1,
          prevEl: null,
          viewWidth: n._width,
          viewHeight: n._height
        };
        for (x = y; x < v.__endIndex; x++) {
          var Z = t[x];
          if (Z.__inHover && (a = !0), n._doPaintEl(Z, v, r, k, F, x === v.__endIndex - 1), L) {
            var G = Date.now() - E;
            if (G > 15)
              break;
          }
        }
        F.prevElClipPaths && l.restore();
      };
      if (p)
        if (p.length === 0)
          x = v.__endIndex;
        else
          for (var C = g.dpr, P = 0; P < p.length; ++P) {
            var I = p[P];
            l.save(), l.beginPath(), l.rect(I.x * C, I.y * C, I.width * C, I.height * C), l.clip(), b(I), l.restore();
          }
      else
        l.save(), b(), l.restore();
      v.__drawIndex = x, v.__drawIndex < v.__endIndex && (f = !1);
    }, g = this, m = 0; m < s.length; m++)
      u(m);
    return rt.wxa && A(this._layers, function(c) {
      c && c.ctx && c.ctx.draw && c.ctx.draw();
    }), {
      finished: f,
      needsRefreshHover: a
    };
  }, o.prototype._doPaintEl = function(t, i, e, n, s, r) {
    var _ = i.ctx;
    if (e) {
      var h = t.getPaintRect();
      (!n || h && h.intersect(n)) && (S(_, t, s, r), t.setPrevPaintRect(h));
    } else
      S(_, t, s, r);
  }, o.prototype.getLayer = function(t, i) {
    this._singleCanvas && !this._needsManuallyCompositing && (t = R);
    var e = this._layers[t];
    return e || (e = new V("zr_" + t, this, this.dpr), e.zlevel = t, e.__builtin__ = !0, this._layerConfig[t] ? T(e, this._layerConfig[t], !0) : this._layerConfig[t - N] && T(e, this._layerConfig[t - N], !0), i && (e.virtual = i), this.insertLayer(t, e), e.initContext()), e;
  }, o.prototype.insertLayer = function(t, i) {
    var e = this._layers, n = this._zlevelList, s = n.length, r = this._domRoot, _ = null, h = -1;
    if (e[t]) {
      process.env.NODE_ENV !== "production" && M("ZLevel " + t + " has been used already");
      return;
    }
    if (!ot(i)) {
      process.env.NODE_ENV !== "production" && M("Layer of zlevel " + t + " is not valid");
      return;
    }
    if (s > 0 && t > n[0]) {
      for (h = 0; h < s - 1 && !(n[h] < t && n[h + 1] > t); h++)
        ;
      _ = e[n[h]];
    }
    if (n.splice(h + 1, 0, t), e[t] = i, !i.virtual)
      if (_) {
        var d = _.dom;
        d.nextSibling ? r.insertBefore(i.dom, d.nextSibling) : r.appendChild(i.dom);
      } else
        r.firstChild ? r.insertBefore(i.dom, r.firstChild) : r.appendChild(i.dom);
    i.painter || (i.painter = this);
  }, o.prototype.eachLayer = function(t, i) {
    for (var e = this._zlevelList, n = 0; n < e.length; n++) {
      var s = e[n];
      t.call(i, this._layers[s], s);
    }
  }, o.prototype.eachBuiltinLayer = function(t, i) {
    for (var e = this._zlevelList, n = 0; n < e.length; n++) {
      var s = e[n], r = this._layers[s];
      r.__builtin__ && t.call(i, r, s);
    }
  }, o.prototype.eachOtherLayer = function(t, i) {
    for (var e = this._zlevelList, n = 0; n < e.length; n++) {
      var s = e[n], r = this._layers[s];
      r.__builtin__ || t.call(i, r, s);
    }
  }, o.prototype.getLayers = function() {
    return this._layers;
  }, o.prototype._updateLayerStatus = function(t) {
    this.eachBuiltinLayer(function(a, u) {
      a.__dirty = a.__used = !1;
    });
    function i(a) {
      s && (s.__endIndex !== a && (s.__dirty = !0), s.__endIndex = a);
    }
    if (this._singleCanvas)
      for (var e = 1; e < t.length; e++) {
        var n = t[e];
        if (n.zlevel !== t[e - 1].zlevel || n.incremental) {
          this._needsManuallyCompositing = !0;
          break;
        }
      }
    var s = null, r = 0, _, h;
    for (h = 0; h < t.length; h++) {
      var n = t[h], d = n.zlevel, f = void 0;
      _ !== d && (_ = d, r = 0), n.incremental ? (f = this.getLayer(d + at, this._needsManuallyCompositing), f.incremental = !0, r = 1) : f = this.getLayer(d + (r > 0 ? N : 0), this._needsManuallyCompositing), f.__builtin__ || M("ZLevel " + d + " has been used by unkown layer " + f.id), f !== s && (f.__used = !0, f.__startIndex !== h && (f.__dirty = !0), f.__startIndex = h, f.incremental ? f.__drawIndex = -1 : f.__drawIndex = h, i(h), s = f), n.__dirty & O && !n.__inHover && (f.__dirty = !0, f.incremental && f.__drawIndex < 0 && (f.__drawIndex = h));
    }
    i(h), this.eachBuiltinLayer(function(a, u) {
      !a.__used && a.getElementCount() > 0 && (a.__dirty = !0, a.__startIndex = a.__endIndex = a.__drawIndex = 0), a.__dirty && a.__drawIndex < 0 && (a.__drawIndex = a.__startIndex);
    });
  }, o.prototype.clear = function() {
    return this.eachBuiltinLayer(this._clearLayer), this;
  }, o.prototype._clearLayer = function(t) {
    t.clear();
  }, o.prototype.setBackgroundColor = function(t) {
    this._backgroundColor = t, A(this._layers, function(i) {
      i.setUnpainted();
    });
  }, o.prototype.configLayer = function(t, i) {
    if (i) {
      var e = this._layerConfig;
      e[t] ? T(e[t], i, !0) : e[t] = i;
      for (var n = 0; n < this._zlevelList.length; n++) {
        var s = this._zlevelList[n];
        if (s === t || s === t + N) {
          var r = this._layers[s];
          T(r, e[t], !0);
        }
      }
    }
  }, o.prototype.delLayer = function(t) {
    var i = this._layers, e = this._zlevelList, n = i[t];
    n && (n.dom.parentNode.removeChild(n.dom), delete i[t], e.splice(st(e, t), 1));
  }, o.prototype.resize = function(t, i) {
    if (this._domRoot.style) {
      var e = this._domRoot;
      e.style.display = "none";
      var n = this._opts, s = this.root;
      if (t != null && (n.width = t), i != null && (n.height = i), t = D(s, 0, n), i = D(s, 1, n), e.style.display = "", this._width !== t || i !== this._height) {
        e.style.width = t + "px", e.style.height = i + "px";
        for (var r in this._layers)
          this._layers.hasOwnProperty(r) && this._layers[r].resize(t, i);
        this.refresh(!0);
      }
      this._width = t, this._height = i;
    } else {
      if (t == null || i == null)
        return;
      this._width = t, this._height = i, this.getLayer(R).resize(t, i);
    }
    return this;
  }, o.prototype.clearLayer = function(t) {
    var i = this._layers[t];
    i && i.clear();
  }, o.prototype.dispose = function() {
    this.root.innerHTML = "", this.root = this.storage = this._domRoot = this._layers = null;
  }, o.prototype.getRenderedCanvas = function(t) {
    if (t = t || {}, this._singleCanvas && !this._compositeManually)
      return this._layers[R].dom;
    var i = new V("image", this, t.pixelRatio || this.dpr);
    i.initContext(), i.clear(!1, t.backgroundColor || this._backgroundColor);
    var e = i.ctx;
    if (t.pixelRatio <= this.dpr) {
      this.refresh();
      var n = i.dom.width, s = i.dom.height;
      this.eachLayer(function(a) {
        a.__builtin__ ? e.drawImage(a.dom, 0, 0, n, s) : a.renderToCanvas && (e.save(), a.renderToCanvas(e), e.restore());
      });
    } else
      for (var r = {
        inHover: !1,
        viewWidth: this._width,
        viewHeight: this._height
      }, _ = this.storage.getDisplayList(!0), h = 0, d = _.length; h < d; h++) {
        var f = _[h];
        S(e, f, r, h === d - 1);
      }
    return i.dom;
  }, o.prototype.getWidth = function() {
    return this._width;
  }, o.prototype.getHeight = function() {
    return this._height;
  }, o;
}();
function dt(o) {
  o.registerPainter("canvas", _t);
}
export {
  dt as CanvasRenderer
};
