"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index_esm$1 = require("../../min-dash/dist/index.esm.js");
const index_esm = require("../../min-dom/dist/index.esm.js");
const index_esm$3 = require("../../tiny-svg/dist/index.esm.js");
const index_esm$2 = require("../../bpmn-moddle/dist/index.esm.js");
const index_es = require("../../inherits-browser/dist/index.es.js");
const Importer = require("./import/Importer.js");
const CompatibilityUtil = require("./util/CompatibilityUtil.js");
const PoweredByUtil = require("./util/PoweredByUtil.js");
const Diagram = require("../../diagram-js/lib/Diagram.js");
function s(t) {
  t = index_esm$1.assign({}, j, t), this._moddle = this._createModdle(t), this._container = this._createContainer(t), O(this._container), this._init(this._container, this._moddle, t);
}
index_es.default(s, Diagram.default);
s.prototype.importXML = CompatibilityUtil.wrapForCompatibility(async function(e, n) {
  const r = this;
  function c(o) {
    const a = r.get("eventBus").createEvent(o);
    return Object.defineProperty(a, "context", {
      enumerable: true,
      get: function() {
        return console.warn(new Error(
          "import.parse.complete <context> is deprecated and will be removed in future library versions"
        )), {
          warnings: o.warnings,
          references: o.references,
          elementsById: o.elementsById
        };
      }
    }), a;
  }
  let i = [];
  try {
    e = this._emit("import.parse.start", { xml: e }) || e;
    let o;
    try {
      o = await this._moddle.fromXML(e, "bpmn:Definitions");
    } catch (w) {
      throw this._emit("import.parse.complete", {
        error: w
      }), w;
    }
    let a = o.rootElement;
    const m = o.references, d = o.warnings, M = o.elementsById;
    i = i.concat(d), a = this._emit("import.parse.complete", c({
      error: null,
      definitions: a,
      elementsById: M,
      references: m,
      warnings: i
    })) || a;
    const b = await this.importDefinitions(a, n);
    return i = i.concat(b.warnings), this._emit("import.done", { error: null, warnings: i }), { warnings: i };
  } catch (o) {
    let a = o;
    throw i = i.concat(a.warnings || []), l(a, i), a = k(a), this._emit("import.done", { error: a, warnings: a.warnings }), a;
  }
});
s.prototype.importDefinitions = CompatibilityUtil.wrapForCompatibility(async function(e, n) {
  return this._setDefinitions(e), { warnings: (await this.open(n)).warnings };
});
s.prototype.open = CompatibilityUtil.wrapForCompatibility(async function(e) {
  const n = this._definitions;
  let r = e;
  if (!n) {
    const i = new Error("no XML imported");
    throw l(i, []), i;
  }
  if (typeof e == "string" && (r = I(n, e), !r)) {
    const i = new Error("BPMNDiagram <" + e + "> not found");
    throw l(i, []), i;
  }
  try {
    this.clear();
  } catch (i) {
    throw l(i, []), i;
  }
  const { warnings: c } = await Importer.importBpmnDiagram(this, n, r);
  return { warnings: c };
});
s.prototype.saveXML = CompatibilityUtil.wrapForCompatibility(async function(e) {
  e = e || {};
  let n = this._definitions, r, c;
  try {
    if (!n)
      throw new Error("no definitions loaded");
    n = this._emit("saveXML.start", {
      definitions: n
    }) || n, c = (await this._moddle.toXML(n, e)).xml, c = this._emit("saveXML.serialized", {
      xml: c
    }) || c;
  } catch (o) {
    r = o;
  }
  const i = r ? { error: r } : { xml: c };
  if (this._emit("saveXML.done", i), r)
    throw r;
  return i;
});
s.prototype.saveSVG = CompatibilityUtil.wrapForCompatibility(async function(e = {}) {
  this._emit("saveSVG.start");
  let n, r;
  try {
    const c = this.get("canvas"), i = c.getActiveLayer(), o = index_esm.query("defs", c._svg), a = index_esm$3.innerSVG(i), m = o ? "<defs>" + index_esm$3.innerSVG(o) + "</defs>" : "", d = i.getBBox();
    n = `<?xml version="1.0" encoding="utf-8"?>
<!-- created with bpmn-js / http://bpmn.io -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="` + d.width + '" height="' + d.height + '" viewBox="' + d.x + " " + d.y + " " + d.width + " " + d.height + '" version="1.1">' + m + a + "</svg>";
  } catch (c) {
    r = c;
  }
  if (this._emit("saveSVG.done", {
    error: r,
    svg: n
  }), r)
    throw r;
  return { svg: n };
});
s.prototype._setDefinitions = function(t) {
  this._definitions = t;
};
s.prototype.getModules = function() {
  return this._modules;
};
s.prototype.clear = function() {
  this.getDefinitions() && Diagram.default.prototype.clear.call(this);
};
s.prototype.destroy = function() {
  Diagram.default.prototype.destroy.call(this), index_esm.remove(this._container);
};
s.prototype.on = function(t, e, n, r) {
  return this.get("eventBus").on(t, e, n, r);
};
s.prototype.off = function(t, e) {
  this.get("eventBus").off(t, e);
};
s.prototype.attachTo = function(t) {
  if (!t)
    throw new Error("parentNode required");
  this.detach(), t.get && t.constructor.prototype.jquery && (t = t.get(0)), typeof t == "string" && (t = index_esm.query(t)), t.appendChild(this._container), this._emit("attach", {}), this.get("canvas").resized();
};
s.prototype.getDefinitions = function() {
  return this._definitions;
};
s.prototype.detach = function() {
  const t = this._container, e = t.parentNode;
  e && (this._emit("detach", {}), e.removeChild(t));
};
s.prototype._init = function(t, e, n) {
  const r = n.modules || this.getModules(), c = n.additionalModules || [], i = [
    {
      bpmnjs: ["value", this],
      moddle: ["value", e]
    }
  ], o = [].concat(i, r, c), a = index_esm$1.assign(index_esm$1.omit(n, ["additionalModules"]), {
    canvas: index_esm$1.assign({}, n.canvas, { container: t }),
    modules: o
  });
  Diagram.default.call(this, a), n && n.container && this.attachTo(n.container);
};
s.prototype._emit = function(t, e) {
  return this.get("eventBus").fire(t, e);
};
s.prototype._createContainer = function(t) {
  const e = index_esm.domify('<div class="bjs-container"></div>');
  return index_esm.assignStyle(e, {
    width: v(t.width),
    height: v(t.height),
    position: t.position
  }), e;
};
s.prototype._createModdle = function(t) {
  const e = index_esm$1.assign({}, this._moddleExtensions, t.moddleExtensions);
  return new index_esm$2.default(e);
};
s.prototype._modules = [];
function l(t, e) {
  return t.warnings = e, t;
}
function k(t) {
  const n = /unparsable content <([^>]+)> detected([\s\S]*)$/.exec(t.message);
  return n && (t.message = "unparsable content <" + n[1] + "> detected; this may indicate an invalid BPMN 2.0 diagram file" + n[2]), t;
}
const j = {
  width: "100%",
  height: "100%",
  position: "relative"
};
function v(t) {
  return t + (index_esm$1.isNumber(t) ? "px" : "");
}
function I(t, e) {
  return e && index_esm$1.find(t.diagrams, function(n) {
    return n.id === e;
  }) || null;
}
function O(t) {
  const n = '<a href="http://bpmn.io" target="_blank" class="bjs-powered-by" title="Powered by bpmn.io" >' + PoweredByUtil.BPMNIO_IMG + "</a>", r = index_esm.domify(n);
  index_esm.assignStyle(index_esm.query("svg", r), PoweredByUtil.LOGO_STYLES), index_esm.assignStyle(r, PoweredByUtil.LINK_STYLES, {
    position: "absolute",
    bottom: "15px",
    right: "15px",
    zIndex: "100"
  }), t.appendChild(r), index_esm.event.bind(r, "click", function(c) {
    PoweredByUtil.open(), c.preventDefault();
  });
}
exports.default = s;
