'use strict';

var tslib_es6 = require('../../../../../../tslib@2.3.0/node_modules/tslib/tslib.es6.js');
var util = require('../../../../../../zrender@6.0.0/node_modules/zrender/lib/core/util.js');
require('../../util/graphic.js');
var sectorHelper = require('../helper/sectorHelper.js');
var basicTransition = require('../../animation/basicTransition.js');
var states = require('../../util/states.js');
var innerStore = require('../../util/innerStore.js');
var Path = require('../../../../../../zrender@6.0.0/node_modules/zrender/lib/graphic/Path.js');
var LinearGradient = require('../../../../../../zrender@6.0.0/node_modules/zrender/lib/graphic/LinearGradient.js');

var ChordPathShape = /** @class */function () {
  function ChordPathShape() {
    // Souce node, two points forming an arc
    this.s1 = [0, 0];
    this.s2 = [0, 0];
    this.sStartAngle = 0;
    this.sEndAngle = 0;
    // Target node, two points forming an arc
    this.t1 = [0, 0];
    this.t2 = [0, 0];
    this.tStartAngle = 0;
    this.tEndAngle = 0;
    this.cx = 0;
    this.cy = 0;
    // series.r0 of ChordSeries
    this.r = 0;
    this.clockwise = true;
  }
  return ChordPathShape;
}();
var ChordEdge = /** @class */function (_super) {
  tslib_es6.__extends(ChordEdge, _super);
  function ChordEdge(nodeData, edgeData, edgeIdx, startAngle) {
    var _this = _super.call(this) || this;
    innerStore.getECData(_this).dataType = 'edge';
    _this.updateData(nodeData, edgeData, edgeIdx, startAngle, true);
    return _this;
  }
  ChordEdge.prototype.buildPath = function (ctx, shape) {
    // Start from n11
    ctx.moveTo(shape.s1[0], shape.s1[1]);
    var ratio = 0.7;
    var clockwise = shape.clockwise;
    // Draw the arc from n11 to n12
    ctx.arc(shape.cx, shape.cy, shape.r, shape.sStartAngle, shape.sEndAngle, !clockwise);
    // Bezier curve to cp1 and then to n21
    ctx.bezierCurveTo((shape.cx - shape.s2[0]) * ratio + shape.s2[0], (shape.cy - shape.s2[1]) * ratio + shape.s2[1], (shape.cx - shape.t1[0]) * ratio + shape.t1[0], (shape.cy - shape.t1[1]) * ratio + shape.t1[1], shape.t1[0], shape.t1[1]);
    // Draw the arc from n21 to n22
    ctx.arc(shape.cx, shape.cy, shape.r, shape.tStartAngle, shape.tEndAngle, !clockwise);
    // Bezier curve back to cp2 and then to n11
    ctx.bezierCurveTo((shape.cx - shape.t2[0]) * ratio + shape.t2[0], (shape.cy - shape.t2[1]) * ratio + shape.t2[1], (shape.cx - shape.s1[0]) * ratio + shape.s1[0], (shape.cy - shape.s1[1]) * ratio + shape.s1[1], shape.s1[0], shape.s1[1]);
    ctx.closePath();
  };
  ChordEdge.prototype.updateData = function (nodeData, edgeData, edgeIdx, startAngle, firstCreate) {
    var seriesModel = nodeData.hostModel;
    var edge = edgeData.graph.getEdgeByIndex(edgeIdx);
    var layout = edge.getLayout();
    var itemModel = edge.node1.getModel();
    var edgeModel = edgeData.getItemModel(edge.dataIndex);
    var lineStyle = edgeModel.getModel('lineStyle');
    var emphasisModel = edgeModel.getModel('emphasis');
    var focus = emphasisModel.get('focus');
    var shape = util.extend(sectorHelper.getSectorCornerRadius(itemModel.getModel('itemStyle'), layout, true), layout);
    var el = this;
    // Ignore NaN data.
    if (isNaN(shape.sStartAngle) || isNaN(shape.tStartAngle)) {
      // Use NaN shape to avoid drawing shape.
      el.setShape(shape);
      return;
    }
    if (firstCreate) {
      el.setShape(shape);
      applyEdgeFill(el, edge, nodeData, lineStyle);
    } else {
      basicTransition.saveOldStyle(el);
      applyEdgeFill(el, edge, nodeData, lineStyle);
      basicTransition.updateProps(el, {
        shape: shape
      }, seriesModel, edgeIdx);
    }
    states.toggleHoverEmphasis(this, focus === 'adjacency' ? edge.getAdjacentDataIndices() : focus, emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
    states.setStatesStylesFromModel(el, edgeModel, 'lineStyle');
    edgeData.setItemGraphicEl(edge.dataIndex, el);
  };
  return ChordEdge;
}(Path.default);
function applyEdgeFill(edgeShape, edge, nodeData, lineStyleModel) {
  var node1 = edge.node1;
  var node2 = edge.node2;
  var edgeStyle = edgeShape.style;
  edgeShape.setStyle(lineStyleModel.getLineStyle());
  var color = lineStyleModel.get('color');
  switch (color) {
    case 'source':
      // TODO: use visual and node1.getVisual('color');
      edgeStyle.fill = nodeData.getItemVisual(node1.dataIndex, 'style').fill;
      edgeStyle.decal = node1.getVisual('style').decal;
      break;
    case 'target':
      edgeStyle.fill = nodeData.getItemVisual(node2.dataIndex, 'style').fill;
      edgeStyle.decal = node2.getVisual('style').decal;
      break;
    case 'gradient':
      var sourceColor = nodeData.getItemVisual(node1.dataIndex, 'style').fill;
      var targetColor = nodeData.getItemVisual(node2.dataIndex, 'style').fill;
      if (util.isString(sourceColor) && util.isString(targetColor)) {
        // Gradient direction is perpendicular to the mid-angles
        // of source and target nodes.
        var shape = edgeShape.shape;
        var sMidX = (shape.s1[0] + shape.s2[0]) / 2;
        var sMidY = (shape.s1[1] + shape.s2[1]) / 2;
        var tMidX = (shape.t1[0] + shape.t2[0]) / 2;
        var tMidY = (shape.t1[1] + shape.t2[1]) / 2;
        edgeStyle.fill = new LinearGradient.default(sMidX, sMidY, tMidX, tMidY, [{
          offset: 0,
          color: sourceColor
        }, {
          offset: 1,
          color: targetColor
        }], true);
      }
      break;
  }
}

exports.ChordEdge = ChordEdge;
exports.ChordPathShape = ChordPathShape;
