import { closest as D, event as m } from "../../../../min-dom/dist/index.esm.mjs";
import { getStepSize as o, cap as u } from "./ZoomUtil.mjs";
import { log10 as t } from "../../util/Math.mjs";
import { bind as U } from "../../../../min-dash/dist/index.esm.mjs";
var y = Math.sign || function(o2) {
  return o2 >= 0 ? 1 : -1;
}, h = { min: 0.2, max: 4 }, f = 10, g = 0.1, z = 0.75;
function i(o2, t2, e) {
  o2 = o2 || {}, this._enabled = false, this._canvas = e, this._container = e._container, this._handleWheel = U(this._handleWheel, this), this._totalDelta = 0, this._scale = o2.scale || z;
  var a = this;
  t2.on("canvas.init", function(s) {
    a._init(o2.enabled !== false);
  });
}
i.$inject = [
  "config.zoomScroll",
  "eventBus",
  "canvas"
];
i.prototype.scroll = function(t2) {
  this._canvas.scroll(t2);
};
i.prototype.reset = function() {
  this._canvas.zoom("fit-viewport");
};
i.prototype.zoom = function(t2, e) {
  var a = o(h, f * 2);
  this._totalDelta += t2, Math.abs(this._totalDelta) > g && (this._zoom(t2, e, a), this._totalDelta = 0);
};
i.prototype._handleWheel = function(t2) {
  if (!D(t2.target, ".djs-scrollable", true)) {
    var e = this._container;
    t2.preventDefault();
    var a = t2.ctrlKey, s = t2.shiftKey, l = -1 * this._scale, r;
    if (a ? l *= t2.deltaMode === 0 ? 0.02 : 0.32 : l *= t2.deltaMode === 0 ? 1 : 16, a) {
      var n = e.getBoundingClientRect(), p = {
        x: t2.clientX - n.left,
        y: t2.clientY - n.top
      };
      r = Math.sqrt(
        Math.pow(t2.deltaY, 2) + Math.pow(t2.deltaX, 2)
      ) * y(t2.deltaY) * l, this.zoom(r, p);
    } else
      s ? r = {
        dx: l * t2.deltaY,
        dy: 0
      } : r = {
        dx: l * t2.deltaX,
        dy: l * t2.deltaY
      }, this.scroll(r);
  }
};
i.prototype.stepZoom = function(t2, e) {
  var a = o(h, f);
  this._zoom(t2, e, a);
};
i.prototype._zoom = function(o2, t$1, e) {
  var a = this._canvas, s = o2 > 0 ? 1 : -1, l = t(a.zoom()), r = Math.round(l / e) * e;
  r += e * s;
  var n = Math.pow(10, r);
  a.zoom(u(h, n), t$1);
};
i.prototype.toggle = function(t2) {
  var e = this._container, a = this._handleWheel, s = this._enabled;
  return typeof t2 > "u" && (t2 = !s), s !== t2 && m[t2 ? "bind" : "unbind"](e, "wheel", a, false), this._enabled = t2, t2;
};
i.prototype._init = function(o2) {
  this.toggle(o2);
};
export {
  i as default
};
