import { C as l, w as z, x as f, d as N, a as y, k as B, t as L, y as x, z as Z, B as u, G as p, M as k } from "./am-chart.H2jnOzFc.js";
class m extends l {
  constructor() {
    super(...arguments), this._movePoints = {}, this._downScale = 1, this._downX = 0, this._downY = 0, this.contents = this.children.push(l.new(this._root, {
      width: f,
      height: f,
      x: 0,
      y: 0,
      draggable: !0,
      background: z.new(this._root, {
        fill: N(16777215),
        fillOpacity: 0
      })
    }));
  }
  _afterNew() {
    super._afterNew();
    const i = this.contents.events;
    this._disposers.push(i.on("pointerdown", (t) => {
      this._handleThisDown(t);
    })), this._disposers.push(i.on("globalpointerup", (t) => {
      this._handleThisUp(t);
    })), this._disposers.push(i.on("globalpointermove", (t) => {
      this._handleThisMove(t);
    }));
    const e = this.contents.get("background");
    e && (e.adapters.add("width", (t) => Number(t) * 20), e.adapters.add("height", (t) => Number(t) * 20), e.adapters.add("x", (t) => Number(t) - e.width() / 2), e.adapters.add("y", (t) => Number(t) - e.height() / 2));
    const o = this.contents;
    o.adapters.add("x", (t) => {
      const s = o.get("scale", 1);
      if (y(t) && s >= 1) {
        let n = this.get("maxPanOut", 0.4), h = o.width();
        t = Math.min(h * n, t), t = Math.max(this.width() - h * s * (1 + n), t);
      }
      return t;
    }), o.adapters.add("y", (t) => {
      const s = o.get("scale", 1);
      if (y(t) && s >= 1) {
        let n = this.get("maxPanOut", 0.4), h = o.height();
        t = Math.min(h * n, t), t = Math.max(this.height() - h * s * (1 + n), t);
      }
      return t;
    });
  }
  _prepareChildren() {
    super._prepareChildren(), this.isDirty("wheelable") && this._handleSetWheel(), this.contents._display.cancelTouch = this.get("pinchZoom", !1);
  }
  _handleSetWheel() {
    this.get("wheelable") ? (this._wheelDp && this._wheelDp.dispose(), this._wheelDp = this.events.on("wheel", (i) => {
      const e = i.originalEvent;
      if (B(e, this))
        e.preventDefault();
      else
        return;
      const o = this.toLocal(i.point);
      this._handleWheelZoom(e.deltaY, o);
    }), this._disposers.push(this._wheelDp)) : this._wheelDp && this._wheelDp.dispose();
  }
  _handleWheelZoom(i, e) {
    let o = this.get("zoomStep", 2), t = this.contents.get("scale", 1), s = t;
    i > 0 ? s = t / o : i < 0 && (s = t * o), s != t && this.zoomToPoint(e, s);
  }
  /**
   * Zooms to specific X/Y point.
   *
   * @param   point  Center point
   * @param   level  Zoom level
   * @return         Zoom Animation object
   */
  zoomToPoint(i, e) {
    e && (e = L(e, this.get("minZoomLevel", 1), this.get("maxZoomLevel", 32)));
    const o = this.contents.get("scale", 1);
    let t = i.x, s = i.y, n = t, h = s;
    const r = this.contents;
    let _ = r.x(), a = r.y(), c = n - (t - _) / o * e, d = h - (s - a) / o * e;
    return this._animateTo(c, d, e), this._za;
  }
  /**
   * Zooms the container contents in by `zoomStep`.
   *
   * @return Zoom Animation object
   */
  zoomIn() {
    return this.zoomToPoint({ x: this.width() / 2, y: this.height() / 2 }, this.contents.get("scale", 1) * this.get("zoomStep", 2));
  }
  /**
   * Zooms the container contents out by `zoomStep`.
   *
   * @return Zoom Animation object
   */
  zoomOut() {
    return this.zoomToPoint({ x: this.width() / 2, y: this.height() / 2 }, this.contents.get("scale", 1) / this.get("zoomStep", 2));
  }
  /**
   * Fully zooms out the container contents.
   *
   * @return Zoom Animation object
   */
  goHome() {
    return this._animateTo(0, 0, 1);
  }
  _animateTo(i, e, o) {
    const t = this.get("animationDuration", 0), s = this.get("animationEasing"), n = this.contents;
    this._txa = n.animate({ key: "x", to: i, duration: t, easing: s }), this._tya = n.animate({ key: "y", to: e, duration: t, easing: s }), this._za = n.animate({ key: "scale", to: o, duration: t, easing: s });
  }
  _handleThisUp(i) {
    this._downPoints = {};
  }
  _handleThisDown(i) {
    const e = this.contents;
    this._downScale = e.get("scale", 1);
    const o = e._downPoints;
    let t = x(o).length;
    if (t == 1) {
      let s = o[1];
      s || (s = o[0]), s && s.x == i.point.x && s.y == i.point.y && (t = 0);
    }
    if (t > 0) {
      this._downX = e.x(), this._downY = e.y();
      const s = e._getDownPointId();
      if (s) {
        let n = this._movePoints[s];
        n && (e._downPoints[s] = n);
      }
    }
  }
  _handleThisMove(i) {
    const o = i.originalEvent.pointerId;
    if (this.get("pinchZoom") && o && (this._movePoints[o] = i.point, x(this.contents._downPoints).length > 1)) {
      this._handlePinch();
      return;
    }
  }
  _handlePinch() {
    let i = 0, e = [], o = [];
    if (Z(this.contents._downPoints, (t, s) => {
      e[i] = s;
      let n = this._movePoints[t];
      n && (o[i] = n), i++;
    }), e.length > 1 && o.length > 1) {
      this.contents._isDragging = !1;
      let t = e[0], s = e[1], n = o[0], h = o[1];
      if (t && s && n && h) {
        t = this.toLocal(t), s = this.toLocal(s), n = this.toLocal(n), h = this.toLocal(h);
        let r = Math.hypot(s.x - t.x, s.y - t.y), a = Math.hypot(h.x - n.x, h.y - n.y) / r * this._downScale, c = { x: n.x + (h.x - n.x) / 2, y: n.y + (h.y - n.y) / 2 }, d = { x: t.x + (s.x - t.x) / 2, y: t.y + (s.y - t.y) / 2 }, P = this._downX || 0, T = this._downY || 0, w = this._downScale, D = c.x - (-P + d.x) / w * a, b = c.y - (-T + d.y) / w * a;
        this.contents.setAll({
          x: D,
          y: b,
          scale: a
        });
      }
    }
  }
}
m.className = "ZoomableContainer";
m.classNames = l.classNames.concat([m.className]);
class g extends l {
  constructor() {
    super(...arguments), this.homeButton = this.children.push(u.new(this._root, { width: 35, height: 35, themeTags: ["home"] })), this.plusButton = this.children.push(u.new(this._root, { width: 35, height: 35, themeTags: ["plus"] })), this.minusButton = this.children.push(u.new(this._root, { width: 35, height: 35, themeTags: ["minus"] }));
  }
  _afterNew() {
    super._afterNew(), this.set("position", "absolute"), this.set("layout", this._root.verticalLayout), this.addTag("zoomtools"), this.plusButton.setAll({
      icon: p.new(this._root, { themeTags: ["icon"] }),
      layout: void 0
    }), this.minusButton.setAll({
      icon: p.new(this._root, { themeTags: ["icon"] }),
      layout: void 0
    }), this.homeButton.setAll({
      icon: p.new(this._root, { themeTags: ["icon"] }),
      layout: void 0
    });
  }
  _prepareChildren() {
    if (super._prepareChildren(), this.isDirty("target")) {
      const i = this.get("target"), e = this._prevSettings.target;
      i && (i instanceof m && (this._targetDisposer = this.addDisposer(i.contents.on("scale", (o) => {
        o == i.get("minZoomLevel") ? this.minusButton.set("disabled", !0) : this.minusButton.set("disabled", !1), o == i.get("maxZoomLevel") ? this.plusButton.set("disabled", !0) : this.plusButton.set("disabled", !1);
      })), this.root.events.once("frameended", () => {
        i.get("scale") == i.get("minZoomLevel") && this.minusButton.set("disabled", !0);
      })), this._disposer = new k([
        this.plusButton.events.on("click", () => {
          i.zoomIn();
        }),
        this.minusButton.events.on("click", () => {
          i.zoomOut();
        }),
        this.homeButton.events.on("click", () => {
          i.goHome();
        })
      ])), e && (this._disposer && this._disposer.dispose(), this._targetDisposer && this._targetDisposer.dispose());
    }
  }
}
g.className = "ZoomTools";
g.classNames = l.classNames.concat([g.className]);
export {
  m as Z,
  g as a
};
