import { assign as q, omit as C, isNumber as P, find as _ } from "../../min-dash/dist/index.esm.mjs";
import { remove as I$1, query as K, domify as G, assignStyle as B, event as m } from "../../min-dom/dist/index.esm.mjs";
import { innerSVG as $ } from "../../tiny-svg/dist/index.esm.mjs";
import ie from "../../bpmn-moddle/dist/index.esm.mjs";
import t from "../../inherits-browser/dist/index.es.mjs";
import { importBpmnDiagram as y } from "./import/Importer.mjs";
import { wrapForCompatibility as u } from "./util/CompatibilityUtil.mjs";
import { LOGO_STYLES as h, LINK_STYLES as l$1, open as m$1, BPMNIO_IMG as n } from "./util/PoweredByUtil.mjs";
import r from "../../diagram-js/lib/Diagram.mjs";
function s(t2) {
  t2 = q({}, j, t2), this._moddle = this._createModdle(t2), this._container = this._createContainer(t2), O(this._container), this._init(this._container, this._moddle, t2);
}
t(s, r);
s.prototype.importXML = u(async function(e, n2) {
  const r2 = this;
  function c(o) {
    const a = r2.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 m2 = 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: m2,
      warnings: i
    })) || a;
    const b = await this.importDefinitions(a, n2);
    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 = u(async function(e, n2) {
  return this._setDefinitions(e), { warnings: (await this.open(n2)).warnings };
});
s.prototype.open = u(async function(e) {
  const n2 = this._definitions;
  let r2 = e;
  if (!n2) {
    const i = new Error("no XML imported");
    throw l(i, []), i;
  }
  if (typeof e == "string" && (r2 = I(n2, e), !r2)) {
    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 y(this, n2, r2);
  return { warnings: c };
});
s.prototype.saveXML = u(async function(e) {
  e = e || {};
  let n2 = this._definitions, r2, c;
  try {
    if (!n2)
      throw new Error("no definitions loaded");
    n2 = this._emit("saveXML.start", {
      definitions: n2
    }) || n2, c = (await this._moddle.toXML(n2, e)).xml, c = this._emit("saveXML.serialized", {
      xml: c
    }) || c;
  } catch (o) {
    r2 = o;
  }
  const i = r2 ? { error: r2 } : { xml: c };
  if (this._emit("saveXML.done", i), r2)
    throw r2;
  return i;
});
s.prototype.saveSVG = u(async function(e = {}) {
  this._emit("saveSVG.start");
  let n2, r2;
  try {
    const c = this.get("canvas"), i = c.getActiveLayer(), o = K("defs", c._svg), a = $(i), m2 = o ? "<defs>" + $(o) + "</defs>" : "", d = i.getBBox();
    n2 = `<?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">' + m2 + a + "</svg>";
  } catch (c) {
    r2 = c;
  }
  if (this._emit("saveSVG.done", {
    error: r2,
    svg: n2
  }), r2)
    throw r2;
  return { svg: n2 };
});
s.prototype._setDefinitions = function(t2) {
  this._definitions = t2;
};
s.prototype.getModules = function() {
  return this._modules;
};
s.prototype.clear = function() {
  this.getDefinitions() && r.prototype.clear.call(this);
};
s.prototype.destroy = function() {
  r.prototype.destroy.call(this), I$1(this._container);
};
s.prototype.on = function(t2, e, n2, r2) {
  return this.get("eventBus").on(t2, e, n2, r2);
};
s.prototype.off = function(t2, e) {
  this.get("eventBus").off(t2, e);
};
s.prototype.attachTo = function(t2) {
  if (!t2)
    throw new Error("parentNode required");
  this.detach(), t2.get && t2.constructor.prototype.jquery && (t2 = t2.get(0)), typeof t2 == "string" && (t2 = K(t2)), t2.appendChild(this._container), this._emit("attach", {}), this.get("canvas").resized();
};
s.prototype.getDefinitions = function() {
  return this._definitions;
};
s.prototype.detach = function() {
  const t2 = this._container, e = t2.parentNode;
  e && (this._emit("detach", {}), e.removeChild(t2));
};
s.prototype._init = function(t2, e, n2) {
  const r$1 = n2.modules || this.getModules(), c = n2.additionalModules || [], i = [
    {
      bpmnjs: ["value", this],
      moddle: ["value", e]
    }
  ], o = [].concat(i, r$1, c), a = q(C(n2, ["additionalModules"]), {
    canvas: q({}, n2.canvas, { container: t2 }),
    modules: o
  });
  r.call(this, a), n2 && n2.container && this.attachTo(n2.container);
};
s.prototype._emit = function(t2, e) {
  return this.get("eventBus").fire(t2, e);
};
s.prototype._createContainer = function(t2) {
  const e = G('<div class="bjs-container"></div>');
  return B(e, {
    width: v(t2.width),
    height: v(t2.height),
    position: t2.position
  }), e;
};
s.prototype._createModdle = function(t2) {
  const e = q({}, this._moddleExtensions, t2.moddleExtensions);
  return new ie(e);
};
s.prototype._modules = [];
function l(t2, e) {
  return t2.warnings = e, t2;
}
function k(t2) {
  const n2 = /unparsable content <([^>]+)> detected([\s\S]*)$/.exec(t2.message);
  return n2 && (t2.message = "unparsable content <" + n2[1] + "> detected; this may indicate an invalid BPMN 2.0 diagram file" + n2[2]), t2;
}
const j = {
  width: "100%",
  height: "100%",
  position: "relative"
};
function v(t2) {
  return t2 + (P(t2) ? "px" : "");
}
function I(t2, e) {
  return e && _(t2.diagrams, function(n2) {
    return n2.id === e;
  }) || null;
}
function O(t2) {
  const n$1 = '<a href="http://bpmn.io" target="_blank" class="bjs-powered-by" title="Powered by bpmn.io" >' + n + "</a>", r2 = G(n$1);
  B(K("svg", r2), h), B(r2, l$1, {
    position: "absolute",
    bottom: "15px",
    right: "15px",
    zIndex: "100"
  }), t2.appendChild(r2), m.bind(r2, "click", function(c) {
    m$1(), c.preventDefault();
  });
}
export {
  s as default
};
