"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const tslib_es6 = require("../../../../node_modules/tslib/tslib.es6.js");
const echarts = require("../../../core/echarts.js");
const util = require("../../../../../../../zrender@5.4.4/node_modules/zrender/lib/core/util.js");
const featureManager = require("../featureManager.js");
const model = require("../../../util/model.js");
var INNER_STACK_KEYWORD = "__ec_magicType_stack__";
var radioTypes = [["line", "bar"], ["stack"]];
var MagicType = (
  /** @class */
  function(_super) {
    tslib_es6.__extends(MagicType2, _super);
    function MagicType2() {
      return _super !== null && _super.apply(this, arguments) || this;
    }
    MagicType2.prototype.getIcons = function() {
      var model2 = this.model;
      var availableIcons = model2.get("icon");
      var icons = {};
      util.each(model2.get("type"), function(type) {
        if (availableIcons[type]) {
          icons[type] = availableIcons[type];
        }
      });
      return icons;
    };
    MagicType2.getDefaultOption = function(ecModel) {
      var defaultOption = {
        show: true,
        type: [],
        // Icon group
        icon: {
          line: "M4.1,28.9h7.1l9.3-22l7.4,38l9.7-19.7l3,12.8h14.9M4.1,58h51.4",
          bar: "M6.7,22.9h10V48h-10V22.9zM24.9,13h10v35h-10V13zM43.2,2h10v46h-10V2zM3.1,58h53.7",
          // eslint-disable-next-line
          stack: "M8.2,38.4l-8.4,4.1l30.6,15.3L60,42.5l-8.1-4.1l-21.5,11L8.2,38.4z M51.9,30l-8.1,4.2l-13.4,6.9l-13.9-6.9L8.2,30l-8.4,4.2l8.4,4.2l22.2,11l21.5-11l8.1-4.2L51.9,30z M51.9,21.7l-8.1,4.2L35.7,30l-5.3,2.8L24.9,30l-8.4-4.1l-8.3-4.2l-8.4,4.2L8.2,30l8.3,4.2l13.9,6.9l13.4-6.9l8.1-4.2l8.1-4.1L51.9,21.7zM30.4,2.2L-0.2,17.5l8.4,4.1l8.3,4.2l8.4,4.2l5.5,2.7l5.3-2.7l8.1-4.2l8.1-4.2l8.1-4.1L30.4,2.2z"
          // jshint ignore:line
        },
        // `line`, `bar`, `stack`, `tiled`
        title: ecModel.getLocaleModel().get(["toolbox", "magicType", "title"]),
        option: {},
        seriesIndex: {}
      };
      return defaultOption;
    };
    MagicType2.prototype.onclick = function(ecModel, api, type) {
      var model$1 = this.model;
      var seriesIndex = model$1.get(["seriesIndex", type]);
      if (!seriesOptGenreator[type]) {
        return;
      }
      var newOption = {
        series: []
      };
      var generateNewSeriesTypes = function(seriesModel) {
        var seriesType = seriesModel.subType;
        var seriesId = seriesModel.id;
        var newSeriesOpt = seriesOptGenreator[type](seriesType, seriesId, seriesModel, model$1);
        if (newSeriesOpt) {
          util.defaults(newSeriesOpt, seriesModel.option);
          newOption.series.push(newSeriesOpt);
        }
        var coordSys = seriesModel.coordinateSystem;
        if (coordSys && coordSys.type === "cartesian2d" && (type === "line" || type === "bar")) {
          var categoryAxis = coordSys.getAxesByScale("ordinal")[0];
          if (categoryAxis) {
            var axisDim = categoryAxis.dim;
            var axisType = axisDim + "Axis";
            var axisModel = seriesModel.getReferringComponents(axisType, model.SINGLE_REFERRING).models[0];
            var axisIndex = axisModel.componentIndex;
            newOption[axisType] = newOption[axisType] || [];
            for (var i = 0; i <= axisIndex; i++) {
              newOption[axisType][axisIndex] = newOption[axisType][axisIndex] || {};
            }
            newOption[axisType][axisIndex].boundaryGap = type === "bar";
          }
        }
      };
      util.each(radioTypes, function(radio) {
        if (util.indexOf(radio, type) >= 0) {
          util.each(radio, function(item) {
            model$1.setIconStatus(item, "normal");
          });
        }
      });
      model$1.setIconStatus(type, "emphasis");
      ecModel.eachComponent({
        mainType: "series",
        query: seriesIndex == null ? null : {
          seriesIndex
        }
      }, generateNewSeriesTypes);
      var newTitle;
      var currentType = type;
      if (type === "stack") {
        newTitle = util.merge({
          stack: model$1.option.title.tiled,
          tiled: model$1.option.title.stack
        }, model$1.option.title);
        if (model$1.get(["iconStatus", type]) !== "emphasis") {
          currentType = "tiled";
        }
      }
      api.dispatchAction({
        type: "changeMagicType",
        currentType,
        newOption,
        newTitle,
        featureName: "magicType"
      });
    };
    return MagicType2;
  }(featureManager.ToolboxFeature)
);
var seriesOptGenreator = {
  "line": function(seriesType, seriesId, seriesModel, model2) {
    if (seriesType === "bar") {
      return util.merge({
        id: seriesId,
        type: "line",
        // Preserve data related option
        data: seriesModel.get("data"),
        stack: seriesModel.get("stack"),
        markPoint: seriesModel.get("markPoint"),
        markLine: seriesModel.get("markLine")
      }, model2.get(["option", "line"]) || {}, true);
    }
  },
  "bar": function(seriesType, seriesId, seriesModel, model2) {
    if (seriesType === "line") {
      return util.merge({
        id: seriesId,
        type: "bar",
        // Preserve data related option
        data: seriesModel.get("data"),
        stack: seriesModel.get("stack"),
        markPoint: seriesModel.get("markPoint"),
        markLine: seriesModel.get("markLine")
      }, model2.get(["option", "bar"]) || {}, true);
    }
  },
  "stack": function(seriesType, seriesId, seriesModel, model2) {
    var isStack = seriesModel.get("stack") === INNER_STACK_KEYWORD;
    if (seriesType === "line" || seriesType === "bar") {
      model2.setIconStatus("stack", isStack ? "normal" : "emphasis");
      return util.merge({
        id: seriesId,
        stack: isStack ? "" : INNER_STACK_KEYWORD
      }, model2.get(["option", "stack"]) || {}, true);
    }
  }
};
echarts.registerAction({
  type: "changeMagicType",
  event: "magicTypeChanged",
  update: "prepareAndUpdate"
}, function(payload, ecModel) {
  ecModel.mergeOption(payload.newOption);
});
const MagicType$1 = MagicType;
exports.default = MagicType$1;
