const r = (n) => n.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;"), m = (n) => n.replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&gt;/g, ">").replace(/&lt;/g, "<").replace(/&amp;/g, "&");
function f(n) {
  return Object.values(n).flatMap((o) => Object.values(o).flat());
}
function g(n, o = {}) {
  const a = o.processId ?? "flowforge_process", c = o.processName ?? "FlowForge Workflow", s = [...n.slots, ...f(n.branches)], l = r(JSON.stringify(n)), d = s.map((e) => {
    var t, i, p;
    return e.kind === "start" ? `    <bpmn:startEvent id="${r(e.id)}" />` : e.kind === "end" ? `    <bpmn:endEvent id="${r(e.id)}" />` : e.kind === "triggerFixed" ? `    <bpmn:startEvent id="${r(e.id)}" name="${r(String(((t = e.nodeData) == null ? void 0 : t.label) ?? "Trigger"))}" />` : e.kind === "filter" || e.kind === "approval" ? `    <bpmn:exclusiveGateway id="${r(e.id)}" name="${r(String(((i = e.nodeData) == null ? void 0 : i.title) ?? e.kind))}" />` : `    <bpmn:serviceTask id="${r(e.id)}" name="${r(String(((p = e.nodeData) == null ? void 0 : p.title) ?? e.descriptorType ?? "Task"))}" />`;
  });
  return [
    '<?xml version="1.0" encoding="UTF-8"?>',
    '<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:flowforge="https://miniorange.com/flowforge">',
    `  <bpmn:process id="${r(a)}" name="${r(c)}" isExecutable="true">`,
    ...d,
    `    <bpmn:documentation textFormat="application/vnd.flowforge.workflow+json">${l}</bpmn:documentation>`,
    "  </bpmn:process>",
    "</bpmn:definitions>"
  ].join(`
`);
}
function u(n) {
  const o = n.match(/<bpmn:documentation[^>]*application\/vnd\.flowforge\.workflow\+json[^>]*>([\s\S]*?)<\/bpmn:documentation>/);
  if (!o)
    throw new Error("No FlowForge workflow payload found in BPMN XML.");
  const a = JSON.parse(m(o[1]));
  if (!Array.isArray(a.slots) || !a.branches || typeof a.branches != "object")
    throw new Error("Invalid FlowForge workflow payload in BPMN XML.");
  return a;
}
export {
  u as l,
  g as s
};
//# sourceMappingURL=bpmn-CtfWDaOY.js.map
