"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const tslib_es6 = require("../../../node_modules/tslib/tslib.es6.js");
const Line = require("./Line.js");
const util = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/util.js");
const symbol = require("../../util/symbol.js");
const vector = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/vector.js");
const curve = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/curve.js");
const Group = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Group.js");
var EffectLine = (
  /** @class */
  function(_super) {
    tslib_es6.__extends(EffectLine2, _super);
    function EffectLine2(lineData, idx, seriesScope) {
      var _this = _super.call(this) || this;
      _this.add(_this.createLine(lineData, idx, seriesScope));
      _this._updateEffectSymbol(lineData, idx);
      return _this;
    }
    EffectLine2.prototype.createLine = function(lineData, idx, seriesScope) {
      return new Line.default(lineData, idx, seriesScope);
    };
    EffectLine2.prototype._updateEffectSymbol = function(lineData, idx) {
      var itemModel = lineData.getItemModel(idx);
      var effectModel = itemModel.getModel("effect");
      var size = effectModel.get("symbolSize");
      var symbolType = effectModel.get("symbol");
      if (!util.isArray(size)) {
        size = [size, size];
      }
      var lineStyle = lineData.getItemVisual(idx, "style");
      var color = effectModel.get("color") || lineStyle && lineStyle.stroke;
      var symbol$1 = this.childAt(1);
      if (this._symbolType !== symbolType) {
        this.remove(symbol$1);
        symbol$1 = symbol.createSymbol(symbolType, -0.5, -0.5, 1, 1, color);
        symbol$1.z2 = 100;
        symbol$1.culling = true;
        this.add(symbol$1);
      }
      if (!symbol$1) {
        return;
      }
      symbol$1.setStyle("shadowColor", color);
      symbol$1.setStyle(effectModel.getItemStyle(["color"]));
      symbol$1.scaleX = size[0];
      symbol$1.scaleY = size[1];
      symbol$1.setColor(color);
      this._symbolType = symbolType;
      this._symbolScale = size;
      this._updateEffectAnimation(lineData, effectModel, idx);
    };
    EffectLine2.prototype._updateEffectAnimation = function(lineData, effectModel, idx) {
      var symbol2 = this.childAt(1);
      if (!symbol2) {
        return;
      }
      var points = lineData.getItemLayout(idx);
      var period = effectModel.get("period") * 1e3;
      var loop = effectModel.get("loop");
      var roundTrip = effectModel.get("roundTrip");
      var constantSpeed = effectModel.get("constantSpeed");
      var delayExpr = util.retrieve(effectModel.get("delay"), function(idx2) {
        return idx2 / lineData.count() * period / 3;
      });
      symbol2.ignore = true;
      this._updateAnimationPoints(symbol2, points);
      if (constantSpeed > 0) {
        period = this._getLineLength(symbol2) / constantSpeed * 1e3;
      }
      if (period !== this._period || loop !== this._loop || roundTrip !== this._roundTrip) {
        symbol2.stopAnimation();
        var delayNum = void 0;
        if (util.isFunction(delayExpr)) {
          delayNum = delayExpr(idx);
        } else {
          delayNum = delayExpr;
        }
        if (symbol2.__t > 0) {
          delayNum = -period * symbol2.__t;
        }
        this._animateSymbol(symbol2, period, delayNum, loop, roundTrip);
      }
      this._period = period;
      this._loop = loop;
      this._roundTrip = roundTrip;
    };
    EffectLine2.prototype._animateSymbol = function(symbol2, period, delayNum, loop, roundTrip) {
      if (period > 0) {
        symbol2.__t = 0;
        var self_1 = this;
        var animator = symbol2.animate("", loop).when(roundTrip ? period * 2 : period, {
          __t: roundTrip ? 2 : 1
        }).delay(delayNum).during(function() {
          self_1._updateSymbolPosition(symbol2);
        });
        if (!loop) {
          animator.done(function() {
            self_1.remove(symbol2);
          });
        }
        animator.start();
      }
    };
    EffectLine2.prototype._getLineLength = function(symbol2) {
      return vector.dist(symbol2.__p1, symbol2.__cp1) + vector.dist(symbol2.__cp1, symbol2.__p2);
    };
    EffectLine2.prototype._updateAnimationPoints = function(symbol2, points) {
      symbol2.__p1 = points[0];
      symbol2.__p2 = points[1];
      symbol2.__cp1 = points[2] || [(points[0][0] + points[1][0]) / 2, (points[0][1] + points[1][1]) / 2];
    };
    EffectLine2.prototype.updateData = function(lineData, idx, seriesScope) {
      this.childAt(0).updateData(lineData, idx, seriesScope);
      this._updateEffectSymbol(lineData, idx);
    };
    EffectLine2.prototype._updateSymbolPosition = function(symbol2) {
      var p1 = symbol2.__p1;
      var p2 = symbol2.__p2;
      var cp1 = symbol2.__cp1;
      var t = symbol2.__t < 1 ? symbol2.__t : 2 - symbol2.__t;
      var pos = [symbol2.x, symbol2.y];
      var lastPos = pos.slice();
      var quadraticAt = curve.quadraticAt;
      var quadraticDerivativeAt = curve.quadraticDerivativeAt;
      pos[0] = quadraticAt(p1[0], cp1[0], p2[0], t);
      pos[1] = quadraticAt(p1[1], cp1[1], p2[1], t);
      var tx = symbol2.__t < 1 ? quadraticDerivativeAt(p1[0], cp1[0], p2[0], t) : quadraticDerivativeAt(p2[0], cp1[0], p1[0], 1 - t);
      var ty = symbol2.__t < 1 ? quadraticDerivativeAt(p1[1], cp1[1], p2[1], t) : quadraticDerivativeAt(p2[1], cp1[1], p1[1], 1 - t);
      symbol2.rotation = -Math.atan2(ty, tx) - Math.PI / 2;
      if (this._symbolType === "line" || this._symbolType === "rect" || this._symbolType === "roundRect") {
        if (symbol2.__lastT !== void 0 && symbol2.__lastT < symbol2.__t) {
          symbol2.scaleY = vector.dist(lastPos, pos) * 1.05;
          if (t === 1) {
            pos[0] = lastPos[0] + (pos[0] - lastPos[0]) / 2;
            pos[1] = lastPos[1] + (pos[1] - lastPos[1]) / 2;
          }
        } else if (symbol2.__lastT === 1) {
          symbol2.scaleY = 2 * vector.dist(p1, pos);
        } else {
          symbol2.scaleY = this._symbolScale[1];
        }
      }
      symbol2.__lastT = symbol2.__t;
      symbol2.ignore = false;
      symbol2.x = pos[0];
      symbol2.y = pos[1];
    };
    EffectLine2.prototype.updateLayout = function(lineData, idx) {
      this.childAt(0).updateLayout(lineData, idx);
      var effectModel = lineData.getItemModel(idx).getModel("effect");
      this._updateEffectAnimation(lineData, effectModel, idx);
    };
    return EffectLine2;
  }(Group.default)
);
const EffectLine$1 = EffectLine;
exports.default = EffectLine$1;
