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