import { t as layout } from "./dagre-VYEPqXIV.js";
import { t as Graph } from "./graphlib-CVPKjKCS.js";
import "./purify.es-DT70lfR0.js";
import { _ as basis_default } from "./step-CWipAYTY.js";
import { t as line_default } from "./line-C5s_12ee.js";
import { g as utils_default } from "./chunk-S3R3BYOJ-mQeCz5CE.js";
import { i as log, r as __name, t as select_default } from "./src-Bf2iLOlr.js";
import { E as getUrl, b as getConfig2, c as configureSvgSize, s as common_default } from "./chunk-ABZYJK2D-D0cLy8Bb.js";
import "./chunk-HN2XXSSU-E3n-Ys7Z.js";
import "./chunk-CVBHYZKI-B_c5YBcW.js";
import "./chunk-ATLVNIR6-BXsEjlHF.js";
import "./dist-BGdYVvOu.js";
import "./chunk-JA3XYJ7Z-D6c6cOBG.js";
import "./chunk-JZLCHNYA-BvsPHJmL.js";
import "./chunk-QXUST7PY-DV8yRwBd.js";
import "./chunk-N4CR4FBY-8ycT-O9a.js";
import "./chunk-55IACEB6-B-1mjMMC.js";
import "./chunk-QN33PNHL-Bb-eUBW3.js";
import { i as styles_default, n as stateDiagram_default, t as StateDB } from "./chunk-DI55MBZ5-D1qLYNrb.js";
var drawStartState = /* @__PURE__ */ __name((e) => e.append("circle").attr("class", "start-state").attr("r", getConfig2().state.sizeUnit).attr("cx", getConfig2().state.padding + getConfig2().state.sizeUnit).attr("cy", getConfig2().state.padding + getConfig2().state.sizeUnit), "drawStartState"), drawDivider = /* @__PURE__ */ __name((e) => e.append("line").style("stroke", "grey").style("stroke-dasharray", "3").attr("x1", getConfig2().state.textHeight).attr("class", "divider").attr("x2", getConfig2().state.textHeight * 2).attr("y1", 0).attr("y2", 0), "drawDivider"), drawSimpleState = /* @__PURE__ */ __name((e, h) => {
  let g = e.append("text").attr("x", 2 * getConfig2().state.padding).attr("y", getConfig2().state.textHeight + 2 * getConfig2().state.padding).attr("font-size", getConfig2().state.fontSize).attr("class", "state-title").text(h.id), _ = g.node().getBBox();
  return e.insert("rect", ":first-child").attr("x", getConfig2().state.padding).attr("y", getConfig2().state.padding).attr("width", _.width + 2 * getConfig2().state.padding).attr("height", _.height + 2 * getConfig2().state.padding).attr("rx", getConfig2().state.radius), g;
}, "drawSimpleState"), drawDescrState = /* @__PURE__ */ __name((e, h) => {
  let g = /* @__PURE__ */ __name(function(e2, h2, g2) {
    let _2 = e2.append("tspan").attr("x", 2 * getConfig2().state.padding).text(h2);
    g2 || _2.attr("dy", getConfig2().state.textHeight);
  }, "addTspan"), _ = e.append("text").attr("x", 2 * getConfig2().state.padding).attr("y", getConfig2().state.textHeight + 1.3 * getConfig2().state.padding).attr("font-size", getConfig2().state.fontSize).attr("class", "state-title").text(h.descriptions[0]).node().getBBox(), v = _.height, y = e.append("text").attr("x", getConfig2().state.padding).attr("y", v + getConfig2().state.padding * 0.4 + getConfig2().state.dividerMargin + getConfig2().state.textHeight).attr("class", "state-description"), x = true, S = true;
  h.descriptions.forEach(function(e2) {
    x || (g(y, e2, S), S = false), x = false;
  });
  let w = e.append("line").attr("x1", getConfig2().state.padding).attr("y1", getConfig2().state.padding + v + getConfig2().state.dividerMargin / 2).attr("y2", getConfig2().state.padding + v + getConfig2().state.dividerMargin / 2).attr("class", "descr-divider"), T = y.node().getBBox(), E = Math.max(T.width, _.width);
  return w.attr("x2", E + 3 * getConfig2().state.padding), e.insert("rect", ":first-child").attr("x", getConfig2().state.padding).attr("y", getConfig2().state.padding).attr("width", E + 2 * getConfig2().state.padding).attr("height", T.height + v + 2 * getConfig2().state.padding).attr("rx", getConfig2().state.radius), e;
}, "drawDescrState"), addTitleAndBox = /* @__PURE__ */ __name((e, h, g) => {
  let _ = getConfig2().state.padding, v = 2 * getConfig2().state.padding, y = e.node().getBBox(), b = y.width, x = y.x, S = e.append("text").attr("x", 0).attr("y", getConfig2().state.titleShift).attr("font-size", getConfig2().state.fontSize).attr("class", "state-title").text(h.id), w = S.node().getBBox().width + v, T = Math.max(w, b);
  T === b && (T += v);
  let E, D = e.node().getBBox();
  h.doc, E = x - _, w > b && (E = (b - T) / 2 + _), Math.abs(x - D.x) < _ && w > b && (E = x - (w - b) / 2);
  let O = 1 - getConfig2().state.textHeight;
  return e.insert("rect", ":first-child").attr("x", E).attr("y", O).attr("class", g ? "alt-composit" : "composit").attr("width", T).attr("height", D.height + getConfig2().state.textHeight + getConfig2().state.titleShift + 1).attr("rx", "0"), S.attr("x", E + _), w <= b && S.attr("x", x + (T - v) / 2 - w / 2 + _), e.insert("rect", ":first-child").attr("x", E).attr("y", getConfig2().state.titleShift - getConfig2().state.textHeight - getConfig2().state.padding).attr("width", T).attr("height", getConfig2().state.textHeight * 3).attr("rx", getConfig2().state.radius), e.insert("rect", ":first-child").attr("x", E).attr("y", getConfig2().state.titleShift - getConfig2().state.textHeight - getConfig2().state.padding).attr("width", T).attr("height", D.height + 3 + 2 * getConfig2().state.textHeight).attr("rx", getConfig2().state.radius), e;
}, "addTitleAndBox"), drawEndState = /* @__PURE__ */ __name((e) => (e.append("circle").attr("class", "end-state-outer").attr("r", getConfig2().state.sizeUnit + getConfig2().state.miniPadding).attr("cx", getConfig2().state.padding + getConfig2().state.sizeUnit + getConfig2().state.miniPadding).attr("cy", getConfig2().state.padding + getConfig2().state.sizeUnit + getConfig2().state.miniPadding), e.append("circle").attr("class", "end-state-inner").attr("r", getConfig2().state.sizeUnit).attr("cx", getConfig2().state.padding + getConfig2().state.sizeUnit + 2).attr("cy", getConfig2().state.padding + getConfig2().state.sizeUnit + 2)), "drawEndState"), drawForkJoinState = /* @__PURE__ */ __name((e, h) => {
  let g = getConfig2().state.forkWidth, _ = getConfig2().state.forkHeight;
  if (h.parentId) {
    let e2 = g;
    g = _, _ = e2;
  }
  return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", g).attr("height", _).attr("x", getConfig2().state.padding).attr("y", getConfig2().state.padding);
}, "drawForkJoinState"), _drawLongText = /* @__PURE__ */ __name((e, h, g, _) => {
  let v = 0, y = _.append("text");
  y.style("text-anchor", "start"), y.attr("class", "noteText");
  let b = e.replace(/\r\n/g, "<br/>");
  b = b.replace(/\n/g, "<br/>");
  let x = b.split(common_default.lineBreakRegex), S = 1.25 * getConfig2().state.noteMargin;
  for (let e2 of x) {
    let _2 = e2.trim();
    if (_2.length > 0) {
      let e3 = y.append("tspan");
      if (e3.text(_2), S === 0) {
        let h2 = e3.node().getBBox();
        S += h2.height;
      }
      v += S, e3.attr("x", h + getConfig2().state.noteMargin), e3.attr("y", g + v + 1.25 * getConfig2().state.noteMargin);
    }
  }
  return {
    textWidth: y.node().getBBox().width,
    textHeight: v
  };
}, "_drawLongText"), drawNote = /* @__PURE__ */ __name((e, h) => {
  h.attr("class", "state-note");
  let g = h.append("rect").attr("x", 0).attr("y", getConfig2().state.padding), { textWidth: _, textHeight: v } = _drawLongText(e, 0, 0, h.append("g"));
  return g.attr("height", v + 2 * getConfig2().state.noteMargin), g.attr("width", _ + getConfig2().state.noteMargin * 2), g;
}, "drawNote"), drawState = /* @__PURE__ */ __name(function(e, h) {
  let g = h.id, _ = {
    id: g,
    label: h.id,
    width: 0,
    height: 0
  }, v = e.append("g").attr("id", g).attr("class", "stateGroup");
  h.type === "start" && drawStartState(v), h.type === "end" && drawEndState(v), (h.type === "fork" || h.type === "join") && drawForkJoinState(v, h), h.type === "note" && drawNote(h.note.text, v), h.type === "divider" && drawDivider(v), h.type === "default" && h.descriptions.length === 0 && drawSimpleState(v, h), h.type === "default" && h.descriptions.length > 0 && drawDescrState(v, h);
  let y = v.node().getBBox();
  return _.width = y.width + 2 * getConfig2().state.padding, _.height = y.height + 2 * getConfig2().state.padding, _;
}, "drawState"), edgeCount = 0, drawEdge = /* @__PURE__ */ __name(function(e, h, x) {
  let w = /* @__PURE__ */ __name(function(e2) {
    switch (e2) {
      case StateDB.relationType.AGGREGATION:
        return "aggregation";
      case StateDB.relationType.EXTENSION:
        return "extension";
      case StateDB.relationType.COMPOSITION:
        return "composition";
      case StateDB.relationType.DEPENDENCY:
        return "dependency";
    }
  }, "getRelationType");
  h.points = h.points.filter((e2) => !Number.isNaN(e2.y));
  let E = h.points, D = line_default().x(function(e2) {
    return e2.x;
  }).y(function(e2) {
    return e2.y;
  }).curve(basis_default), k = e.append("path").attr("d", D(E)).attr("id", "edge" + edgeCount).attr("class", "transition"), A = "";
  if (getConfig2().state.arrowMarkerAbsolute && (A = getUrl(true)), k.attr("marker-end", "url(" + A + "#" + w(StateDB.relationType.DEPENDENCY) + "End)"), x.title !== void 0) {
    let g = e.append("g").attr("class", "stateLabel"), { x: _, y: b } = utils_default.calcLabelPosition(h.points), S = common_default.getRows(x.title), w2 = 0, E2 = [], D2 = 0, O = 0;
    for (let e2 = 0; e2 <= S.length; e2++) {
      let h2 = g.append("text").attr("text-anchor", "middle").text(S[e2]).attr("x", _).attr("y", b + w2), v = h2.node().getBBox();
      D2 = Math.max(D2, v.width), O = Math.min(O, v.x), log.info(v.x, _, b + w2), w2 === 0 && (w2 = h2.node().getBBox().height, log.info("Title height", w2, b)), E2.push(h2);
    }
    let k2 = w2 * S.length;
    if (S.length > 1) {
      let e2 = (S.length - 1) * w2 * 0.5;
      E2.forEach((h2, g2) => h2.attr("y", b + g2 * w2 - e2)), k2 = w2 * S.length;
    }
    let A2 = g.node().getBBox();
    g.insert("rect", ":first-child").attr("class", "box").attr("x", _ - D2 / 2 - getConfig2().state.padding / 2).attr("y", b - k2 / 2 - getConfig2().state.padding / 2 - 3.5).attr("width", D2 + getConfig2().state.padding).attr("height", k2 + getConfig2().state.padding), log.info(A2);
  }
  edgeCount++;
}, "drawEdge"), conf, transformationLog = {}, setConf = /* @__PURE__ */ __name(function() {
}, "setConf"), insertMarkers = /* @__PURE__ */ __name(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"), draw = /* @__PURE__ */ __name(function(e, h, g, _) {
  conf = getConfig2().state;
  let v = getConfig2().securityLevel, b;
  v === "sandbox" && (b = select_default("#i" + h));
  let S = select_default(v === "sandbox" ? b.nodes()[0].contentDocument.body : "body"), T = v === "sandbox" ? b.nodes()[0].contentDocument : document;
  log.debug("Rendering diagram " + e);
  let E = S.select(`[id='${h}']`);
  insertMarkers(E), renderDoc(_.db.getRootDoc(), E, void 0, false, S, T, _);
  let D = conf.padding, O = E.node().getBBox(), k = O.width + D * 2, A = O.height + D * 2;
  configureSvgSize(E, A, k * 1.75, conf.useMaxWidth), E.attr("viewBox", `${O.x - conf.padding}  ${O.y - conf.padding} ` + k + " " + A);
}, "draw"), getLabelWidth = /* @__PURE__ */ __name((e) => e ? e.length * conf.fontSizeFactor : 1, "getLabelWidth"), renderDoc = /* @__PURE__ */ __name((g, _, v, b, x, S, C) => {
  let w = new Graph({
    compound: true,
    multigraph: true
  }), E, D = true;
  for (E = 0; E < g.length; E++) if (g[E].stmt === "relation") {
    D = false;
    break;
  }
  v ? w.setGraph({
    rankdir: "LR",
    multigraph: true,
    compound: true,
    ranker: "tight-tree",
    ranksep: D ? 1 : conf.edgeLengthFactor,
    nodeSep: D ? 1 : 50,
    isMultiGraph: true
  }) : w.setGraph({
    rankdir: "TB",
    multigraph: true,
    compound: true,
    ranksep: D ? 1 : conf.edgeLengthFactor,
    nodeSep: D ? 1 : 50,
    ranker: "tight-tree",
    isMultiGraph: true
  }), w.setDefaultEdgeLabel(function() {
    return {};
  });
  let O = C.db.getStates(), k = C.db.getRelations(), A = Object.keys(O);
  for (let e of A) {
    let h = O[e];
    v && (h.parentId = v);
    let g2;
    if (h.doc) {
      let e2 = _.append("g").attr("id", h.id).attr("class", "stateGroup");
      g2 = renderDoc(h.doc, e2, h.id, !b, x, S, C);
      {
        e2 = addTitleAndBox(e2, h, b);
        let _2 = e2.node().getBBox();
        g2.width = _2.width, g2.height = _2.height + conf.padding / 2, transformationLog[h.id] = { y: conf.compositTitleSize };
      }
    } else g2 = drawState(_, h, w);
    if (h.note) {
      let e2 = drawState(_, {
        descriptions: [],
        id: h.id + "-note",
        note: h.note,
        type: "note"
      }, w);
      h.note.position === "left of" ? (w.setNode(g2.id + "-note", e2), w.setNode(g2.id, g2)) : (w.setNode(g2.id, g2), w.setNode(g2.id + "-note", e2)), w.setParent(g2.id, g2.id + "-group"), w.setParent(g2.id + "-note", g2.id + "-group");
    } else w.setNode(g2.id, g2);
  }
  log.debug("Count=", w.nodeCount(), w);
  let j = 0;
  k.forEach(function(e) {
    j++, log.debug("Setting edge", e), w.setEdge(e.id1, e.id2, {
      relation: e,
      width: getLabelWidth(e.title),
      height: conf.labelHeight * common_default.getRows(e.title).length,
      labelpos: "c"
    }, "id" + j);
  }), layout(w), log.debug("Graph after layout", w.nodes());
  let M = _.node();
  w.nodes().forEach(function(e) {
    e !== void 0 && w.node(e) !== void 0 ? (log.warn("Node " + e + ": " + JSON.stringify(w.node(e))), x.select("#" + M.id + " #" + e).attr("transform", "translate(" + (w.node(e).x - w.node(e).width / 2) + "," + (w.node(e).y + (transformationLog[e] ? transformationLog[e].y : 0) - w.node(e).height / 2) + " )"), x.select("#" + M.id + " #" + e).attr("data-x-shift", w.node(e).x - w.node(e).width / 2), S.querySelectorAll("#" + M.id + " #" + e + " .divider").forEach((e2) => {
      let h = e2.parentElement, g2 = 0, _2 = 0;
      h && (h.parentElement && (g2 = h.parentElement.getBBox().width), _2 = parseInt(h.getAttribute("data-x-shift"), 10), Number.isNaN(_2) && (_2 = 0)), e2.setAttribute("x1", 0 - _2 + 8), e2.setAttribute("x2", g2 - _2 - 8);
    })) : log.debug("No Node " + e + ": " + JSON.stringify(w.node(e)));
  });
  let N = M.getBBox();
  w.edges().forEach(function(e) {
    e !== void 0 && w.edge(e) !== void 0 && (log.debug("Edge " + e.v + " -> " + e.w + ": " + JSON.stringify(w.edge(e))), drawEdge(_, w.edge(e), w.edge(e).relation));
  }), N = M.getBBox();
  let P = {
    id: v || "root",
    label: v || "root",
    width: 0,
    height: 0
  };
  return P.width = N.width + 2 * conf.padding, P.height = N.height + 2 * conf.padding, log.debug("Doc rendered", P, w), P;
}, "renderDoc"), diagram = {
  parser: stateDiagram_default,
  get db() {
    return new StateDB(1);
  },
  renderer: {
    setConf,
    draw
  },
  styles: styles_default,
  init: /* @__PURE__ */ __name((e) => {
    e.state || (e.state = {}), e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
  }, "init")
};
export {
  diagram
};
