import { styles_default as W, stateDiagram_default as G, StateDB as N } from "./chunk-DI55MBZ5.js";
import { __name as f, log as S } from "./chunk-AGHRB4JF.js";
import "../../../../../../d3-transition@3.0.1_d3-selection@3.0.0/node_modules/d3-transition/src/selection/index.js";
import "../../../../../../d3-zoom@3.0.0/node_modules/d3-zoom/src/transform.js";
import { getConfig2 as t, configureSvgSize as P, common_default as z, getUrl as U } from "./chunk-ABZYJK2D.js";
import "./chunk-N4CR4FBY.js";
import "./chunk-QXUST7PY.js";
import "./chunk-HN2XXSSU.js";
import "./chunk-JZLCHNYA.js";
import "./chunk-JA3XYJ7Z.js";
import { utils_default as _ } from "./chunk-S3R3BYOJ.js";
import { Graph as C } from "../../../../../../dagre-d3-es@7.0.13/node_modules/dagre-d3-es/src/graphlib/graph.js";
import { layout as F } from "../../../../../../dagre-d3-es@7.0.13/node_modules/dagre-d3-es/src/dagre/layout.js";
import "../../../../../../dagre-d3-es@7.0.13/node_modules/dagre-d3-es/src/dagre/rank/network-simplex.js";
import H from "../../../../../../d3-selection@3.0.0/node_modules/d3-selection/src/select.js";
import O from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/line.js";
import J from "../../../../../../d3-shape@3.2.0/node_modules/d3-shape/src/curve/basis.js";
var X = /* @__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"), D = /* @__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"), Y = /* @__PURE__ */ f((e, i) => {
  const s = 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 = s.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), s;
}, "drawSimpleState"), I = /* @__PURE__ */ f((e, i) => {
  const s = /* @__PURE__ */ f(function(g, B, w) {
    const E = g.append("tspan").attr("x", 2 * t().state.padding).text(B);
    w || E.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, x = 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, d = !0;
  i.descriptions.forEach(function(g) {
    a || (s(x, g, d), d = !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"), h = x.node().getBBox(), o = Math.max(h.width, n.width);
  return m.attr("x2", o + 3 * t().state.padding), e.insert("rect", ":first-child").attr("x", t().state.padding).attr("y", t().state.padding).attr("width", o + 2 * t().state.padding).attr("height", h.height + l + 2 * t().state.padding).attr("rx", t().state.radius), e;
}, "drawDescrState"), $ = /* @__PURE__ */ f((e, i, s) => {
  const c = t().state.padding, n = 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(i.id), h = d.node().getBBox().width + n;
  let o = Math.max(h, x);
  o === x && (o = o + n);
  let g;
  const B = e.node().getBBox();
  i.doc, g = a - c, h > x && (g = (x - o) / 2 + c), Math.abs(a - B.x) < c && h > x && (g = a - (h - x) / 2);
  const w = 1 - t().state.textHeight;
  return e.insert("rect", ":first-child").attr("x", g).attr("y", w).attr("class", s ? "alt-composit" : "composit").attr("width", o).attr(
    "height",
    B.height + t().state.textHeight + t().state.titleShift + 1
  ).attr("rx", "0"), d.attr("x", g + c), h <= x && d.attr("x", a + (o - n) / 2 - h / 2 + c), e.insert("rect", ":first-child").attr("x", g).attr(
    "y",
    t().state.titleShift - t().state.textHeight - t().state.padding
  ).attr("width", o).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", o).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 s = t().state.forkWidth, c = t().state.forkHeight;
  if (i.parentId) {
    let n = s;
    s = c, c = n;
  }
  return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", s).attr("height", c).attr("x", t().state.padding).attr("y", t().state.padding);
}, "drawForkJoinState"), j = /* @__PURE__ */ f((e, i, s, c) => {
  let n = 0;
  const l = c.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(z.lineBreakRegex);
  let d = 1.25 * t().state.noteMargin;
  for (const m of a) {
    const h = m.trim();
    if (h.length > 0) {
      const o = l.append("tspan");
      if (o.text(h), d === 0) {
        const g = o.node().getBBox();
        d += g.height;
      }
      n += d, o.attr("x", i + t().state.noteMargin), o.attr("y", s + 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 s = 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 s.attr("height", l + 2 * t().state.noteMargin), s.attr("width", n + t().state.noteMargin * 2), s;
}, "drawNote"), L = /* @__PURE__ */ f(function(e, i) {
  const s = i.id, c = {
    id: s,
    label: i.id,
    width: 0,
    height: 0
  }, n = e.append("g").attr("id", s).attr("class", "stateGroup");
  i.type === "start" && X(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" && D(n), i.type === "default" && i.descriptions.length === 0 && Y(n, i), i.type === "default" && i.descriptions.length > 0 && I(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"), A = 0, Q = /* @__PURE__ */ f(function(e, i, s) {
  const c = /* @__PURE__ */ f(function(d) {
    switch (d) {
      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((d) => !Number.isNaN(d.y));
  const n = i.points, l = O().x(function(d) {
    return d.x;
  }).y(function(d) {
    return d.y;
  }).curve(J), x = e.append("path").attr("d", l(n)).attr("id", "edge" + A).attr("class", "transition");
  let a = "";
  if (t().state.arrowMarkerAbsolute && (a = U(!0)), x.attr(
    "marker-end",
    "url(" + a + "#" + c(N.relationType.DEPENDENCY) + "End)"
  ), s.title !== void 0) {
    const d = e.append("g").attr("class", "stateLabel"), { x: m, y: h } = _.calcLabelPosition(i.points), o = z.getRows(s.title);
    let g = 0;
    const B = [];
    let w = 0, E = 0;
    for (let u = 0; u <= o.length; u++) {
      const p = d.append("text").attr("text-anchor", "middle").text(o[u]).attr("x", m).attr("y", h + g), y = p.node().getBBox();
      w = Math.max(w, y.width), E = Math.min(E, y.x), S.info(y.x, m, h + g), g === 0 && (g = p.node().getBBox().height, S.info("Title height", g, h)), B.push(p);
    }
    let k = g * o.length;
    if (o.length > 1) {
      const u = (o.length - 1) * g * 0.5;
      B.forEach((p, y) => p.attr("y", h + y * g - u)), k = g * o.length;
    }
    const r = d.node().getBBox();
    d.insert("rect", ":first-child").attr("class", "box").attr("x", m - w / 2 - t().state.padding / 2).attr("y", h - k / 2 - t().state.padding / 2 - 3.5).attr("width", w + t().state.padding).attr("height", k + t().state.padding), S.info(r);
  }
  A++;
}, "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, s, c) {
  b = t().state;
  const n = t().securityLevel;
  let l;
  n === "sandbox" && (l = H("#i" + i));
  const x = n === "sandbox" ? H(l.nodes()[0].contentDocument.body) : H("body"), a = n === "sandbox" ? l.nodes()[0].contentDocument : document;
  S.debug("Rendering diagram " + e);
  const d = x.select(`[id='${i}']`);
  tt(d);
  const m = c.db.getRootDoc();
  R(m, d, void 0, !1, x, a, c);
  const h = b.padding, o = d.node().getBBox(), g = o.width + h * 2, B = o.height + h * 2, w = g * 1.75;
  P(d, B, w, b.useMaxWidth), d.attr(
    "viewBox",
    `${o.x - b.padding}  ${o.y - b.padding} ` + g + " " + B
  );
}, "draw"), at = /* @__PURE__ */ f((e) => e ? e.length * b.fontSizeFactor : 1, "getLabelWidth"), R = /* @__PURE__ */ f((e, i, s, c, n, l, x) => {
  const a = new C({
    compound: !0,
    multigraph: !0
  });
  let d, m = !0;
  for (d = 0; d < e.length; d++)
    if (e[d].stmt === "relation") {
      m = !1;
      break;
    }
  s ? 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 h = x.db.getStates(), o = x.db.getRelations(), g = Object.keys(h);
  for (const r of g) {
    const u = h[r];
    s && (u.parentId = s);
    let p;
    if (u.doc) {
      let y = i.append("g").attr("id", u.id).attr("class", "stateGroup");
      p = R(u.doc, y, u.id, !c, n, l, x);
      {
        y = $(y, u, c);
        let v = y.node().getBBox();
        p.width = v.width, p.height = v.height + b.padding / 2, T[u.id] = { y: b.compositTitleSize };
      }
    } else
      p = L(i, u, a);
    if (u.note) {
      const y = {
        descriptions: [],
        id: u.id + "-note",
        note: u.note,
        type: "note"
      }, v = L(i, y, a);
      u.note.position === "left of" ? (a.setNode(p.id + "-note", v), a.setNode(p.id, p)) : (a.setNode(p.id, p), a.setNode(p.id + "-note", v)), a.setParent(p.id, p.id + "-group"), a.setParent(p.id + "-note", p.id + "-group");
    } else
      a.setNode(p.id, p);
  }
  S.debug("Count=", a.nodeCount(), a);
  let B = 0;
  o.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
    );
  }), F(a), S.debug("Graph after layout", a.nodes());
  const w = 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("#" + w.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("#" + w.id + " #" + r).attr("data-x-shift", a.node(r).x - a.node(r).width / 2), l.querySelectorAll("#" + w.id + " #" + r + " .divider").forEach((p) => {
      const y = p.parentElement;
      let v = 0, M = 0;
      y && (y.parentElement && (v = y.parentElement.getBBox().width), M = parseInt(y.getAttribute("data-x-shift"), 10), Number.isNaN(M) && (M = 0)), p.setAttribute("x1", 0 - M + 8), p.setAttribute("x2", v - M - 8);
    })) : S.debug("No Node " + r + ": " + JSON.stringify(a.node(r)));
  });
  let E = w.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));
  }), E = w.getBBox();
  const k = {
    id: s || "root",
    label: s || "root",
    width: 0,
    height: 0
  };
  return k.width = E.width + 2 * b.padding, k.height = E.height + 2 * b.padding, S.debug("Doc rendered", k, a), k;
}, "renderDoc"), it = {
  setConf: V,
  draw: et
}, bt = {
  parser: G,
  get db() {
    return new N(1);
  },
  renderer: it,
  styles: W,
  init: /* @__PURE__ */ f((e) => {
    e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
  }, "init")
};
export {
  bt as diagram
};
