import { I as G, R as N, A as I } from "./chunk-7U56Z5CX-awhAxjZ0-Cs_UWlwk.js";
import { _ as u, g as t, a9 as M, l as S, aa as O, ag as H, ay as P, az as U, ao as C } from "./index-CMZig9xg.js";
import { L as _ } from "./graph-BYZMvqpu-BXRs5843.js";
import { o as $ } from "./layout-BHI4imdk-CESYC-il.js";
var T = {}, W = /* @__PURE__ */ u((e, n) => {
  T[e] = n;
}, "set"), j = /* @__PURE__ */ u((e) => T[e], "get"), D = /* @__PURE__ */ u(() => Object.keys(T), "keys"), F = /* @__PURE__ */ u(() => D().length, "size"), J = {
  get: j,
  set: W,
  keys: D,
  size: F
}, Y = /* @__PURE__ */ u((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"), q = /* @__PURE__ */ u((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"), X = /* @__PURE__ */ u((e, n) => {
  const 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(n.id), r = o.node().getBBox();
  return e.insert("rect", ":first-child").attr("x", t().state.padding).attr("y", t().state.padding).attr("width", r.width + 2 * t().state.padding).attr("height", r.height + 2 * t().state.padding).attr("rx", t().state.radius), o;
}, "drawSimpleState"), Z = /* @__PURE__ */ u((e, n) => {
  const o = /* @__PURE__ */ u(function(g, f, B) {
    const m = g.append("tspan").attr("x", 2 * t().state.padding).text(f);
    B || m.attr("dy", t().state.textHeight);
  }, "addTspan"), r = 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(), d = r.height, l = 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");
  let h = !0, a = !0;
  n.descriptions.forEach(function(g) {
    h || (o(l, g, a), a = !1), h = !1;
  });
  const s = 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"), x = l.node().getBBox(), p = Math.max(x.width, r.width);
  return s.attr("x2", p + 3 * t().state.padding), e.insert("rect", ":first-child").attr("x", t().state.padding).attr("y", t().state.padding).attr("width", p + 2 * t().state.padding).attr("height", x.height + d + 2 * t().state.padding).attr("rx", t().state.radius), e;
}, "drawDescrState"), K = /* @__PURE__ */ u((e, n, o) => {
  const r = t().state.padding, d = 2 * t().state.padding, l = e.node().getBBox(), h = 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(n.id), x = s.node().getBBox().width + d;
  let p = Math.max(x, h);
  p === h && (p = p + d);
  let g;
  const f = e.node().getBBox();
  n.doc, g = a - r, x > h && (g = (h - p) / 2 + r), Math.abs(a - f.x) < r && x > h && (g = a - (x - h) / 2);
  const B = 1 - t().state.textHeight;
  return e.insert("rect", ":first-child").attr("x", g).attr("y", B).attr("class", o ? "alt-composit" : "composit").attr("width", p).attr(
    "height",
    f.height + t().state.textHeight + t().state.titleShift + 1
  ).attr("rx", "0"), s.attr("x", g + r), x <= h && s.attr("x", a + (p - d) / 2 - x / 2 + r), e.insert("rect", ":first-child").attr("x", g).attr(
    "y",
    t().state.titleShift - t().state.textHeight - t().state.padding
  ).attr("width", p).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", p).attr("height", f.height + 3 + 2 * t().state.textHeight).attr("rx", t().state.radius), e;
}, "addTitleAndBox"), Q = /* @__PURE__ */ u((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"), V = /* @__PURE__ */ u((e, n) => {
  let o = t().state.forkWidth, r = t().state.forkHeight;
  if (n.parentId) {
    let d = o;
    o = r, r = d;
  }
  return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", o).attr("height", r).attr("x", t().state.padding).attr("y", t().state.padding);
}, "drawForkJoinState"), tt = /* @__PURE__ */ u((e, n, o, r) => {
  let d = 0;
  const l = r.append("text");
  l.style("text-anchor", "start"), l.attr("class", "noteText");
  let h = e.replace(/\r\n/g, "<br/>");
  h = h.replace(/\n/g, "<br/>");
  const a = h.split(H.lineBreakRegex);
  let s = 1.25 * t().state.noteMargin;
  for (const x of a) {
    const p = x.trim();
    if (p.length > 0) {
      const g = l.append("tspan");
      if (g.text(p), s === 0) {
        const f = g.node().getBBox();
        s += f.height;
      }
      d += s, g.attr("x", n + t().state.noteMargin), g.attr("y", o + d + 1.25 * t().state.noteMargin);
    }
  }
  return { textWidth: l.node().getBBox().width, textHeight: d };
}, "_drawLongText"), et = /* @__PURE__ */ u((e, n) => {
  n.attr("class", "state-note");
  const o = n.append("rect").attr("x", 0).attr("y", t().state.padding), r = n.append("g"), { textWidth: d, textHeight: l } = tt(e, 0, 0, r);
  return o.attr("height", l + 2 * t().state.noteMargin), o.attr("width", d + t().state.noteMargin * 2), o;
}, "drawNote"), L = /* @__PURE__ */ u(function(e, n) {
  const o = n.id, r = {
    id: o,
    label: n.id,
    width: 0,
    height: 0
  }, d = e.append("g").attr("id", o).attr("class", "stateGroup");
  n.type === "start" && Y(d), n.type === "end" && Q(d), (n.type === "fork" || n.type === "join") && V(d, n), n.type === "note" && et(n.note.text, d), n.type === "divider" && q(d), n.type === "default" && n.descriptions.length === 0 && X(d, n), n.type === "default" && n.descriptions.length > 0 && Z(d, n);
  const l = d.node().getBBox();
  return r.width = l.width + 2 * t().state.padding, r.height = l.height + 2 * t().state.padding, J.set(o, r), r;
}, "drawState"), A = 0, at = /* @__PURE__ */ u(function(e, n, o) {
  const r = /* @__PURE__ */ u(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");
  n.points = n.points.filter((s) => !Number.isNaN(s.y));
  const d = n.points, l = P().x(function(s) {
    return s.x;
  }).y(function(s) {
    return s.y;
  }).curve(U), h = e.append("path").attr("d", l(d)).attr("id", "edge" + A).attr("class", "transition");
  let a = "";
  if (t().state.arrowMarkerAbsolute && (a = window.location.protocol + "//" + window.location.host + window.location.pathname + window.location.search, a = a.replace(/\(/g, "\\("), a = a.replace(/\)/g, "\\)")), h.attr(
    "marker-end",
    "url(" + a + "#" + r(N.relationType.DEPENDENCY) + "End)"
  ), o.title !== void 0) {
    const s = e.append("g").attr("class", "stateLabel"), { x, y: p } = C.calcLabelPosition(n.points), g = H.getRows(o.title);
    let f = 0;
    const B = [];
    let m = 0, v = 0;
    for (let y = 0; y <= g.length; y++) {
      const c = s.append("text").attr("text-anchor", "middle").text(g[y]).attr("x", x).attr("y", p + f), w = c.node().getBBox();
      m = Math.max(m, w.width), v = Math.min(v, w.x), S.info(w.x, x, p + f), f === 0 && (f = c.node().getBBox().height, S.info("Title height", f, p)), B.push(c);
    }
    let E = f * g.length;
    if (g.length > 1) {
      const y = (g.length - 1) * f * 0.5;
      B.forEach((c, w) => c.attr("y", p + w * f - y)), E = f * g.length;
    }
    const i = s.node().getBBox();
    s.insert("rect", ":first-child").attr("class", "box").attr("x", x - m / 2 - t().state.padding / 2).attr("y", p - E / 2 - t().state.padding / 2 - 3.5).attr("width", m + t().state.padding).attr("height", E + t().state.padding), S.info(i);
  }
  A++;
}, "drawEdge"), b, z = {}, nt = /* @__PURE__ */ u(function() {
}, "setConf"), it = /* @__PURE__ */ u(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"), dt = /* @__PURE__ */ u(function(e, n, o, r) {
  b = t().state;
  const d = t().securityLevel;
  let l;
  d === "sandbox" && (l = M("#i" + n));
  const h = d === "sandbox" ? M(l.nodes()[0].contentDocument.body) : M("body"), a = d === "sandbox" ? l.nodes()[0].contentDocument : document;
  S.debug("Rendering diagram " + e);
  const s = h.select(`[id='${n}']`);
  it(s);
  const x = r.db.getRootDoc();
  R(x, s, void 0, !1, h, a, r);
  const p = b.padding, g = s.node().getBBox(), f = g.width + p * 2, B = g.height + p * 2, m = f * 1.75;
  O(s, B, m, b.useMaxWidth), s.attr(
    "viewBox",
    `${g.x - b.padding}  ${g.y - b.padding} ` + f + " " + B
  );
}, "draw"), rt = /* @__PURE__ */ u((e) => e ? e.length * b.fontSizeFactor : 1, "getLabelWidth"), R = /* @__PURE__ */ u((e, n, o, r, d, l, h) => {
  const a = new _({
    compound: !0,
    multigraph: !0
  });
  let s, x = !0;
  for (s = 0; s < e.length; s++)
    if (e[s].stmt === "relation") {
      x = !1;
      break;
    }
  o ? 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 {};
  }), h.db.extract(e);
  const p = h.db.getStates(), g = h.db.getRelations(), f = Object.keys(p);
  for (const i of f) {
    const y = p[i];
    o && (y.parentId = o);
    let c;
    if (y.doc) {
      let w = n.append("g").attr("id", y.id).attr("class", "stateGroup");
      c = R(y.doc, w, y.id, !r, d, l, h);
      {
        w = K(w, y, r);
        let k = w.node().getBBox();
        c.width = k.width, c.height = k.height + b.padding / 2, z[y.id] = { y: b.compositTitleSize };
      }
    } else
      c = L(n, y, a);
    if (y.note) {
      const w = {
        descriptions: [],
        id: y.id + "-note",
        note: y.note,
        type: "note"
      }, k = L(n, w, a);
      y.note.position === "left of" ? (a.setNode(c.id + "-note", k), a.setNode(c.id, c)) : (a.setNode(c.id, c), a.setNode(c.id + "-note", k)), a.setParent(c.id, c.id + "-group"), a.setParent(c.id + "-note", c.id + "-group");
    } else
      a.setNode(c.id, c);
  }
  S.debug("Count=", a.nodeCount(), a);
  let B = 0;
  g.forEach(function(i) {
    B++, S.debug("Setting edge", i), a.setEdge(
      i.id1,
      i.id2,
      {
        relation: i,
        width: rt(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))), d.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) + " )"
    ), d.select("#" + m.id + " #" + i).attr("data-x-shift", a.node(i).x - a.node(i).width / 2), l.querySelectorAll("#" + m.id + " #" + i + " .divider").forEach((y) => {
      const c = y.parentElement;
      let w = 0, k = 0;
      c && (c.parentElement && (w = c.parentElement.getBBox().width), k = parseInt(c.getAttribute("data-x-shift"), 10), Number.isNaN(k) && (k = 0)), y.setAttribute("x1", 0 - k + 8), y.setAttribute("x2", w - k - 8);
    })) : S.debug("No Node " + i + ": " + JSON.stringify(a.node(i)));
  });
  let v = 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))), at(n, a.edge(i), a.edge(i).relation));
  }), v = m.getBBox();
  const E = {
    id: o || "root",
    label: o || "root",
    width: 0,
    height: 0
  };
  return E.width = v.width + 2 * b.padding, E.height = v.height + 2 * b.padding, S.debug("Doc rendered", E, a), E;
}, "renderDoc"), st = {
  setConf: nt,
  draw: dt
}, lt = {
  parser: I,
  db: N,
  renderer: st,
  styles: G,
  init: /* @__PURE__ */ u((e) => {
    e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute, N.clear();
  }, "init")
};
export {
  lt as diagram
};
