import { __name as e, log as t } from "./chunk-AGHRB4JF.js";
import { common_default as n, configureSvgSize as r, getConfig2 as i, getUrl as a } from "./chunk-ICPOFSXX.js";
import o from "../../../../d3-selection/src/select.js";
import s from "../../../../d3-shape/src/line.js";
import c from "../../../../d3-shape/src/curve/basis.js";
import "../../../../d3/src/index.js";
import "./chunk-BSJP7CBP.js";
import { utils_default as l } from "./chunk-5PVQY5BW.js";
import "./chunk-ZZ45TVLE.js";
import "./chunk-X2U36JSP.js";
import "./chunk-U2HBQHQK.js";
import "./chunk-5FUZZQ4R.js";
import "./chunk-ENJZ2VHE.js";
import "./chunk-336JU56O.js";
import { Graph as u } from "../../../../dagre-d3-es/src/graphlib/graph.js";
import "../../../../dagre-d3-es/src/graphlib/index.js";
import "./chunk-55IACEB6.js";
import "./chunk-EDXVE4YY.js";
import { layout as d } from "../../../../dagre-d3-es/src/dagre/layout.js";
import "../../../../dagre-d3-es/src/dagre/index.js";
import { StateDB as f, stateDiagram_default as p, styles_default as m } from "./chunk-OYMX7WX6.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/stateDiagram-FHFEXIEX.mjs
var h = /* @__PURE__ */ e((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__ */ e((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__ */ e((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__ */ e((t, n) => {
	let r = /* @__PURE__ */ e(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 = t.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 = t.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 = t.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), t.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), t;
}, "drawDescrState"), y = /* @__PURE__ */ e((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__ */ e((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__ */ e((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__ */ e((e, t, r, a) => {
	let o = 0, s = a.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(n.lineBreakRegex), u = 1.25 * i().state.noteMargin;
	for (let e of l) {
		let n = e.trim();
		if (n.length > 0) {
			let e = s.append("tspan");
			if (e.text(n), u === 0) {
				let t = e.node().getBBox();
				u += t.height;
			}
			o += u, e.attr("x", t + i().state.noteMargin), e.attr("y", r + o + 1.25 * i().state.noteMargin);
		}
	}
	return {
		textWidth: s.node().getBBox().width,
		textHeight: o
	};
}, "_drawLongText"), C = /* @__PURE__ */ e((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__ */ e(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__ */ e(function(r, o, u) {
	let d = /* @__PURE__ */ e(function(e) {
		switch (e) {
			case f.relationType.AGGREGATION: return "aggregation";
			case f.relationType.EXTENSION: return "extension";
			case f.relationType.COMPOSITION: return "composition";
			case f.relationType.DEPENDENCY: return "dependency";
		}
	}, "getRelationType");
	o.points = o.points.filter((e) => !Number.isNaN(e.y));
	let p = o.points, m = s().x(function(e) {
		return e.x;
	}).y(function(e) {
		return e.y;
	}).curve(c), h = r.append("path").attr("d", m(p)).attr("id", "edge" + T).attr("class", "transition"), g = "";
	if (i().state.arrowMarkerAbsolute && (g = a(!0)), h.attr("marker-end", "url(" + g + "#" + d(f.relationType.DEPENDENCY) + "End)"), u.title !== void 0) {
		let e = r.append("g").attr("class", "stateLabel"), { x: a, y: s } = l.calcLabelPosition(o.points), c = n.getRows(u.title), d = 0, f = [], p = 0, m = 0;
		for (let n = 0; n <= c.length; n++) {
			let r = e.append("text").attr("text-anchor", "middle").text(c[n]).attr("x", a).attr("y", s + d), i = r.node().getBBox();
			p = Math.max(p, i.width), m = Math.min(m, i.x), t.info(i.x, a, s + d), d === 0 && (d = r.node().getBBox().height, t.info("Title height", d, s)), f.push(r);
		}
		let h = d * c.length;
		if (c.length > 1) {
			let e = (c.length - 1) * d * .5;
			f.forEach((t, n) => t.attr("y", s + n * d - e)), h = d * c.length;
		}
		let g = e.node().getBBox();
		e.insert("rect", ":first-child").attr("class", "box").attr("x", a - 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), t.info(g);
	}
	T++;
}, "drawEdge"), D, O = {}, k = /* @__PURE__ */ e(function() {}, "setConf"), A = /* @__PURE__ */ e(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__ */ e(function(e, n, a, s) {
	D = i().state;
	let c = i().securityLevel, l;
	c === "sandbox" && (l = o("#i" + n));
	let u = o(c === "sandbox" ? l.nodes()[0].contentDocument.body : "body"), d = c === "sandbox" ? l.nodes()[0].contentDocument : document;
	t.debug("Rendering diagram " + e);
	let f = u.select(`[id='${n}']`);
	A(f), N(s.db.getRootDoc(), f.append("g").attr("id", n + "-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;
	r(f, g, h * 1.75, D.useMaxWidth), f.attr("viewBox", `${m.x - D.padding}  ${m.y - D.padding} ` + h + " " + g);
}, "draw"), M = /* @__PURE__ */ e((e) => e ? e.length * D.fontSizeFactor : 1, "getLabelWidth"), N = /* @__PURE__ */ e((e, r, i, a, o, s, c) => {
	let l = new u({
		compound: !0,
		multigraph: !0
	}), f, p = !0;
	for (f = 0; f < e.length; f++) if (e[f].stmt === "relation") {
		p = !1;
		break;
	}
	i ? 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];
		i && (t.parentId = i);
		let n;
		if (t.doc) {
			let e = r.append("g").attr("id", t.id).attr("class", "stateGroup");
			n = N(t.doc, e, t.id, !a, o, s, c);
			{
				e = y(e, t, a);
				let r = e.node().getBBox();
				n.width = r.width, n.height = r.height + D.padding / 2, O[t.id] = { y: D.compositTitleSize };
			}
		} else n = w(r, t, l);
		if (t.note) {
			let e = w(r, {
				descriptions: [],
				id: t.id + "-note",
				note: t.note,
				type: "note"
			}, l);
			t.note.position === "left of" ? (l.setNode(n.id + "-note", e), l.setNode(n.id, n)) : (l.setNode(n.id, n), l.setNode(n.id + "-note", e)), l.setParent(n.id, n.id + "-group"), l.setParent(n.id + "-note", n.id + "-group");
		} else l.setNode(n.id, n);
	}
	t.debug("Count=", l.nodeCount(), l);
	let _ = 0;
	h.forEach(function(e) {
		_++, t.debug("Setting edge", e), l.setEdge(e.id1, e.id2, {
			relation: e,
			width: M(e.title),
			height: D.labelHeight * n.getRows(e.title).length,
			labelpos: "c"
		}, "id" + _);
	}), d(l), t.debug("Graph after layout", l.nodes());
	let v = r.node();
	l.nodes().forEach(function(e) {
		e !== void 0 && l.node(e) !== void 0 ? (t.warn("Node " + e + ": " + JSON.stringify(l.node(e))), o.select("#" + v.id + " #" + e).attr("transform", "translate(" + (l.node(e).x - l.node(e).width / 2) + "," + (l.node(e).y + (O[e] ? O[e].y : 0) - l.node(e).height / 2) + " )"), o.select("#" + v.id + " #" + e).attr("data-x-shift", l.node(e).x - l.node(e).width / 2), s.querySelectorAll("#" + v.id + " #" + e + " .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);
		})) : t.debug("No Node " + e + ": " + JSON.stringify(l.node(e)));
	});
	let b = v.getBBox();
	l.edges().forEach(function(e) {
		e !== void 0 && l.edge(e) !== void 0 && (t.debug("Edge " + e.v + " -> " + e.w + ": " + JSON.stringify(l.edge(e))), E(r, l.edge(e), l.edge(e).relation));
	}), b = v.getBBox();
	let x = {
		id: i || "root",
		label: i || "root",
		width: 0,
		height: 0
	};
	return x.width = b.width + 2 * D.padding, x.height = b.height + 2 * D.padding, t.debug("Doc rendered", x, l), x;
}, "renderDoc"), P = {
	parser: p,
	get db() {
		return new f(1);
	},
	renderer: {
		setConf: k,
		draw: j
	},
	styles: m,
	init: /* @__PURE__ */ e((e) => {
		e.state ||= {}, e.state.arrowMarkerAbsolute = e.arrowMarkerAbsolute;
	}, "init")
};
//#endregion
export { P as diagram };
