"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("../../../../tiny-svg/dist/index.esm.js");
const Elements = require("../../util/Elements.js");
var MARKER_HOVER = "hover", MARKER_SELECTED = "selected";
var SELECTION_OUTLINE_PADDING = 6;
function SelectionVisuals(canvas, eventBus, selection) {
  this._canvas = canvas;
  var self = this;
  this._multiSelectionBox = null;
  function addMarker(e, cls) {
    canvas.addMarker(e, cls);
  }
  function removeMarker(e, cls) {
    canvas.removeMarker(e, cls);
  }
  eventBus.on("element.hover", function(event) {
    addMarker(event.element, MARKER_HOVER);
  });
  eventBus.on("element.out", function(event) {
    removeMarker(event.element, MARKER_HOVER);
  });
  eventBus.on("selection.changed", function(event) {
    function deselect(s) {
      removeMarker(s, MARKER_SELECTED);
    }
    function select(s) {
      addMarker(s, MARKER_SELECTED);
    }
    var oldSelection = event.oldSelection, newSelection = event.newSelection;
    index_esm.forEach(oldSelection, function(e) {
      if (newSelection.indexOf(e) === -1) {
        deselect(e);
      }
    });
    index_esm.forEach(newSelection, function(e) {
      if (oldSelection.indexOf(e) === -1) {
        select(e);
      }
    });
    self._updateSelectionOutline(newSelection);
  });
  eventBus.on("element.changed", function(event) {
    if (selection.isSelected(event.element)) {
      self._updateSelectionOutline(selection.get());
    }
  });
}
SelectionVisuals.$inject = [
  "canvas",
  "eventBus",
  "selection"
];
SelectionVisuals.prototype._updateSelectionOutline = function(selection) {
  var layer = this._canvas.getLayer("selectionOutline");
  index_esm$1.clear(layer);
  var enabled = selection.length > 1;
  var container = this._canvas.getContainer();
  index_esm$1.classes(container)[enabled ? "add" : "remove"]("djs-multi-select");
  if (!enabled) {
    return;
  }
  var bBox = addSelectionOutlinePadding(Elements.getBBox(selection));
  var rect = index_esm$1.create("rect");
  index_esm$1.attr(rect, index_esm.assign({
    rx: 3
  }, bBox));
  index_esm$1.classes(rect).add("djs-selection-outline");
  index_esm$1.append(layer, rect);
};
function addSelectionOutlinePadding(bBox) {
  return {
    x: bBox.x - SELECTION_OUTLINE_PADDING,
    y: bBox.y - SELECTION_OUTLINE_PADDING,
    width: bBox.width + SELECTION_OUTLINE_PADDING * 2,
    height: bBox.height + SELECTION_OUTLINE_PADDING * 2
  };
}
exports.default = SelectionVisuals;
