import { __extends } from "../../../node_modules/tslib/tslib.es6.mjs";
import { setStatesStylesFromModel, toggleHoverEmphasis } from "../../util/states.mjs";
import ChartView from "../../view/Chart.mjs";
import { numericToNumber } from "../../util/number.mjs";
import { eqNaN } from "../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/util.mjs";
import { updateProps, saveOldStyle, initProps } from "../../animation/basicTransition.mjs";
import Group from "../../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Group.mjs";
import Rect from "../../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Rect.mjs";
import Polyline from "../../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Polyline.mjs";
var DEFAULT_SMOOTH = 0.3;
var ParallelView = (
  /** @class */
  function(_super) {
    __extends(ParallelView2, _super);
    function ParallelView2() {
      var _this = _super !== null && _super.apply(this, arguments) || this;
      _this.type = ParallelView2.type;
      _this._dataGroup = new Group();
      _this._initialized = false;
      return _this;
    }
    ParallelView2.prototype.init = function() {
      this.group.add(this._dataGroup);
    };
    ParallelView2.prototype.render = function(seriesModel, ecModel, api, payload) {
      this._progressiveEls = null;
      var dataGroup = this._dataGroup;
      var data = seriesModel.getData();
      var oldData = this._data;
      var coordSys = seriesModel.coordinateSystem;
      var dimensions = coordSys.dimensions;
      var seriesScope = makeSeriesScope(seriesModel);
      data.diff(oldData).add(add).update(update).remove(remove).execute();
      function add(newDataIndex) {
        var line = addEl(data, dataGroup, newDataIndex, dimensions, coordSys);
        updateElCommon(line, data, newDataIndex, seriesScope);
      }
      function update(newDataIndex, oldDataIndex) {
        var line = oldData.getItemGraphicEl(oldDataIndex);
        var points = createLinePoints(data, newDataIndex, dimensions, coordSys);
        data.setItemGraphicEl(newDataIndex, line);
        updateProps(line, {
          shape: {
            points
          }
        }, seriesModel, newDataIndex);
        saveOldStyle(line);
        updateElCommon(line, data, newDataIndex, seriesScope);
      }
      function remove(oldDataIndex) {
        var line = oldData.getItemGraphicEl(oldDataIndex);
        dataGroup.remove(line);
      }
      if (!this._initialized) {
        this._initialized = true;
        var clipPath = createGridClipShape(coordSys, seriesModel, function() {
          setTimeout(function() {
            dataGroup.removeClipPath();
          });
        });
        dataGroup.setClipPath(clipPath);
      }
      this._data = data;
    };
    ParallelView2.prototype.incrementalPrepareRender = function(seriesModel, ecModel, api) {
      this._initialized = true;
      this._data = null;
      this._dataGroup.removeAll();
    };
    ParallelView2.prototype.incrementalRender = function(taskParams, seriesModel, ecModel) {
      var data = seriesModel.getData();
      var coordSys = seriesModel.coordinateSystem;
      var dimensions = coordSys.dimensions;
      var seriesScope = makeSeriesScope(seriesModel);
      var progressiveEls = this._progressiveEls = [];
      for (var dataIndex = taskParams.start; dataIndex < taskParams.end; dataIndex++) {
        var line = addEl(data, this._dataGroup, dataIndex, dimensions, coordSys);
        line.incremental = true;
        updateElCommon(line, data, dataIndex, seriesScope);
        progressiveEls.push(line);
      }
    };
    ParallelView2.prototype.remove = function() {
      this._dataGroup && this._dataGroup.removeAll();
      this._data = null;
    };
    ParallelView2.type = "parallel";
    return ParallelView2;
  }(ChartView)
);
function createGridClipShape(coordSys, seriesModel, cb) {
  var parallelModel = coordSys.model;
  var rect = coordSys.getRect();
  var rectEl = new Rect({
    shape: {
      x: rect.x,
      y: rect.y,
      width: rect.width,
      height: rect.height
    }
  });
  var dim = parallelModel.get("layout") === "horizontal" ? "width" : "height";
  rectEl.setShape(dim, 0);
  initProps(rectEl, {
    shape: {
      width: rect.width,
      height: rect.height
    }
  }, seriesModel, cb);
  return rectEl;
}
function createLinePoints(data, dataIndex, dimensions, coordSys) {
  var points = [];
  for (var i = 0; i < dimensions.length; i++) {
    var dimName = dimensions[i];
    var value = data.get(data.mapDimension(dimName), dataIndex);
    if (!isEmptyValue(value, coordSys.getAxis(dimName).type)) {
      points.push(coordSys.dataToPoint(value, dimName));
    }
  }
  return points;
}
function addEl(data, dataGroup, dataIndex, dimensions, coordSys) {
  var points = createLinePoints(data, dataIndex, dimensions, coordSys);
  var line = new Polyline({
    shape: {
      points
    },
    // silent: true,
    z2: 10
  });
  dataGroup.add(line);
  data.setItemGraphicEl(dataIndex, line);
  return line;
}
function makeSeriesScope(seriesModel) {
  var smooth = seriesModel.get("smooth", true);
  smooth === true && (smooth = DEFAULT_SMOOTH);
  smooth = numericToNumber(smooth);
  eqNaN(smooth) && (smooth = 0);
  return {
    smooth
  };
}
function updateElCommon(el, data, dataIndex, seriesScope) {
  el.useStyle(data.getItemVisual(dataIndex, "style"));
  el.style.fill = null;
  el.setShape("smooth", seriesScope.smooth);
  var itemModel = data.getItemModel(dataIndex);
  var emphasisModel = itemModel.getModel("emphasis");
  setStatesStylesFromModel(el, itemModel, "lineStyle");
  toggleHoverEmphasis(el, emphasisModel.get("focus"), emphasisModel.get("blurScope"), emphasisModel.get("disabled"));
}
function isEmptyValue(val, axisType) {
  return axisType === "category" ? val == null : val == null || isNaN(val);
}
const ParallelView$1 = ParallelView;
export {
  ParallelView$1 as default
};
