"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const tslib_es6 = require("../../../node_modules/tslib/tslib.es6.js");
const util = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/util.js");
const Chart = require("../../view/Chart.js");
const SunburstPiece = require("./SunburstPiece.js");
const DataDiffer = require("../../data/DataDiffer.js");
const sunburstAction = require("./sunburstAction.js");
const format = require("../../util/format.js");
var SunburstView = (
  /** @class */
  function(_super) {
    tslib_es6.__extends(SunburstView2, _super);
    function SunburstView2() {
      var _this = _super !== null && _super.apply(this, arguments) || this;
      _this.type = SunburstView2.type;
      return _this;
    }
    SunburstView2.prototype.render = function(seriesModel, ecModel, api, payload) {
      var self = this;
      this.seriesModel = seriesModel;
      this.api = api;
      this.ecModel = ecModel;
      var data = seriesModel.getData();
      var virtualRoot = data.tree.root;
      var newRoot = seriesModel.getViewRoot();
      var group = this.group;
      var renderLabelForZeroData = seriesModel.get("renderLabelForZeroData");
      var newChildren = [];
      newRoot.eachNode(function(node) {
        newChildren.push(node);
      });
      var oldChildren = this._oldChildren || [];
      dualTravel(newChildren, oldChildren);
      renderRollUp(virtualRoot, newRoot);
      this._initEvents();
      this._oldChildren = newChildren;
      function dualTravel(newChildren2, oldChildren2) {
        if (newChildren2.length === 0 && oldChildren2.length === 0) {
          return;
        }
        new DataDiffer.default(oldChildren2, newChildren2, getKey, getKey).add(processNode).update(processNode).remove(util.curry(processNode, null)).execute();
        function getKey(node) {
          return node.getId();
        }
        function processNode(newIdx, oldIdx) {
          var newNode = newIdx == null ? null : newChildren2[newIdx];
          var oldNode = oldIdx == null ? null : oldChildren2[oldIdx];
          doRenderNode(newNode, oldNode);
        }
      }
      function doRenderNode(newNode, oldNode) {
        if (!renderLabelForZeroData && newNode && !newNode.getValue()) {
          newNode = null;
        }
        if (newNode !== virtualRoot && oldNode !== virtualRoot) {
          if (oldNode && oldNode.piece) {
            if (newNode) {
              oldNode.piece.updateData(false, newNode, seriesModel, ecModel, api);
              data.setItemGraphicEl(newNode.dataIndex, oldNode.piece);
            } else {
              removeNode(oldNode);
            }
          } else if (newNode) {
            var piece = new SunburstPiece.default(newNode, seriesModel, ecModel, api);
            group.add(piece);
            data.setItemGraphicEl(newNode.dataIndex, piece);
          }
        }
      }
      function removeNode(node) {
        if (!node) {
          return;
        }
        if (node.piece) {
          group.remove(node.piece);
          node.piece = null;
        }
      }
      function renderRollUp(virtualRoot2, viewRoot) {
        if (viewRoot.depth > 0) {
          if (self.virtualPiece) {
            self.virtualPiece.updateData(false, virtualRoot2, seriesModel, ecModel, api);
          } else {
            self.virtualPiece = new SunburstPiece.default(virtualRoot2, seriesModel, ecModel, api);
            group.add(self.virtualPiece);
          }
          viewRoot.piece.off("click");
          self.virtualPiece.on("click", function(e) {
            self._rootToNode(viewRoot.parentNode);
          });
        } else if (self.virtualPiece) {
          group.remove(self.virtualPiece);
          self.virtualPiece = null;
        }
      }
    };
    SunburstView2.prototype._initEvents = function() {
      var _this = this;
      this.group.off("click");
      this.group.on("click", function(e) {
        var targetFound = false;
        var viewRoot = _this.seriesModel.getViewRoot();
        viewRoot.eachNode(function(node) {
          if (!targetFound && node.piece && node.piece === e.target) {
            var nodeClick = node.getModel().get("nodeClick");
            if (nodeClick === "rootToNode") {
              _this._rootToNode(node);
            } else if (nodeClick === "link") {
              var itemModel = node.getModel();
              var link = itemModel.get("link");
              if (link) {
                var linkTarget = itemModel.get("target", true) || "_blank";
                format.windowOpen(link, linkTarget);
              }
            }
            targetFound = true;
          }
        });
      });
    };
    SunburstView2.prototype._rootToNode = function(node) {
      if (node !== this.seriesModel.getViewRoot()) {
        this.api.dispatchAction({
          type: sunburstAction.ROOT_TO_NODE_ACTION,
          from: this.uid,
          seriesId: this.seriesModel.id,
          targetNode: node
        });
      }
    };
    SunburstView2.prototype.containPoint = function(point, seriesModel) {
      var treeRoot = seriesModel.getData();
      var itemLayout = treeRoot.getItemLayout(0);
      if (itemLayout) {
        var dx = point[0] - itemLayout.cx;
        var dy = point[1] - itemLayout.cy;
        var radius = Math.sqrt(dx * dx + dy * dy);
        return radius <= itemLayout.r && radius >= itemLayout.r0;
      }
    };
    SunburstView2.type = "sunburst";
    return SunburstView2;
  }(Chart.default)
);
const SunburstView$1 = SunburstView;
exports.default = SunburstView$1;
