import { p as y, Z as t, q as H, g as B, A as L, a as z, aj as G, ap as O, am as P, N as R } from "./index-CNUFa5l3.js";
import { y as U } from "./graphlib-D2YdGpuK-CM8PX5pf.js";
import { z as C } from "./dagre-DJxg_nfI-BzVw7cNP.js";
import { y as I, _ as W, v } from "./chunk-AQP2D5EJ-DEAEiJjW-DRXeKo9f.js";
var $ = /* @__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"), j = /* @__PURE__ */ y((e, i) => {
  let o = 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(i.id), s = o.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), o;
}, "drawSimpleState"), F = /* @__PURE__ */ y((e, i) => {
  let o = /* @__PURE__ */ y(function(g, u, N) {
    let b = g.append("tspan").attr("x", 2 * t().state.padding).text(u);
    N || b.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(i.descriptions[0]).node().getBBox(), d = s.height, x = e.append("text").attr("x", t().state.padding).attr("y", d + t().state.padding * 0.4 + t().state.dividerMargin + t().state.textHeight).attr("class", "state-description"), c = !0, a = !0;
  i.descriptions.forEach(function(g) {
    c || (o(x, g, a), a = !1), c = !1;
  });
  let n = e.append("line").attr("x1", t().state.padding).attr("y1", t().state.padding + d + t().state.dividerMargin / 2).attr("y2", t().state.padding + d + t().state.dividerMargin / 2).attr("class", "descr-divider"), h = x.node().getBBox(), l = Math.max(h.width, s.width);
  return n.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", h.height + d + 2 * t().state.padding).attr("rx", t().state.radius), e;
}, "drawDescrState"), J = /* @__PURE__ */ y((e, i, o) => {
  let s = t().state.padding, d = 2 * t().state.padding, x = e.node().getBBox(), c = x.width, a = x.x, n = e.append("text").attr("x", 0).attr("y", t().state.titleShift).attr("font-size", t().state.fontSize).attr("class", "state-title").text(i.id), h = n.node().getBBox().width + d, l = Math.max(h, c);
  l === c && (l += d);
  let g, u = e.node().getBBox();
  i.doc, g = a - s, h > c && (g = (c - l) / 2 + s), Math.abs(a - u.x) < s && h > c && (g = a - (h - c) / 2);
  let N = 1 - t().state.textHeight;
  return e.insert("rect", ":first-child").attr("x", g).attr("y", N).attr("class", o ? "alt-composit" : "composit").attr("width", l).attr("height", u.height + t().state.textHeight + t().state.titleShift + 1).attr("rx", "0"), n.attr("x", g + s), h <= c && n.attr("x", a + (l - d) / 2 - h / 2 + s), e.insert("rect", ":first-child").attr("x", g).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", g).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"), q = /* @__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"), Y = /* @__PURE__ */ y((e, i) => {
  let o = t().state.forkWidth, s = t().state.forkHeight;
  if (i.parentId) {
    let d = o;
    o = s, s = d;
  }
  return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", o).attr("height", s).attr("x", t().state.padding).attr("y", t().state.padding);
}, "drawForkJoinState"), Z = /* @__PURE__ */ y((e, i, o, s) => {
  let d = 0, x = s.append("text");
  x.style("text-anchor", "start"), x.attr("class", "noteText");
  let c = e.replace(/\r\n/g, "<br/>");
  c = c.replace(/\n/g, "<br/>");
  let a = c.split(z.lineBreakRegex), n = 1.25 * t().state.noteMargin;
  for (let h of a) {
    let l = h.trim();
    if (l.length > 0) {
      let g = x.append("tspan");
      if (g.text(l), n === 0) {
        let u = g.node().getBBox();
        n += u.height;
      }
      d += n, g.attr("x", i + t().state.noteMargin), g.attr("y", o + d + 1.25 * t().state.noteMargin);
    }
  }
  return {
    textWidth: x.node().getBBox().width,
    textHeight: d
  };
}, "_drawLongText"), X = /* @__PURE__ */ y((e, i) => {
  i.attr("class", "state-note");
  let o = i.append("rect").attr("x", 0).attr("y", t().state.padding), { textWidth: s, textHeight: d } = Z(e, 0, 0, i.append("g"));
  return o.attr("height", d + 2 * t().state.noteMargin), o.attr("width", s + t().state.noteMargin * 2), o;
}, "drawNote"), T = /* @__PURE__ */ y(function(e, i) {
  let o = i.id, s = {
    id: o,
    label: i.id,
    width: 0,
    height: 0
  }, d = e.append("g").attr("id", o).attr("class", "stateGroup");
  i.type === "start" && $(d), i.type === "end" && q(d), (i.type === "fork" || i.type === "join") && Y(d, i), i.type === "note" && X(i.note.text, d), i.type === "divider" && _(d), i.type === "default" && i.descriptions.length === 0 && j(d, i), i.type === "default" && i.descriptions.length > 0 && F(d, i);
  let x = d.node().getBBox();
  return s.width = x.width + 2 * t().state.padding, s.height = x.height + 2 * t().state.padding, s;
}, "drawState"), D = 0, K = /* @__PURE__ */ y(function(e, i, o) {
  let s = /* @__PURE__ */ y(function(n) {
    switch (n) {
      case v.relationType.AGGREGATION:
        return "aggregation";
      case v.relationType.EXTENSION:
        return "extension";
      case v.relationType.COMPOSITION:
        return "composition";
      case v.relationType.DEPENDENCY:
        return "dependency";
    }
  }, "getRelationType");
  i.points = i.points.filter((n) => !Number.isNaN(n.y));
  let d = i.points, x = G().x(function(n) {
    return n.x;
  }).y(function(n) {
    return n.y;
  }).curve(O), c = e.append("path").attr("d", x(d)).attr("id", "edge" + D).attr("class", "transition"), a = "";
  if (t().state.arrowMarkerAbsolute && (a = P(!0)), c.attr("marker-end", "url(" + a + "#" + s(v.relationType.DEPENDENCY) + "End)"), o.title !== void 0) {
    let n = e.append("g").attr("class", "stateLabel"), { x: h, y: l } = R.calcLabelPosition(i.points), g = z.getRows(o.title), u = 0, N = [], b = 0, E = 0;
    for (let f = 0; f <= g.length; f++) {
      let p = n.append("text").attr("text-anchor", "middle").text(g[f]).attr("x", h).attr("y", l + u), w = p.node().getBBox();
      b = Math.max(b, w.width), E = Math.min(E, w.x), B.info(w.x, h, l + u), u === 0 && (u = p.node().getBBox().height, B.info("Title height", u, l)), N.push(p);
    }
    let S = u * g.length;
    if (g.length > 1) {
      let f = (g.length - 1) * u * 0.5;
      N.forEach((p, w) => p.attr("y", l + w * u - f)), S = u * g.length;
    }
    let r = n.node().getBBox();
    n.insert("rect", ":first-child").attr("class", "box").attr("x", h - b / 2 - t().state.padding / 2).attr("y", l - S / 2 - t().state.padding / 2 - 3.5).attr("width", b + t().state.padding).attr("height", S + t().state.padding), B.info(r);
  }
  D++;
}, "drawEdge"), m, M = {}, 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, i, o, s) {
  m = t().state;
  let d = t().securityLevel, x;
  d === "sandbox" && (x = H("#i" + i));
  let c = H(d === "sandbox" ? x.nodes()[0].contentDocument.body : "body"), a = d === "sandbox" ? x.nodes()[0].contentDocument : document;
  B.debug("Rendering diagram " + e);
  let n = c.select(`[id='${i}']`);
  V(n), A(s.db.getRootDoc(), n.append("g").attr("id", i + "-root"), void 0, !1, c, a, s);
  let h = m.padding, l = n.node().getBBox(), g = l.width + h * 2, u = l.height + h * 2;
  L(n, u, g * 1.75, m.useMaxWidth), n.attr("viewBox", `${l.x - m.padding}  ${l.y - m.padding} ` + g + " " + u);
}, "draw"), et = /* @__PURE__ */ y((e) => e ? e.length * m.fontSizeFactor : 1, "getLabelWidth"), A = /* @__PURE__ */ y((e, i, o, s, d, x, c) => {
  let a = new U({
    compound: !0,
    multigraph: !0
  }), n, h = !0;
  for (n = 0; n < e.length; n++) if (e[n].stmt === "relation") {
    h = !1;
    break;
  }
  o ? a.setGraph({
    rankdir: "LR",
    multigraph: !0,
    compound: !0,
    ranker: "tight-tree",
    ranksep: h ? 1 : m.edgeLengthFactor,
    nodeSep: h ? 1 : 50,
    isMultiGraph: !0
  }) : a.setGraph({
    rankdir: "TB",
    multigraph: !0,
    compound: !0,
    ranksep: h ? 1 : m.edgeLengthFactor,
    nodeSep: h ? 1 : 50,
    ranker: "tight-tree",
    isMultiGraph: !0
  }), a.setDefaultEdgeLabel(function() {
    return {};
  });
  let l = c.db.getStates(), g = c.db.getRelations(), u = Object.keys(l);
  for (let r of u) {
    let f = l[r];
    o && (f.parentId = o);
    let p;
    if (f.doc) {
      let w = i.append("g").attr("id", f.id).attr("class", "stateGroup");
      p = A(f.doc, w, f.id, !s, d, x, c);
      {
        w = J(w, f, s);
        let k = w.node().getBBox();
        p.width = k.width, p.height = k.height + m.padding / 2, M[f.id] = { y: m.compositTitleSize };
      }
    } else p = T(i, f, a);
    if (f.note) {
      let w = T(i, {
        descriptions: [],
        id: f.id + "-note",
        note: f.note,
        type: "note"
      }, a);
      f.note.position === "left of" ? (a.setNode(p.id + "-note", w), a.setNode(p.id, p)) : (a.setNode(p.id, p), a.setNode(p.id + "-note", w)), a.setParent(p.id, p.id + "-group"), a.setParent(p.id + "-note", p.id + "-group");
    } else a.setNode(p.id, p);
  }
  B.debug("Count=", a.nodeCount(), a);
  let N = 0;
  g.forEach(function(r) {
    N++, B.debug("Setting edge", r), a.setEdge(r.id1, r.id2, {
      relation: r,
      width: et(r.title),
      height: m.labelHeight * z.getRows(r.title).length,
      labelpos: "c"
    }, "id" + N);
  }), C(a), B.debug("Graph after layout", a.nodes());
  let b = i.node();
  a.nodes().forEach(function(r) {
    r !== void 0 && a.node(r) !== void 0 ? (B.warn("Node " + r + ": " + JSON.stringify(a.node(r))), d.select("#" + b.id + " #" + r).attr("transform", "translate(" + (a.node(r).x - a.node(r).width / 2) + "," + (a.node(r).y + (M[r] ? M[r].y : 0) - a.node(r).height / 2) + " )"), d.select("#" + b.id + " #" + r).attr("data-x-shift", a.node(r).x - a.node(r).width / 2), x.querySelectorAll("#" + b.id + " #" + r + " .divider").forEach((f) => {
      let p = f.parentElement, 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);
    })) : B.debug("No Node " + r + ": " + JSON.stringify(a.node(r)));
  });
  let E = b.getBBox();
  a.edges().forEach(function(r) {
    r !== void 0 && a.edge(r) !== void 0 && (B.debug("Edge " + r.v + " -> " + r.w + ": " + JSON.stringify(a.edge(r))), K(i, a.edge(r), a.edge(r).relation));
  }), E = b.getBBox();
  let S = {
    id: o || "root",
    label: o || "root",
    width: 0,
    height: 0
  };
  return S.width = E.width + 2 * m.padding, S.height = E.height + 2 * m.padding, B.debug("Doc rendered", S, a), S;
}, "renderDoc"), nt = {
  parser: W,
  get db() {
    return new v(1);
  },
  renderer: {
    setConf: Q,
    draw: tt
  },
  styles: I,
  init: /* @__PURE__ */ y((e) => {
    e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
  }, "init")
};
export {
  nt as diagram
};
