import { U, B as A, M } from "./chunk-LXBSTHXV-DDrUAnPm-BhQORC-M.js";
import { _ as y, g as t, a9 as v, l as S, aa as G, ag as H, bd as R, bk as O, bh as P, am as C } from "./index-DeBlJN4f.js";
import { L as I } from "./graph-DM4yJZhA-Bbo6I0vd.js";
import { o as $ } from "./layout-CDChcvw3-C4cdx174.js";
var W = /* @__PURE__ */ y((e) => e.append("circle").attr("class", "start-state").attr("r", t().state.sizeUnit).attr("cx", t().state.padding + t().state.sizeUnit).attr("cy", t().state.padding + t().state.sizeUnit), "drawStartState"), _ = /* @__PURE__ */ y((e) => e.append("line").style("stroke", "grey").style("stroke-dasharray", "3").attr("x1", t().state.textHeight).attr("class", "divider").attr("x2", t().state.textHeight * 2).attr("y1", 0).attr("y2", 0), "drawDivider"), F = /* @__PURE__ */ y((e, n) => {
  const g = e.append("text").attr("x", 2 * t().state.padding).attr("y", t().state.textHeight + 2 * t().state.padding).attr("font-size", t().state.fontSize).attr("class", "state-title").text(n.id), s = g.node().getBBox();
  return e.insert("rect", ":first-child").attr("x", t().state.padding).attr("y", t().state.padding).attr("width", s.width + 2 * t().state.padding).attr("height", s.height + 2 * t().state.padding).attr("rx", t().state.radius), g;
}, "drawSimpleState"), J = /* @__PURE__ */ y((e, n) => {
  const g = /* @__PURE__ */ y(function(o, u, b) {
    const m = o.append("tspan").attr("x", 2 * t().state.padding).text(u);
    b || m.attr("dy", t().state.textHeight);
  }, "addTspan"), s = e.append("text").attr("x", 2 * t().state.padding).attr("y", t().state.textHeight + 1.3 * t().state.padding).attr("font-size", t().state.fontSize).attr("class", "state-title").text(n.descriptions[0]).node().getBBox(), r = s.height, c = e.append("text").attr("x", t().state.padding).attr(
    "y",
    r + t().state.padding * 0.4 + t().state.dividerMargin + t().state.textHeight
  ).attr("class", "state-description");
  let h = !0, a = !0;
  n.descriptions.forEach(function(o) {
    h || (g(c, o, a), a = !1), h = !1;
  });
  const d = e.append("line").attr("x1", t().state.padding).attr("y1", t().state.padding + r + t().state.dividerMargin / 2).attr("y2", t().state.padding + r + t().state.dividerMargin / 2).attr("class", "descr-divider"), x = c.node().getBBox(), l = Math.max(x.width, s.width);
  return d.attr("x2", l + 3 * t().state.padding), e.insert("rect", ":first-child").attr("x", t().state.padding).attr("y", t().state.padding).attr("width", l + 2 * t().state.padding).attr("height", x.height + r + 2 * t().state.padding).attr("rx", t().state.radius), e;
}, "drawDescrState"), Y = /* @__PURE__ */ y((e, n, g) => {
  const s = t().state.padding, r = 2 * t().state.padding, c = e.node().getBBox(), h = c.width, a = c.x, d = e.append("text").attr("x", 0).attr("y", t().state.titleShift).attr("font-size", t().state.fontSize).attr("class", "state-title").text(n.id), x = d.node().getBBox().width + r;
  let l = Math.max(x, h);
  l === h && (l = l + r);
  let o;
  const u = e.node().getBBox();
  n.doc, o = a - s, x > h && (o = (h - l) / 2 + s), Math.abs(a - u.x) < s && x > h && (o = a - (x - h) / 2);
  const b = 1 - t().state.textHeight;
  return e.insert("rect", ":first-child").attr("x", o).attr("y", b).attr("class", g ? "alt-composit" : "composit").attr("width", l).attr(
    "height",
    u.height + t().state.textHeight + t().state.titleShift + 1
  ).attr("rx", "0"), d.attr("x", o + s), x <= h && d.attr("x", a + (l - r) / 2 - x / 2 + s), e.insert("rect", ":first-child").attr("x", o).attr(
    "y",
    t().state.titleShift - t().state.textHeight - t().state.padding
  ).attr("width", l).attr("height", t().state.textHeight * 3).attr("rx", t().state.radius), e.insert("rect", ":first-child").attr("x", o).attr(
    "y",
    t().state.titleShift - t().state.textHeight - t().state.padding
  ).attr("width", l).attr("height", u.height + 3 + 2 * t().state.textHeight).attr("rx", t().state.radius), e;
}, "addTitleAndBox"), j = /* @__PURE__ */ y((e) => (e.append("circle").attr("class", "end-state-outer").attr("r", t().state.sizeUnit + t().state.miniPadding).attr(
  "cx",
  t().state.padding + t().state.sizeUnit + t().state.miniPadding
).attr(
  "cy",
  t().state.padding + t().state.sizeUnit + t().state.miniPadding
), e.append("circle").attr("class", "end-state-inner").attr("r", t().state.sizeUnit).attr("cx", t().state.padding + t().state.sizeUnit + 2).attr("cy", t().state.padding + t().state.sizeUnit + 2)), "drawEndState"), X = /* @__PURE__ */ y((e, n) => {
  let g = t().state.forkWidth, s = t().state.forkHeight;
  if (n.parentId) {
    let r = g;
    g = s, s = r;
  }
  return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", g).attr("height", s).attr("x", t().state.padding).attr("y", t().state.padding);
}, "drawForkJoinState"), q = /* @__PURE__ */ y((e, n, g, s) => {
  let r = 0;
  const c = s.append("text");
  c.style("text-anchor", "start"), c.attr("class", "noteText");
  let h = e.replace(/\r\n/g, "<br/>");
  h = h.replace(/\n/g, "<br/>");
  const a = h.split(H.lineBreakRegex);
  let d = 1.25 * t().state.noteMargin;
  for (const x of a) {
    const l = x.trim();
    if (l.length > 0) {
      const o = c.append("tspan");
      if (o.text(l), d === 0) {
        const u = o.node().getBBox();
        d += u.height;
      }
      r += d, o.attr("x", n + t().state.noteMargin), o.attr("y", g + r + 1.25 * t().state.noteMargin);
    }
  }
  return { textWidth: c.node().getBBox().width, textHeight: r };
}, "_drawLongText"), Z = /* @__PURE__ */ y((e, n) => {
  n.attr("class", "state-note");
  const g = n.append("rect").attr("x", 0).attr("y", t().state.padding), s = n.append("g"), { textWidth: r, textHeight: c } = q(e, 0, 0, s);
  return g.attr("height", c + 2 * t().state.noteMargin), g.attr("width", r + t().state.noteMargin * 2), g;
}, "drawNote"), T = /* @__PURE__ */ y(function(e, n) {
  const g = n.id, s = {
    id: g,
    label: n.id,
    width: 0,
    height: 0
  }, r = e.append("g").attr("id", g).attr("class", "stateGroup");
  n.type === "start" && W(r), n.type === "end" && j(r), (n.type === "fork" || n.type === "join") && X(r, n), n.type === "note" && Z(n.note.text, r), n.type === "divider" && _(r), n.type === "default" && n.descriptions.length === 0 && F(r, n), n.type === "default" && n.descriptions.length > 0 && J(r, n);
  const c = r.node().getBBox();
  return s.width = c.width + 2 * t().state.padding, s.height = c.height + 2 * t().state.padding, s;
}, "drawState"), L = 0, K = /* @__PURE__ */ y(function(e, n, g) {
  const s = /* @__PURE__ */ y(function(d) {
    switch (d) {
      case M.relationType.AGGREGATION:
        return "aggregation";
      case M.relationType.EXTENSION:
        return "extension";
      case M.relationType.COMPOSITION:
        return "composition";
      case M.relationType.DEPENDENCY:
        return "dependency";
    }
  }, "getRelationType");
  n.points = n.points.filter((d) => !Number.isNaN(d.y));
  const r = n.points, c = R().x(function(d) {
    return d.x;
  }).y(function(d) {
    return d.y;
  }).curve(O), h = e.append("path").attr("d", c(r)).attr("id", "edge" + L).attr("class", "transition");
  let a = "";
  if (t().state.arrowMarkerAbsolute && (a = P(!0)), h.attr(
    "marker-end",
    "url(" + a + "#" + s(M.relationType.DEPENDENCY) + "End)"
  ), g.title !== void 0) {
    const d = e.append("g").attr("class", "stateLabel"), { x, y: l } = C.calcLabelPosition(n.points), o = H.getRows(g.title);
    let u = 0;
    const b = [];
    let m = 0, N = 0;
    for (let f = 0; f <= o.length; f++) {
      const p = d.append("text").attr("text-anchor", "middle").text(o[f]).attr("x", x).attr("y", l + u), w = p.node().getBBox();
      m = Math.max(m, w.width), N = Math.min(N, w.x), S.info(w.x, x, l + u), u === 0 && (u = p.node().getBBox().height, S.info("Title height", u, l)), b.push(p);
    }
    let E = u * o.length;
    if (o.length > 1) {
      const f = (o.length - 1) * u * 0.5;
      b.forEach((p, w) => p.attr("y", l + w * u - f)), E = u * o.length;
    }
    const i = d.node().getBBox();
    d.insert("rect", ":first-child").attr("class", "box").attr("x", x - m / 2 - t().state.padding / 2).attr("y", l - E / 2 - t().state.padding / 2 - 3.5).attr("width", m + t().state.padding).attr("height", E + t().state.padding), S.info(i);
  }
  L++;
}, "drawEdge"), B, z = {}, Q = /* @__PURE__ */ y(function() {
}, "setConf"), V = /* @__PURE__ */ y(function(e) {
  e.append("defs").append("marker").attr("id", "dependencyEnd").attr("refX", 19).attr("refY", 7).attr("markerWidth", 20).attr("markerHeight", 28).attr("orient", "auto").append("path").attr("d", "M 19,7 L9,13 L14,7 L9,1 Z");
}, "insertMarkers"), tt = /* @__PURE__ */ y(function(e, n, g, s) {
  B = t().state;
  const r = t().securityLevel;
  let c;
  r === "sandbox" && (c = v("#i" + n));
  const h = r === "sandbox" ? v(c.nodes()[0].contentDocument.body) : v("body"), a = r === "sandbox" ? c.nodes()[0].contentDocument : document;
  S.debug("Rendering diagram " + e);
  const d = h.select(`[id='${n}']`);
  V(d);
  const x = s.db.getRootDoc();
  D(x, d, void 0, !1, h, a, s);
  const l = B.padding, o = d.node().getBBox(), u = o.width + l * 2, b = o.height + l * 2, m = u * 1.75;
  G(d, b, m, B.useMaxWidth), d.attr(
    "viewBox",
    `${o.x - B.padding}  ${o.y - B.padding} ` + u + " " + b
  );
}, "draw"), et = /* @__PURE__ */ y((e) => e ? e.length * B.fontSizeFactor : 1, "getLabelWidth"), D = /* @__PURE__ */ y((e, n, g, s, r, c, h) => {
  const a = new I({
    compound: !0,
    multigraph: !0
  });
  let d, x = !0;
  for (d = 0; d < e.length; d++)
    if (e[d].stmt === "relation") {
      x = !1;
      break;
    }
  g ? a.setGraph({
    rankdir: "LR",
    multigraph: !0,
    compound: !0,
    // acyclicer: 'greedy',
    ranker: "tight-tree",
    ranksep: x ? 1 : B.edgeLengthFactor,
    nodeSep: x ? 1 : 50,
    isMultiGraph: !0
    // ranksep: 5,
    // nodesep: 1
  }) : a.setGraph({
    rankdir: "TB",
    multigraph: !0,
    compound: !0,
    // isCompound: true,
    // acyclicer: 'greedy',
    // ranker: 'longest-path'
    ranksep: x ? 1 : B.edgeLengthFactor,
    nodeSep: x ? 1 : 50,
    ranker: "tight-tree",
    // ranker: 'network-simplex'
    isMultiGraph: !0
  }), a.setDefaultEdgeLabel(function() {
    return {};
  });
  const l = h.db.getStates(), o = h.db.getRelations(), u = Object.keys(l);
  for (const i of u) {
    const f = l[i];
    g && (f.parentId = g);
    let p;
    if (f.doc) {
      let w = n.append("g").attr("id", f.id).attr("class", "stateGroup");
      p = D(f.doc, w, f.id, !s, r, c, h);
      {
        w = Y(w, f, s);
        let k = w.node().getBBox();
        p.width = k.width, p.height = k.height + B.padding / 2, z[f.id] = { y: B.compositTitleSize };
      }
    } else
      p = T(n, f, a);
    if (f.note) {
      const w = {
        descriptions: [],
        id: f.id + "-note",
        note: f.note,
        type: "note"
      }, k = T(n, w, a);
      f.note.position === "left of" ? (a.setNode(p.id + "-note", k), a.setNode(p.id, p)) : (a.setNode(p.id, p), a.setNode(p.id + "-note", k)), a.setParent(p.id, p.id + "-group"), a.setParent(p.id + "-note", p.id + "-group");
    } else
      a.setNode(p.id, p);
  }
  S.debug("Count=", a.nodeCount(), a);
  let b = 0;
  o.forEach(function(i) {
    b++, S.debug("Setting edge", i), a.setEdge(
      i.id1,
      i.id2,
      {
        relation: i,
        width: et(i.title),
        height: B.labelHeight * H.getRows(i.title).length,
        labelpos: "c"
      },
      "id" + b
    );
  }), $(a), S.debug("Graph after layout", a.nodes());
  const m = n.node();
  a.nodes().forEach(function(i) {
    i !== void 0 && a.node(i) !== void 0 ? (S.warn("Node " + i + ": " + JSON.stringify(a.node(i))), r.select("#" + m.id + " #" + i).attr(
      "transform",
      "translate(" + (a.node(i).x - a.node(i).width / 2) + "," + (a.node(i).y + (z[i] ? z[i].y : 0) - a.node(i).height / 2) + " )"
    ), r.select("#" + m.id + " #" + i).attr("data-x-shift", a.node(i).x - a.node(i).width / 2), c.querySelectorAll("#" + m.id + " #" + i + " .divider").forEach((f) => {
      const p = f.parentElement;
      let w = 0, k = 0;
      p && (p.parentElement && (w = p.parentElement.getBBox().width), k = parseInt(p.getAttribute("data-x-shift"), 10), Number.isNaN(k) && (k = 0)), f.setAttribute("x1", 0 - k + 8), f.setAttribute("x2", w - k - 8);
    })) : S.debug("No Node " + i + ": " + JSON.stringify(a.node(i)));
  });
  let N = m.getBBox();
  a.edges().forEach(function(i) {
    i !== void 0 && a.edge(i) !== void 0 && (S.debug("Edge " + i.v + " -> " + i.w + ": " + JSON.stringify(a.edge(i))), K(n, a.edge(i), a.edge(i).relation));
  }), N = m.getBBox();
  const E = {
    id: g || "root",
    label: g || "root",
    width: 0,
    height: 0
  };
  return E.width = N.width + 2 * B.padding, E.height = N.height + 2 * B.padding, S.debug("Doc rendered", E, a), E;
}, "renderDoc"), at = {
  setConf: Q,
  draw: tt
}, st = {
  parser: A,
  get db() {
    return new M(1);
  },
  renderer: at,
  styles: U,
  init: /* @__PURE__ */ y((e) => {
    e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
  }, "init")
};
export {
  st as diagram
};
