import { g as e, h as t, p as n } from "./src-CFVe5TPD.js";
import { D as r, b as i, c as a, s as o } from "./chunk-ICPOFSXX-B9JXAgJU.js";
import { H as s, h as c } from "./chunk-5PVQY5BW-BCjrVrWB.js";
import { t as l } from "./line-BF1XS-Ng.js";
import "./chunk-U2HBQHQK-75Isf1R4.js";
import "./chunk-BSJP7CBP-DE8WR0MI.js";
import "./chunk-ZZ45TVLE-DUagTZ9d.js";
import { t as u } from "./graphlib-DrXiQBFg.js";
import { t as d } from "./dagre-kfvGmNUa.js";
import "./chunk-55IACEB6-DBivnNb2.js";
import "./chunk-EDXVE4YY-DE9KzYM-.js";
import "./chunk-X2U36JSP-DSzducJI.js";
import "./chunk-5FUZZQ4R-tiEjqlP4.js";
import "./chunk-ENJZ2VHE-DlxIC-A3.js";
import "./chunk-336JU56O-CFKWNcxR.js";
import { i as f, n as p, t as m } from "./chunk-OYMX7WX6-D2UHnmfD.js";
//#region ../../node_modules/.pnpm/mermaid@11.14.0/node_modules/mermaid/dist/chunks/mermaid.core/stateDiagram-FHFEXIEX.mjs
var h = /* @__PURE__ */ t((e) => e.append("circle").attr("class", "start-state").attr("r", i().state.sizeUnit).attr("cx", i().state.padding + i().state.sizeUnit).attr("cy", i().state.padding + i().state.sizeUnit), "drawStartState"), g = /* @__PURE__ */ t((e) => e.append("line").style("stroke", "grey").style("stroke-dasharray", "3").attr("x1", i().state.textHeight).attr("class", "divider").attr("x2", i().state.textHeight * 2).attr("y1", 0).attr("y2", 0), "drawDivider"), _ = /* @__PURE__ */ t((e, t) => {
	let n = e.append("text").attr("x", 2 * i().state.padding).attr("y", i().state.textHeight + 2 * i().state.padding).attr("font-size", i().state.fontSize).attr("class", "state-title").text(t.id), r = n.node().getBBox();
	return e.insert("rect", ":first-child").attr("x", i().state.padding).attr("y", i().state.padding).attr("width", r.width + 2 * i().state.padding).attr("height", r.height + 2 * i().state.padding).attr("rx", i().state.radius), n;
}, "drawSimpleState"), v = /* @__PURE__ */ t((e, n) => {
	let r = /* @__PURE__ */ t(function(e, t, n) {
		let r = e.append("tspan").attr("x", 2 * i().state.padding).text(t);
		n || r.attr("dy", i().state.textHeight);
	}, "addTspan"), a = e.append("text").attr("x", 2 * i().state.padding).attr("y", i().state.textHeight + 1.3 * i().state.padding).attr("font-size", i().state.fontSize).attr("class", "state-title").text(n.descriptions[0]).node().getBBox(), o = a.height, s = e.append("text").attr("x", i().state.padding).attr("y", o + i().state.padding * .4 + i().state.dividerMargin + i().state.textHeight).attr("class", "state-description"), c = !0, l = !0;
	n.descriptions.forEach(function(e) {
		c || (r(s, e, l), l = !1), c = !1;
	});
	let u = e.append("line").attr("x1", i().state.padding).attr("y1", i().state.padding + o + i().state.dividerMargin / 2).attr("y2", i().state.padding + o + i().state.dividerMargin / 2).attr("class", "descr-divider"), d = s.node().getBBox(), f = Math.max(d.width, a.width);
	return u.attr("x2", f + 3 * i().state.padding), e.insert("rect", ":first-child").attr("x", i().state.padding).attr("y", i().state.padding).attr("width", f + 2 * i().state.padding).attr("height", d.height + o + 2 * i().state.padding).attr("rx", i().state.radius), e;
}, "drawDescrState"), y = /* @__PURE__ */ t((e, t, n) => {
	let r = i().state.padding, a = 2 * i().state.padding, o = e.node().getBBox(), s = o.width, c = o.x, l = e.append("text").attr("x", 0).attr("y", i().state.titleShift).attr("font-size", i().state.fontSize).attr("class", "state-title").text(t.id), u = l.node().getBBox().width + a, d = Math.max(u, s);
	d === s && (d += a);
	let f, p = e.node().getBBox();
	t.doc, f = c - r, u > s && (f = (s - d) / 2 + r), Math.abs(c - p.x) < r && u > s && (f = c - (u - s) / 2);
	let m = 1 - i().state.textHeight;
	return e.insert("rect", ":first-child").attr("x", f).attr("y", m).attr("class", n ? "alt-composit" : "composit").attr("width", d).attr("height", p.height + i().state.textHeight + i().state.titleShift + 1).attr("rx", "0"), l.attr("x", f + r), u <= s && l.attr("x", c + (d - a) / 2 - u / 2 + r), e.insert("rect", ":first-child").attr("x", f).attr("y", i().state.titleShift - i().state.textHeight - i().state.padding).attr("width", d).attr("height", i().state.textHeight * 3).attr("rx", i().state.radius), e.insert("rect", ":first-child").attr("x", f).attr("y", i().state.titleShift - i().state.textHeight - i().state.padding).attr("width", d).attr("height", p.height + 3 + 2 * i().state.textHeight).attr("rx", i().state.radius), e;
}, "addTitleAndBox"), b = /* @__PURE__ */ t((e) => (e.append("circle").attr("class", "end-state-outer").attr("r", i().state.sizeUnit + i().state.miniPadding).attr("cx", i().state.padding + i().state.sizeUnit + i().state.miniPadding).attr("cy", i().state.padding + i().state.sizeUnit + i().state.miniPadding), e.append("circle").attr("class", "end-state-inner").attr("r", i().state.sizeUnit).attr("cx", i().state.padding + i().state.sizeUnit + 2).attr("cy", i().state.padding + i().state.sizeUnit + 2)), "drawEndState"), x = /* @__PURE__ */ t((e, t) => {
	let n = i().state.forkWidth, r = i().state.forkHeight;
	if (t.parentId) {
		let e = n;
		n = r, r = e;
	}
	return e.append("rect").style("stroke", "black").style("fill", "black").attr("width", n).attr("height", r).attr("x", i().state.padding).attr("y", i().state.padding);
}, "drawForkJoinState"), S = /* @__PURE__ */ t((e, t, n, r) => {
	let a = 0, s = r.append("text");
	s.style("text-anchor", "start"), s.attr("class", "noteText");
	let c = e.replace(/\r\n/g, "<br/>");
	c = c.replace(/\n/g, "<br/>");
	let l = c.split(o.lineBreakRegex), u = 1.25 * i().state.noteMargin;
	for (let e of l) {
		let r = e.trim();
		if (r.length > 0) {
			let e = s.append("tspan");
			if (e.text(r), u === 0) {
				let t = e.node().getBBox();
				u += t.height;
			}
			a += u, e.attr("x", t + i().state.noteMargin), e.attr("y", n + a + 1.25 * i().state.noteMargin);
		}
	}
	return {
		textWidth: s.node().getBBox().width,
		textHeight: a
	};
}, "_drawLongText"), C = /* @__PURE__ */ t((e, t) => {
	t.attr("class", "state-note");
	let n = t.append("rect").attr("x", 0).attr("y", i().state.padding), { textWidth: r, textHeight: a } = S(e, 0, 0, t.append("g"));
	return n.attr("height", a + 2 * i().state.noteMargin), n.attr("width", r + i().state.noteMargin * 2), n;
}, "drawNote"), w = /* @__PURE__ */ t(function(e, t) {
	let n = t.id, r = {
		id: n,
		label: t.id,
		width: 0,
		height: 0
	}, a = e.append("g").attr("id", n).attr("class", "stateGroup");
	t.type === "start" && h(a), t.type === "end" && b(a), (t.type === "fork" || t.type === "join") && x(a, t), t.type === "note" && C(t.note.text, a), t.type === "divider" && g(a), t.type === "default" && t.descriptions.length === 0 && _(a, t), t.type === "default" && t.descriptions.length > 0 && v(a, t);
	let o = a.node().getBBox();
	return r.width = o.width + 2 * i().state.padding, r.height = o.height + 2 * i().state.padding, r;
}, "drawState"), T = 0, E = /* @__PURE__ */ t(function(n, a, u) {
	let d = /* @__PURE__ */ t(function(e) {
		switch (e) {
			case m.relationType.AGGREGATION: return "aggregation";
			case m.relationType.EXTENSION: return "extension";
			case m.relationType.COMPOSITION: return "composition";
			case m.relationType.DEPENDENCY: return "dependency";
		}
	}, "getRelationType");
	a.points = a.points.filter((e) => !Number.isNaN(e.y));
	let f = a.points, p = l().x(function(e) {
		return e.x;
	}).y(function(e) {
		return e.y;
	}).curve(s), h = n.append("path").attr("d", p(f)).attr("id", "edge" + T).attr("class", "transition"), g = "";
	if (i().state.arrowMarkerAbsolute && (g = r(!0)), h.attr("marker-end", "url(" + g + "#" + d(m.relationType.DEPENDENCY) + "End)"), u.title !== void 0) {
		let t = n.append("g").attr("class", "stateLabel"), { x: r, y: s } = c.calcLabelPosition(a.points), l = o.getRows(u.title), d = 0, f = [], p = 0, m = 0;
		for (let n = 0; n <= l.length; n++) {
			let i = t.append("text").attr("text-anchor", "middle").text(l[n]).attr("x", r).attr("y", s + d), a = i.node().getBBox();
			p = Math.max(p, a.width), m = Math.min(m, a.x), e.info(a.x, r, s + d), d === 0 && (d = i.node().getBBox().height, e.info("Title height", d, s)), f.push(i);
		}
		let h = d * l.length;
		if (l.length > 1) {
			let e = (l.length - 1) * d * .5;
			f.forEach((t, n) => t.attr("y", s + n * d - e)), h = d * l.length;
		}
		let g = t.node().getBBox();
		t.insert("rect", ":first-child").attr("class", "box").attr("x", r - p / 2 - i().state.padding / 2).attr("y", s - h / 2 - i().state.padding / 2 - 3.5).attr("width", p + i().state.padding).attr("height", h + i().state.padding), e.info(g);
	}
	T++;
}, "drawEdge"), D, O = {}, k = /* @__PURE__ */ t(function() {}, "setConf"), A = /* @__PURE__ */ t(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"), j = /* @__PURE__ */ t(function(t, r, o, s) {
	D = i().state;
	let c = i().securityLevel, l;
	c === "sandbox" && (l = n("#i" + r));
	let u = n(c === "sandbox" ? l.nodes()[0].contentDocument.body : "body"), d = c === "sandbox" ? l.nodes()[0].contentDocument : document;
	e.debug("Rendering diagram " + t);
	let f = u.select(`[id='${r}']`);
	A(f), N(s.db.getRootDoc(), f.append("g").attr("id", r + "-root"), void 0, !1, u, d, s);
	let p = D.padding, m = f.node().getBBox(), h = m.width + p * 2, g = m.height + p * 2;
	a(f, g, h * 1.75, D.useMaxWidth), f.attr("viewBox", `${m.x - D.padding}  ${m.y - D.padding} ` + h + " " + g);
}, "draw"), M = /* @__PURE__ */ t((e) => e ? e.length * D.fontSizeFactor : 1, "getLabelWidth"), N = /* @__PURE__ */ t((t, n, r, i, a, s, c) => {
	let l = new u({
		compound: !0,
		multigraph: !0
	}), f, p = !0;
	for (f = 0; f < t.length; f++) if (t[f].stmt === "relation") {
		p = !1;
		break;
	}
	r ? l.setGraph({
		rankdir: "LR",
		multigraph: !0,
		compound: !0,
		ranker: "tight-tree",
		ranksep: p ? 1 : D.edgeLengthFactor,
		nodeSep: p ? 1 : 50,
		isMultiGraph: !0
	}) : l.setGraph({
		rankdir: "TB",
		multigraph: !0,
		compound: !0,
		ranksep: p ? 1 : D.edgeLengthFactor,
		nodeSep: p ? 1 : 50,
		ranker: "tight-tree",
		isMultiGraph: !0
	}), l.setDefaultEdgeLabel(function() {
		return {};
	});
	let m = c.db.getStates(), h = c.db.getRelations(), g = Object.keys(m);
	for (let e of g) {
		let t = m[e];
		r && (t.parentId = r);
		let o;
		if (t.doc) {
			let e = n.append("g").attr("id", t.id).attr("class", "stateGroup");
			o = N(t.doc, e, t.id, !i, a, s, c);
			{
				e = y(e, t, i);
				let n = e.node().getBBox();
				o.width = n.width, o.height = n.height + D.padding / 2, O[t.id] = { y: D.compositTitleSize };
			}
		} else o = w(n, t, l);
		if (t.note) {
			let e = w(n, {
				descriptions: [],
				id: t.id + "-note",
				note: t.note,
				type: "note"
			}, l);
			t.note.position === "left of" ? (l.setNode(o.id + "-note", e), l.setNode(o.id, o)) : (l.setNode(o.id, o), l.setNode(o.id + "-note", e)), l.setParent(o.id, o.id + "-group"), l.setParent(o.id + "-note", o.id + "-group");
		} else l.setNode(o.id, o);
	}
	e.debug("Count=", l.nodeCount(), l);
	let _ = 0;
	h.forEach(function(t) {
		_++, e.debug("Setting edge", t), l.setEdge(t.id1, t.id2, {
			relation: t,
			width: M(t.title),
			height: D.labelHeight * o.getRows(t.title).length,
			labelpos: "c"
		}, "id" + _);
	}), d(l), e.debug("Graph after layout", l.nodes());
	let v = n.node();
	l.nodes().forEach(function(t) {
		t !== void 0 && l.node(t) !== void 0 ? (e.warn("Node " + t + ": " + JSON.stringify(l.node(t))), a.select("#" + v.id + " #" + t).attr("transform", "translate(" + (l.node(t).x - l.node(t).width / 2) + "," + (l.node(t).y + (O[t] ? O[t].y : 0) - l.node(t).height / 2) + " )"), a.select("#" + v.id + " #" + t).attr("data-x-shift", l.node(t).x - l.node(t).width / 2), s.querySelectorAll("#" + v.id + " #" + t + " .divider").forEach((e) => {
			let t = e.parentElement, n = 0, r = 0;
			t && (t.parentElement && (n = t.parentElement.getBBox().width), r = parseInt(t.getAttribute("data-x-shift"), 10), Number.isNaN(r) && (r = 0)), e.setAttribute("x1", 0 - r + 8), e.setAttribute("x2", n - r - 8);
		})) : e.debug("No Node " + t + ": " + JSON.stringify(l.node(t)));
	});
	let b = v.getBBox();
	l.edges().forEach(function(t) {
		t !== void 0 && l.edge(t) !== void 0 && (e.debug("Edge " + t.v + " -> " + t.w + ": " + JSON.stringify(l.edge(t))), E(n, l.edge(t), l.edge(t).relation));
	}), b = v.getBBox();
	let x = {
		id: r || "root",
		label: r || "root",
		width: 0,
		height: 0
	};
	return x.width = b.width + 2 * D.padding, x.height = b.height + 2 * D.padding, e.debug("Doc rendered", x, l), x;
}, "renderDoc"), P = {
	parser: p,
	get db() {
		return new m(1);
	},
	renderer: {
		setConf: k,
		draw: j
	},
	styles: f,
	init: /* @__PURE__ */ t((e) => {
		e.state ||= {}, e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
	}, "init")
};
//#endregion
export { P as diagram };
