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