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