import { E as d, B as p, G as D, e as h } from "./am-chart.H2jnOzFc.js";
class m extends d {
  _afterNew() {
    super._afterNew(), this._setRawDefault("threshold", 5), this._setRawDefault("groupName", "Other"), this._setRawDefault("clickBehavior", "none"), this.initZoomButton(), this._root.addDisposer(this);
    const e = this.get("series");
    if (e) {
      const t = e.get("colors");
      t && (this.setPrivate("currentStep", t.getPrivate("currentStep")), this.setPrivate("currentPass", t.getPrivate("currentPass")));
    }
  }
  initZoomButton() {
    if (this.get("clickBehavior") !== "none") {
      const t = this.root.tooltipContainer;
      this.zoomOutButton = t.children.push(p.new(this._root, {
        themeTags: ["zoom"],
        icon: D.new(this._root, {
          themeTags: ["button", "icon"]
        })
      })), this.zoomOutButton.hide(), this.zoomOutButton.events.on("click", () => {
        this.zoomOut();
      });
    }
  }
  handleData() {
    const e = this.get("series");
    if (e) {
      let t = this.getPrivate("groupDataItem");
      if (!t) {
        const i = this.get("legend"), l = e.get("categoryField", "category"), n = e.get("valueField", "value"), g = {};
        g[l] = this.get("groupName", ""), g[n] = 0;
        const u = e.get("colors");
        u && (u.setPrivate("currentStep", this.getPrivate("currentStep")), u.setPrivate("currentPass", this.getPrivate("currentPass"))), e.data.push(g), t = e.dataItems[e.dataItems.length - 1], t.get("slice").events.on("click", () => {
          this.handleClick();
        }), this.setPrivate("groupDataItem", t), i && (i.data.push(t), t.on("visible", (v) => {
          v && this.zoomOut();
        }));
      }
      const a = this.get("threshold", 0), o = this.get("limit", 1e3), s = [], r = [];
      let c = 0;
      (a || o) && (h(e.dataItems, (i, l) => {
        const n = i.get("legendDataItem");
        (i.get("valuePercentTotal") <= a || l > o - 1) && t !== i ? (c += i.get("value", 0), r.push(i), i.hide(0), n && n.get("itemContainer").hide(0)) : (s.push(i), n && n.get("itemContainer").show(0));
      }), this.setPrivate("normalDataItems", s), this.setPrivate("smallDataItems", r), this.updateGroupDataItem(c));
    }
  }
  /**
   * Resets slice setup to original grouping state.
   */
  zoomOut() {
    const e = this.getPrivate("groupDataItem");
    if (e && e.show(), this.get("clickBehavior") == "zoom") {
      const o = this.getPrivate("normalDataItems", []);
      h(o, (s, r) => {
        s.show();
      });
    }
    const a = this.getPrivate("smallDataItems", []);
    h(a, (o, s) => {
      o.hide();
    }), this.zoomOutButton && this.zoomOutButton.hide();
  }
  updateGroupDataItem(e) {
    const t = this.get("series");
    if (t) {
      const a = {}, o = t.get("categoryField", "category"), s = t.get("valueField", "value");
      a[o] = this.get("groupName", ""), a[s] = e, t.data.setIndex(t.data.length - 1, a);
      const r = this.getPrivate("groupDataItem");
      e == 0 ? r.hide(0) : r.isHidden() && r.show(), this.get("clickBehavior") != "none" && r.get("slice").set("toggleKey", "none");
    }
  }
  handleClick() {
    const e = this.get("clickBehavior"), t = this.getPrivate("smallDataItems");
    if (e == "none" || t && t.length == 0)
      return;
    const a = this.get("series");
    this.getPrivate("groupDataItem").hide(), h(a.dataItems, (s) => {
      t.indexOf(s) !== -1 ? s.show() : e == "zoom" && s.hide();
    }), this.zoomOutButton.show();
  }
  _beforeChanged() {
    if (super._beforeChanged(), this.isDirty("series")) {
      const e = this.get("series");
      e && e.events.on("datavalidated", (t) => {
        this.removePrivate("groupDataItem"), this.handleData();
      });
    }
  }
}
m.className = "SliceGrouper";
m.classNames = d.classNames.concat([m.className]);
export {
  m as SliceGrouper
};
