"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm$2 = require("../../../min-dash/dist/index.esm.js");
const GraphicsUtil = require("../util/GraphicsUtil.js");
const SvgTransformUtil = require("../util/SvgTransformUtil.js");
const index_esm$1 = require("../../../min-dom/dist/index.esm.js");
const index_esm = require("../../../tiny-svg/dist/index.esm.js");
const Elements = require("../util/Elements.js");
function GraphicsFactory(eventBus, elementRegistry) {
  this._eventBus = eventBus;
  this._elementRegistry = elementRegistry;
}
GraphicsFactory.$inject = ["eventBus", "elementRegistry"];
GraphicsFactory.prototype._getChildrenContainer = function(element) {
  var gfx = this._elementRegistry.getGraphics(element);
  var childrenGfx;
  if (!element.parent) {
    childrenGfx = gfx;
  } else {
    childrenGfx = GraphicsUtil.getChildren(gfx);
    if (!childrenGfx) {
      childrenGfx = index_esm.create("g");
      index_esm.classes(childrenGfx).add("djs-children");
      index_esm.append(gfx.parentNode, childrenGfx);
    }
  }
  return childrenGfx;
};
GraphicsFactory.prototype._clear = function(gfx) {
  var visual = GraphicsUtil.getVisual(gfx);
  index_esm$1.clear(visual);
  return visual;
};
GraphicsFactory.prototype._createContainer = function(type, childrenGfx, parentIndex, isFrame) {
  var outerGfx = index_esm.create("g");
  index_esm.classes(outerGfx).add("djs-group");
  if (typeof parentIndex !== "undefined") {
    prependTo(outerGfx, childrenGfx, childrenGfx.childNodes[parentIndex]);
  } else {
    index_esm.append(childrenGfx, outerGfx);
  }
  var gfx = index_esm.create("g");
  index_esm.classes(gfx).add("djs-element");
  index_esm.classes(gfx).add("djs-" + type);
  if (isFrame) {
    index_esm.classes(gfx).add("djs-frame");
  }
  index_esm.append(outerGfx, gfx);
  var visual = index_esm.create("g");
  index_esm.classes(visual).add("djs-visual");
  index_esm.append(gfx, visual);
  return gfx;
};
GraphicsFactory.prototype.create = function(type, element, parentIndex) {
  var childrenGfx = this._getChildrenContainer(element.parent);
  return this._createContainer(type, childrenGfx, parentIndex, Elements.isFrameElement(element));
};
GraphicsFactory.prototype.updateContainments = function(elements) {
  var self = this, elementRegistry = this._elementRegistry, parents;
  parents = index_esm$2.reduce(elements, function(map, e) {
    if (e.parent) {
      map[e.parent.id] = e.parent;
    }
    return map;
  }, {});
  index_esm$2.forEach(parents, function(parent) {
    var children = parent.children;
    if (!children) {
      return;
    }
    var childrenGfx = self._getChildrenContainer(parent);
    index_esm$2.forEach(children.slice().reverse(), function(child) {
      var childGfx = elementRegistry.getGraphics(child);
      prependTo(childGfx.parentNode, childrenGfx);
    });
  });
};
GraphicsFactory.prototype.drawShape = function(visual, element) {
  var eventBus = this._eventBus;
  return eventBus.fire("render.shape", { gfx: visual, element });
};
GraphicsFactory.prototype.getShapePath = function(element) {
  var eventBus = this._eventBus;
  return eventBus.fire("render.getShapePath", element);
};
GraphicsFactory.prototype.drawConnection = function(visual, element) {
  var eventBus = this._eventBus;
  return eventBus.fire("render.connection", { gfx: visual, element });
};
GraphicsFactory.prototype.getConnectionPath = function(waypoints) {
  var eventBus = this._eventBus;
  return eventBus.fire("render.getConnectionPath", waypoints);
};
GraphicsFactory.prototype.update = function(type, element, gfx) {
  if (!element.parent) {
    return;
  }
  var visual = this._clear(gfx);
  if (type === "shape") {
    this.drawShape(visual, element);
    SvgTransformUtil.translate(gfx, element.x, element.y);
  } else if (type === "connection") {
    this.drawConnection(visual, element);
  } else {
    throw new Error("unknown type: " + type);
  }
  if (element.hidden) {
    index_esm.attr(gfx, "display", "none");
  } else {
    index_esm.attr(gfx, "display", "block");
  }
};
GraphicsFactory.prototype.remove = function(element) {
  var gfx = this._elementRegistry.getGraphics(element);
  index_esm.remove(gfx.parentNode);
};
function prependTo(newNode, parentNode, siblingNode) {
  var node = siblingNode || parentNode.firstChild;
  if (newNode === node) {
    return;
  }
  parentNode.insertBefore(newNode, node);
}
exports.default = GraphicsFactory;
