"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Cursor = require("../../util/Cursor.js");
const ClickTrap = require("../../util/ClickTrap.js");
const PositionUtil = require("../../util/PositionUtil.js");
const index_esm = require("../../../../min-dom/dist/index.esm.js");
const Event = require("../../util/Event.js");
var THRESHOLD = 15;
function MoveCanvas(eventBus, canvas) {
  var context;
  eventBus.on("element.mousedown", 500, function(e) {
    return handleStart(e.originalEvent);
  });
  function handleMove(event) {
    var start = context.start, button = context.button, position = Event.toPoint(event), delta = PositionUtil.delta(position, start);
    if (!context.dragging && length(delta) > THRESHOLD) {
      context.dragging = true;
      if (button === 0) {
        ClickTrap.install(eventBus);
      }
      Cursor.set("grab");
    }
    if (context.dragging) {
      var lastPosition = context.last || context.start;
      delta = PositionUtil.delta(position, lastPosition);
      canvas.scroll({
        dx: delta.x,
        dy: delta.y
      });
      context.last = position;
    }
    event.preventDefault();
  }
  function handleEnd(event) {
    index_esm.event.unbind(document, "mousemove", handleMove);
    index_esm.event.unbind(document, "mouseup", handleEnd);
    context = null;
    Cursor.unset();
  }
  function handleStart(event) {
    if (index_esm.closest(event.target, ".djs-draggable")) {
      return;
    }
    var button = event.button;
    if (button >= 2 || event.ctrlKey || event.shiftKey || event.altKey) {
      return;
    }
    context = {
      button,
      start: Event.toPoint(event)
    };
    index_esm.event.bind(document, "mousemove", handleMove);
    index_esm.event.bind(document, "mouseup", handleEnd);
    return true;
  }
  this.isActive = function() {
    return !!context;
  };
}
MoveCanvas.$inject = [
  "eventBus",
  "canvas"
];
function length(point) {
  return Math.sqrt(Math.pow(point.x, 2) + Math.pow(point.y, 2));
}
exports.default = MoveCanvas;
