"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm = require("../../../../min-dash/dist/index.esm.js");
const index_esm$1 = require("../../../../min-dom/dist/index.esm.js");
const Mouse = require("../../util/Mouse.js");
const index_esm$2 = require("../../../../tiny-svg/dist/index.esm.js");
const RenderUtil = require("../../util/RenderUtil.js");
function V(r) {
  return true;
}
function u(r) {
  return Mouse.isPrimaryButton(r) || Mouse.isAuxiliaryButton(r);
}
var k = 500;
function G(r, h, j) {
  var m = this;
  function c(e, t, i) {
    if (!L(e, t)) {
      var n, o, s;
      i ? o = h.getGraphics(i) : (n = t.delegateTarget || t.target, n && (o = n, i = h.get(o))), !(!o || !i) && (s = r.fire(e, {
        element: i,
        gfx: o,
        originalEvent: t
      }), s === false && (t.stopPropagation(), t.preventDefault()));
    }
  }
  var g = {};
  function v(e) {
    return g[e];
  }
  function L(e, t) {
    var i = p[e] || Mouse.isPrimaryButton;
    return !i(t);
  }
  var l = {
    click: "element.click",
    contextmenu: "element.contextmenu",
    dblclick: "element.dblclick",
    mousedown: "element.mousedown",
    mousemove: "element.mousemove",
    mouseover: "element.hover",
    mouseout: "element.out",
    mouseup: "element.mouseup"
  }, p = {
    "element.contextmenu": V,
    "element.mousedown": u,
    "element.mouseup": u,
    "element.click": u,
    "element.dblclick": u
  };
  function I(e, t, i) {
    var n = l[e];
    if (!n)
      throw new Error("unmapped DOM event name <" + e + ">");
    return c(n, t, i);
  }
  var S = "svg, .djs-element";
  function E(e, t, i, n) {
    var o = g[i] = function(s) {
      c(i, s);
    };
    n && (p[i] = n), o.$delegate = index_esm$1.delegate.bind(e, S, t, o);
  }
  function H(e, t, i) {
    var n = v(i);
    n && index_esm$1.delegate.unbind(e, t, n.$delegate);
  }
  function b(e) {
    index_esm.forEach(l, function(t, i) {
      E(e, i, t);
    });
  }
  function O(e) {
    index_esm.forEach(l, function(t, i) {
      H(e, i, t);
    });
  }
  r.on("canvas.destroy", function(e) {
    O(e.svg);
  }), r.on("canvas.init", function(e) {
    b(e.svg);
  }), r.on(["shape.added", "connection.added"], function(e) {
    var t = e.element, i = e.gfx;
    r.fire("interactionEvents.createHit", { element: t, gfx: i });
  }), r.on([
    "shape.changed",
    "connection.changed"
  ], k, function(e) {
    var t = e.element, i = e.gfx;
    r.fire("interactionEvents.updateHit", { element: t, gfx: i });
  }), r.on("interactionEvents.createHit", k, function(e) {
    var t = e.element, i = e.gfx;
    m.createDefaultHit(t, i);
  }), r.on("interactionEvents.updateHit", function(e) {
    var t = e.element, i = e.gfx;
    m.updateDefaultHit(t, i);
  });
  var A = a("djs-hit djs-hit-stroke"), D = a("djs-hit djs-hit-click-stroke"), Y = a("djs-hit djs-hit-all"), M = a("djs-hit djs-hit-no-move"), P = {
    all: Y,
    "click-stroke": D,
    stroke: A,
    "no-move": M
  };
  function a(e, t) {
    return t = index_esm.assign({
      stroke: "white",
      strokeWidth: 15
    }, t || {}), j.cls(e, ["no-fill", "no-border"], t);
  }
  function x(e, t) {
    var i = P[t];
    if (!i)
      throw new Error("invalid hit type <" + t + ">");
    return index_esm$2.attr(e, i), e;
  }
  function w(e, t) {
    index_esm$2.append(e, t);
  }
  this.removeHits = function(e) {
    var t = index_esm$1.queryAll(".djs-hit", e);
    index_esm.forEach(t, index_esm$2.remove);
  }, this.createDefaultHit = function(e, t) {
    var i = e.waypoints, n = e.isFrame, o;
    return i ? this.createWaypointsHit(t, i) : (o = n ? "stroke" : "all", this.createBoxHit(t, o, {
      width: e.width,
      height: e.height
    }));
  }, this.createWaypointsHit = function(e, t) {
    var i = RenderUtil.createLine(t);
    return x(i, "stroke"), w(e, i), i;
  }, this.createBoxHit = function(e, t, i) {
    i = index_esm.assign({
      x: 0,
      y: 0
    }, i);
    var n = index_esm$2.create("rect");
    return x(n, t), index_esm$2.attr(n, i), w(e, n), n;
  }, this.updateDefaultHit = function(e, t) {
    var i = index_esm$1.query(".djs-hit", t);
    if (i)
      return e.waypoints ? RenderUtil.updateLine(i, e.waypoints) : index_esm$2.attr(i, {
        width: e.width,
        height: e.height
      }), i;
  }, this.fire = c, this.triggerMouseEvent = I, this.mouseHandler = v, this.registerEvent = E, this.unregisterEvent = H;
}
G.$inject = [
  "eventBus",
  "elementRegistry",
  "styles"
];
exports.default = G;
