"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 states = require("../../util/states.js");
const labelStyle = require("../../label/labelStyle.js");
const innerStore = require("../../util/innerStore.js");
const sectorHelper = require("../helper/sectorHelper.js");
const decal = require("../../util/decal.js");
const basicTransition = require("../../animation/basicTransition.js");
const util$1 = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/contain/util.js");
const Text = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/Text.js");
const Sector = require("../../../../../../zrender@5.4.4/node_modules/zrender/lib/graphic/shape/Sector.js");
var DEFAULT_SECTOR_Z = 2;
var DEFAULT_TEXT_Z = 4;
var SunburstPiece = (
  /** @class */
  function(_super) {
    tslib_es6.__extends(SunburstPiece2, _super);
    function SunburstPiece2(node, seriesModel, ecModel, api) {
      var _this = _super.call(this) || this;
      _this.z2 = DEFAULT_SECTOR_Z;
      _this.textConfig = {
        inside: true
      };
      innerStore.getECData(_this).seriesIndex = seriesModel.seriesIndex;
      var text = new Text.default({
        z2: DEFAULT_TEXT_Z,
        silent: node.getModel().get(["label", "silent"])
      });
      _this.setTextContent(text);
      _this.updateData(true, node, seriesModel, ecModel, api);
      return _this;
    }
    SunburstPiece2.prototype.updateData = function(firstCreate, node, seriesModel, ecModel, api) {
      this.node = node;
      node.piece = this;
      seriesModel = seriesModel || this._seriesModel;
      ecModel = ecModel || this._ecModel;
      var sector = this;
      innerStore.getECData(sector).dataIndex = node.dataIndex;
      var itemModel = node.getModel();
      var emphasisModel = itemModel.getModel("emphasis");
      var layout = node.getLayout();
      var sectorShape = util.extend({}, layout);
      sectorShape.label = null;
      var normalStyle = node.getVisual("style");
      normalStyle.lineJoin = "bevel";
      var decal$1 = node.getVisual("decal");
      if (decal$1) {
        normalStyle.decal = decal.createOrUpdatePatternFromDecal(decal$1, api);
      }
      var cornerRadius = sectorHelper.getSectorCornerRadius(itemModel.getModel("itemStyle"), sectorShape, true);
      util.extend(sectorShape, cornerRadius);
      util.each(states.SPECIAL_STATES, function(stateName) {
        var state = sector.ensureState(stateName);
        var itemStyleModel = itemModel.getModel([stateName, "itemStyle"]);
        state.style = itemStyleModel.getItemStyle();
        var cornerRadius2 = sectorHelper.getSectorCornerRadius(itemStyleModel, sectorShape);
        if (cornerRadius2) {
          state.shape = cornerRadius2;
        }
      });
      if (firstCreate) {
        sector.setShape(sectorShape);
        sector.shape.r = layout.r0;
        basicTransition.initProps(sector, {
          shape: {
            r: layout.r
          }
        }, seriesModel, node.dataIndex);
      } else {
        basicTransition.updateProps(sector, {
          shape: sectorShape
        }, seriesModel);
        basicTransition.saveOldStyle(sector);
      }
      sector.useStyle(normalStyle);
      this._updateLabel(seriesModel);
      var cursorStyle = itemModel.getShallow("cursor");
      cursorStyle && sector.attr("cursor", cursorStyle);
      this._seriesModel = seriesModel || this._seriesModel;
      this._ecModel = ecModel || this._ecModel;
      var focus = emphasisModel.get("focus");
      var focusOrIndices = focus === "ancestor" ? node.getAncestorsIndices() : focus === "descendant" ? node.getDescendantIndices() : focus;
      states.toggleHoverEmphasis(this, focusOrIndices, emphasisModel.get("blurScope"), emphasisModel.get("disabled"));
    };
    SunburstPiece2.prototype._updateLabel = function(seriesModel) {
      var _this = this;
      var itemModel = this.node.getModel();
      var normalLabelModel = itemModel.getModel("label");
      var layout = this.node.getLayout();
      var angle = layout.endAngle - layout.startAngle;
      var midAngle = (layout.startAngle + layout.endAngle) / 2;
      var dx = Math.cos(midAngle);
      var dy = Math.sin(midAngle);
      var sector = this;
      var label = sector.getTextContent();
      var dataIndex = this.node.dataIndex;
      var labelMinAngle = normalLabelModel.get("minAngle") / 180 * Math.PI;
      var isNormalShown = normalLabelModel.get("show") && !(labelMinAngle != null && Math.abs(angle) < labelMinAngle);
      label.ignore = !isNormalShown;
      util.each(states.DISPLAY_STATES, function(stateName) {
        var labelStateModel = stateName === "normal" ? itemModel.getModel("label") : itemModel.getModel([stateName, "label"]);
        var isNormal = stateName === "normal";
        var state = isNormal ? label : label.ensureState(stateName);
        var text = seriesModel.getFormattedLabel(dataIndex, stateName);
        if (isNormal) {
          text = text || _this.node.name;
        }
        state.style = labelStyle.createTextStyle(labelStateModel, {}, null, stateName !== "normal", true);
        if (text) {
          state.style.text = text;
        }
        var isShown = labelStateModel.get("show");
        if (isShown != null && !isNormal) {
          state.ignore = !isShown;
        }
        var labelPosition = getLabelAttr(labelStateModel, "position");
        var sectorState = isNormal ? sector : sector.states[stateName];
        var labelColor = sectorState.style.fill;
        sectorState.textConfig = {
          outsideFill: labelStateModel.get("color") === "inherit" ? labelColor : null,
          inside: labelPosition !== "outside"
        };
        var r;
        var labelPadding = getLabelAttr(labelStateModel, "distance") || 0;
        var textAlign = getLabelAttr(labelStateModel, "align");
        if (labelPosition === "outside") {
          r = layout.r + labelPadding;
          textAlign = midAngle > Math.PI / 2 ? "right" : "left";
        } else {
          if (!textAlign || textAlign === "center") {
            if (angle === 2 * Math.PI && layout.r0 === 0) {
              r = 0;
            } else {
              r = (layout.r + layout.r0) / 2;
            }
            textAlign = "center";
          } else if (textAlign === "left") {
            r = layout.r0 + labelPadding;
            if (midAngle > Math.PI / 2) {
              textAlign = "right";
            }
          } else if (textAlign === "right") {
            r = layout.r - labelPadding;
            if (midAngle > Math.PI / 2) {
              textAlign = "left";
            }
          }
        }
        state.style.align = textAlign;
        state.style.verticalAlign = getLabelAttr(labelStateModel, "verticalAlign") || "middle";
        state.x = r * dx + layout.cx;
        state.y = r * dy + layout.cy;
        var rotateType = getLabelAttr(labelStateModel, "rotate");
        var rotate = 0;
        if (rotateType === "radial") {
          rotate = util$1.normalizeRadian(-midAngle);
          if (rotate > Math.PI / 2 && rotate < Math.PI * 1.5) {
            rotate += Math.PI;
          }
        } else if (rotateType === "tangential") {
          rotate = Math.PI / 2 - midAngle;
          if (rotate > Math.PI / 2) {
            rotate -= Math.PI;
          } else if (rotate < -Math.PI / 2) {
            rotate += Math.PI;
          }
        } else if (util.isNumber(rotateType)) {
          rotate = rotateType * Math.PI / 180;
        }
        state.rotation = util$1.normalizeRadian(rotate);
      });
      function getLabelAttr(model, name) {
        var stateAttr = model.get(name);
        if (stateAttr == null) {
          return normalLabelModel.get(name);
        }
        return stateAttr;
      }
      label.dirtyStyle();
    };
    return SunburstPiece2;
  }(Sector.default)
);
const SunburstPiece$1 = SunburstPiece;
exports.default = SunburstPiece$1;
