import { St as _, u as M, vt as G } from "./vendor/recharts.js";
import { t as P } from "./layout.js";
import { h as R } from "./chunk-5PVQY5BW.js";
import { t as W } from "./graph.js";
import { n as y, r as B } from "./chunk-AGHRB4JF.js";
import { D as U, b as t, c as C, s as T } from "./chunk-ICPOFSXX.js";
import { i as F, n as O, t as N } from "./chunk-OYMX7WX6.js";
var J = /* @__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"), X = /* @__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"), Y = /* @__PURE__ */ y((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), o = d.node().getBBox();
  return e.insert("rect", ":first-child").attr("x", t().state.padding).attr("y", t().state.padding).attr("width", o.width + 2 * t().state.padding).attr("height", o.height + 2 * t().state.padding).attr("rx", t().state.radius), d;
}, "drawSimpleState"), D = /* @__PURE__ */ y((e, i) => {
  const d = /* @__PURE__ */ y(function(c, u, S) {
    const b = c.append("tspan").attr("x", 2 * t().state.padding).text(u);
    S || b.attr("dy", t().state.textHeight);
  }, "addTspan"), o = 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(), n = o.height, x = e.append("text").attr("x", t().state.padding).attr("y", n + t().state.padding * 0.4 + t().state.dividerMargin + t().state.textHeight).attr("class", "state-description");
  let p = !0, a = !0;
  i.descriptions.forEach(function(c) {
    p || (d(x, c, a), a = !1), p = !1;
  });
  const s = e.append("line").attr("x1", t().state.padding).attr("y1", t().state.padding + n + t().state.dividerMargin / 2).attr("y2", t().state.padding + n + t().state.dividerMargin / 2).attr("class", "descr-divider"), h = x.node().getBBox(), g = Math.max(h.width, o.width);
  return s.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", h.height + n + 2 * t().state.padding).attr("rx", t().state.radius), e;
}, "drawDescrState"), I = /* @__PURE__ */ y((e, i, d) => {
  const o = t().state.padding, n = 2 * t().state.padding, x = e.node().getBBox(), p = x.width, a = x.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), h = s.node().getBBox().width + n;
  let g = Math.max(h, p);
  g === p && (g = g + n);
  let c;
  const u = e.node().getBBox();
  i.doc, c = a - o, h > p && (c = (p - g) / 2 + o), Math.abs(a - u.x) < o && h > p && (c = a - (h - p) / 2);
  const S = 1 - t().state.textHeight;
  return e.insert("rect", ":first-child").attr("x", c).attr("y", S).attr("class", d ? "alt-composit" : "composit").attr("width", g).attr("height", u.height + t().state.textHeight + t().state.titleShift + 1).attr("rx", "0"), s.attr("x", c + o), h <= p && s.attr("x", a + (g - n) / 2 - h / 2 + o), e.insert("rect", ":first-child").attr("x", c).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", c).attr("y", t().state.titleShift - t().state.textHeight - t().state.padding).attr("width", g).attr("height", u.height + 3 + 2 * t().state.textHeight).attr("rx", t().state.radius), e;
}, "addTitleAndBox"), $ = /* @__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"), q = /* @__PURE__ */ y((e, i) => {
  let d = t().state.forkWidth, o = t().state.forkHeight;
  if (i.parentId) {
    let n = d;
    d = o, o = n;
  }
  return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", d).attr("height", o).attr("x", t().state.padding).attr("y", t().state.padding);
}, "drawForkJoinState"), Z = /* @__PURE__ */ y((e, i, d, o) => {
  let n = 0;
  const x = o.append("text");
  x.style("text-anchor", "start"), x.attr("class", "noteText");
  let p = e.replace(/\r\n/g, "<br/>");
  p = p.replace(/\n/g, "<br/>");
  const a = p.split(T.lineBreakRegex);
  let s = 1.25 * t().state.noteMargin;
  for (const h of a) {
    const g = h.trim();
    if (g.length > 0) {
      const c = x.append("tspan");
      if (c.text(g), s === 0) {
        const u = c.node().getBBox();
        s += u.height;
      }
      n += s, c.attr("x", i + t().state.noteMargin), c.attr("y", d + n + 1.25 * t().state.noteMargin);
    }
  }
  return {
    textWidth: x.node().getBBox().width,
    textHeight: n
  };
}, "_drawLongText"), j = /* @__PURE__ */ y((e, i) => {
  i.attr("class", "state-note");
  const d = i.append("rect").attr("x", 0).attr("y", t().state.padding), { textWidth: o, textHeight: n } = Z(e, 0, 0, i.append("g"));
  return d.attr("height", n + 2 * t().state.noteMargin), d.attr("width", o + t().state.noteMargin * 2), d;
}, "drawNote"), z = /* @__PURE__ */ y(function(e, i) {
  const d = i.id, o = {
    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" && $(n), (i.type === "fork" || i.type === "join") && q(n, i), i.type === "note" && j(i.note.text, n), i.type === "divider" && X(n), i.type === "default" && i.descriptions.length === 0 && Y(n, i), i.type === "default" && i.descriptions.length > 0 && D(n, i);
  const x = n.node().getBBox();
  return o.width = x.width + 2 * t().state.padding, o.height = x.height + 2 * t().state.padding, o;
}, "drawState"), L = 0, K = /* @__PURE__ */ y(function(e, i, d) {
  const o = /* @__PURE__ */ y(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, x = _().x(function(s) {
    return s.x;
  }).y(function(s) {
    return s.y;
  }).curve(G), p = e.append("path").attr("d", x(n)).attr("id", "edge" + L).attr("class", "transition");
  let a = "";
  if (t().state.arrowMarkerAbsolute && (a = U(!0)), p.attr("marker-end", "url(" + a + "#" + o(N.relationType.DEPENDENCY) + "End)"), d.title !== void 0) {
    const s = e.append("g").attr("class", "stateLabel"), { x: h, y: g } = R.calcLabelPosition(i.points), c = T.getRows(d.title);
    let u = 0;
    const S = [];
    let b = 0, v = 0;
    for (let f = 0; f <= c.length; f++) {
      const l = s.append("text").attr("text-anchor", "middle").text(c[f]).attr("x", h).attr("y", g + u), w = l.node().getBBox();
      b = Math.max(b, w.width), v = Math.min(v, w.x), B.info(w.x, h, g + u), u === 0 && (u = l.node().getBBox().height, B.info("Title height", u, g)), S.push(l);
    }
    let E = u * c.length;
    if (c.length > 1) {
      const f = (c.length - 1) * u * 0.5;
      S.forEach((l, w) => l.attr("y", g + w * u - f)), E = u * c.length;
    }
    const r = s.node().getBBox();
    s.insert("rect", ":first-child").attr("class", "box").attr("x", h - b / 2 - t().state.padding / 2).attr("y", g - E / 2 - t().state.padding / 2 - 3.5).attr("width", b + t().state.padding).attr("height", E + t().state.padding), B.info(r);
  }
  L++;
}, "drawEdge"), m, H = {}, 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, d, o) {
  m = t().state;
  const n = t().securityLevel;
  let x;
  n === "sandbox" && (x = M("#i" + i));
  const p = n === "sandbox" ? M(x.nodes()[0].contentDocument.body) : M("body"), a = n === "sandbox" ? x.nodes()[0].contentDocument : document;
  B.debug("Rendering diagram " + e);
  const s = p.select(`[id='${i}']`);
  V(s), A(o.db.getRootDoc(), s.append("g").attr("id", i + "-root"), void 0, !1, p, a, o);
  const h = m.padding, g = s.node().getBBox(), c = g.width + h * 2, u = g.height + h * 2;
  C(s, u, c * 1.75, m.useMaxWidth), s.attr("viewBox", `${g.x - m.padding}  ${g.y - m.padding} ` + c + " " + u);
}, "draw"), et = /* @__PURE__ */ y((e) => e ? e.length * m.fontSizeFactor : 1, "getLabelWidth"), A = /* @__PURE__ */ y((e, i, d, o, n, x, p) => {
  const a = new W({
    compound: !0,
    multigraph: !0
  });
  let s, h = !0;
  for (s = 0; s < e.length; s++) if (e[s].stmt === "relation") {
    h = !1;
    break;
  }
  d ? 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 {};
  });
  const g = p.db.getStates(), c = p.db.getRelations(), u = Object.keys(g);
  for (const r of u) {
    const f = g[r];
    d && (f.parentId = d);
    let l;
    if (f.doc) {
      let w = i.append("g").attr("id", f.id).attr("class", "stateGroup");
      l = A(f.doc, w, f.id, !o, n, x, p);
      {
        w = I(w, f, o);
        let k = w.node().getBBox();
        l.width = k.width, l.height = k.height + m.padding / 2, H[f.id] = { y: m.compositTitleSize };
      }
    } else l = z(i, f, a);
    if (f.note) {
      const w = z(i, {
        descriptions: [],
        id: f.id + "-note",
        note: f.note,
        type: "note"
      }, a);
      f.note.position === "left of" ? (a.setNode(l.id + "-note", w), a.setNode(l.id, l)) : (a.setNode(l.id, l), a.setNode(l.id + "-note", w)), a.setParent(l.id, l.id + "-group"), a.setParent(l.id + "-note", l.id + "-group");
    } else a.setNode(l.id, l);
  }
  B.debug("Count=", a.nodeCount(), a);
  let S = 0;
  c.forEach(function(r) {
    S++, B.debug("Setting edge", r), a.setEdge(r.id1, r.id2, {
      relation: r,
      width: et(r.title),
      height: m.labelHeight * T.getRows(r.title).length,
      labelpos: "c"
    }, "id" + S);
  }), P(a), B.debug("Graph after layout", a.nodes());
  const 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))), n.select("#" + b.id + " #" + r).attr("transform", "translate(" + (a.node(r).x - a.node(r).width / 2) + "," + (a.node(r).y + (H[r] ? H[r].y : 0) - a.node(r).height / 2) + " )"), n.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) => {
      const l = f.parentElement;
      let w = 0, k = 0;
      l && (l.parentElement && (w = l.parentElement.getBBox().width), k = parseInt(l.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 v = 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));
  }), v = b.getBBox();
  const E = {
    id: d || "root",
    label: d || "root",
    width: 0,
    height: 0
  };
  return E.width = v.width + 2 * m.padding, E.height = v.height + 2 * m.padding, B.debug("Doc rendered", E, a), E;
}, "renderDoc"), gt = {
  parser: O,
  get db() {
    return new N(1);
  },
  renderer: {
    setConf: Q,
    draw: tt
  },
  styles: F,
  init: /* @__PURE__ */ y((e) => {
    e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
  }, "init")
};
export {
  gt as diagram
};

//# sourceMappingURL=stateDiagram-FHFEXIEX.js.map