import { forEach, assign } from "../../../../min-dash/dist/index.esm.mjs";
import { clear, classes, create, attr, append } from "../../../../tiny-svg/dist/index.esm.mjs";
import { getBBox } from "../../util/Elements.mjs";
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;
    forEach(oldSelection, function(e) {
      if (newSelection.indexOf(e) === -1) {
        deselect(e);
      }
    });
    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");
  clear(layer);
  var enabled = selection.length > 1;
  var container = this._canvas.getContainer();
  classes(container)[enabled ? "add" : "remove"]("djs-multi-select");
  if (!enabled) {
    return;
  }
  var bBox = addSelectionOutlinePadding(getBBox(selection));
  var rect = create("rect");
  attr(rect, assign({
    rx: 3
  }, bBox));
  classes(rect).add("djs-selection-outline");
  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
  };
}
export {
  SelectionVisuals as default
};
