import "./chunk-5PVQY5BW-De02jDY6.js";
import { i as e, r as t } from "./src-CiYEh6Gu.js";
import { a as n } from "./flatten-BftWKfuJ.js";
import { b as r } from "./chunk-ICPOFSXX-BGDNDwyb.js";
import "./dist-6KEP7Sv7.js";
import "./chunk-U2HBQHQK-zQDW-w6z.js";
import "./chunk-BSJP7CBP-B_RLxeu-.js";
import { n as i } from "./chunk-ZZ45TVLE-DR9n48w9.js";
import { n as a, t as o } from "./graphlib-Ci0_poOu.js";
import { t as s } from "./clone-DeHxarYe.js";
import { t as c } from "./dagre-JZb6Mrnp.js";
import "./chunk-X2U36JSP-vovBAHWP.js";
import { a as l, c as u, i as d, l as f, n as p, t as m, u as h } from "./chunk-5FUZZQ4R-CJuRfR98.js";
import { a as g, i as _, n as v, r as y, t as b } from "./chunk-ENJZ2VHE-DJN4zRqT.js";
//#region ../../node_modules/.pnpm/dagre-d3-es@7.0.14/node_modules/dagre-d3-es/src/graphlib/json.js
function x(e) {
	var t = {
		options: {
			directed: e.isDirected(),
			multigraph: e.isMultigraph(),
			compound: e.isCompound()
		},
		nodes: S(e),
		edges: C(e)
	};
	return a(e.graph()) || (t.value = s(e.graph())), t;
}
function S(e) {
	return n(e.nodes(), function(t) {
		var n = e.node(t), r = e.parent(t), i = { v: t };
		return a(n) || (i.value = n), a(r) || (i.parent = r), i;
	});
}
function C(e) {
	return n(e.edges(), function(t) {
		var n = e.edge(t), r = {
			v: t.v,
			w: t.w
		};
		return a(t.name) || (r.name = t.name), a(n) || (r.value = n), r;
	});
}
//#endregion
//#region ../../node_modules/.pnpm/mermaid@11.14.0/node_modules/mermaid/dist/chunks/mermaid.core/dagre-KV5264BT.mjs
var w = /* @__PURE__ */ new Map(), T = /* @__PURE__ */ new Map(), E = /* @__PURE__ */ new Map(), D = /* @__PURE__ */ t(() => {
	T.clear(), E.clear(), w.clear();
}, "clear"), O = /* @__PURE__ */ t((t, n) => {
	let r = T.get(n) || [];
	return e.trace("In isDescendant", n, " ", t, " = ", r.includes(t)), r.includes(t);
}, "isDescendant"), k = /* @__PURE__ */ t((t, n) => {
	let r = T.get(n) || [];
	return e.info("Descendants of ", n, " is ", r), e.info("Edge is ", t), t.v === n || t.w === n ? !1 : r ? r.includes(t.v) || O(t.v, n) || O(t.w, n) || r.includes(t.w) : (e.debug("Tilt, ", n, ",not in descendants"), !1);
}, "edgeInCluster"), A = /* @__PURE__ */ t((t, n, r, i) => {
	e.warn("Copying children of ", t, "root", i, "data", n.node(t), i);
	let a = n.children(t) || [];
	t !== i && a.push(t), e.warn("Copying (nodes) clusterId", t, "nodes", a), a.forEach((a) => {
		if (n.children(a).length > 0) A(a, n, r, i);
		else {
			let o = n.node(a);
			e.info("cp ", a, " to ", i, " with parent ", t), r.setNode(a, o), i !== n.parent(a) && (e.warn("Setting parent", a, n.parent(a)), r.setParent(a, n.parent(a))), t !== i && a !== t ? (e.debug("Setting parent", a, t), r.setParent(a, t)) : (e.info("In copy ", t, "root", i, "data", n.node(t), i), e.debug("Not Setting parent for node=", a, "cluster!==rootId", t !== i, "node!==clusterId", a !== t));
			let s = n.edges(a);
			e.debug("Copying Edges", s), s.forEach((a) => {
				e.info("Edge", a);
				let o = n.edge(a.v, a.w, a.name);
				e.info("Edge data", o, i);
				try {
					k(a, i) ? (e.info("Copying as ", a.v, a.w, o, a.name), r.setEdge(a.v, a.w, o, a.name), e.info("newGraph edges ", r.edges(), r.edge(r.edges()[0]))) : e.info("Skipping copy of edge ", a.v, "-->", a.w, " rootId: ", i, " clusterId:", t);
				} catch (t) {
					e.error(t);
				}
			});
		}
		e.debug("Removing node", a), n.removeNode(a);
	});
}, "copy"), j = /* @__PURE__ */ t((e, t) => {
	let n = t.children(e), r = [...n];
	for (let i of n) E.set(i, e), r = [...r, ...j(i, t)];
	return r;
}, "extractDescendants"), M = /* @__PURE__ */ t((e, t, n) => {
	let r = e.edges().filter((e) => e.v === t || e.w === t), i = e.edges().filter((e) => e.v === n || e.w === n), a = r.map((e) => ({
		v: e.v === t ? n : e.v,
		w: e.w === t ? t : e.w
	})), o = i.map((e) => ({
		v: e.v,
		w: e.w
	}));
	return a.filter((e) => o.some((t) => e.v === t.v && e.w === t.w));
}, "findCommonEdges"), N = /* @__PURE__ */ t((t, n, r) => {
	let i = n.children(t);
	if (e.trace("Searching children of id ", t, i), i.length < 1) return t;
	let a;
	for (let e of i) {
		let t = N(e, n, r), i = M(n, r, t);
		if (t) if (i.length > 0) a = t;
		else return t;
	}
	return a;
}, "findNonClusterChild"), P = /* @__PURE__ */ t((e) => !w.has(e) || !w.get(e).externalConnections ? e : w.has(e) ? w.get(e).id : e, "getAnchorId"), F = /* @__PURE__ */ t((t, n) => {
	if (!t || n > 10) {
		e.debug("Opting out, no graph ");
		return;
	} else e.debug("Opting in, graph ");
	t.nodes().forEach(function(n) {
		t.children(n).length > 0 && (e.warn("Cluster identified", n, " Replacement id in edges: ", N(n, t, n)), T.set(n, j(n, t)), w.set(n, {
			id: N(n, t, n),
			clusterData: t.node(n)
		}));
	}), t.nodes().forEach(function(n) {
		let r = t.children(n), i = t.edges();
		r.length > 0 ? (e.debug("Cluster identified", n, T), i.forEach((t) => {
			O(t.v, n) ^ O(t.w, n) && (e.warn("Edge: ", t, " leaves cluster ", n), e.warn("Descendants of XXX ", n, ": ", T.get(n)), w.get(n).externalConnections = !0);
		})) : e.debug("Not a cluster ", n, T);
	});
	for (let e of w.keys()) {
		let n = w.get(e).id, r = t.parent(n);
		r !== e && w.has(r) && !w.get(r).externalConnections && (w.get(e).id = r);
	}
	t.edges().forEach(function(n) {
		let r = t.edge(n);
		e.warn("Edge " + n.v + " -> " + n.w + ": " + JSON.stringify(n)), e.warn("Edge " + n.v + " -> " + n.w + ": " + JSON.stringify(t.edge(n)));
		let i = n.v, a = n.w;
		if (e.warn("Fix XXX", w, "ids:", n.v, n.w, "Translating: ", w.get(n.v), " --- ", w.get(n.w)), w.get(n.v) || w.get(n.w)) {
			if (e.warn("Fixing and trying - removing XXX", n.v, n.w, n.name), i = P(n.v), a = P(n.w), t.removeEdge(n.v, n.w, n.name), i !== n.v) {
				let e = t.parent(i);
				w.get(e).externalConnections = !0, r.fromCluster = n.v;
			}
			if (a !== n.w) {
				let e = t.parent(a);
				w.get(e).externalConnections = !0, r.toCluster = n.w;
			}
			e.warn("Fix Replacing with XXX", i, a, n.name), t.setEdge(i, a, r, n.name);
		}
	}), e.warn("Adjusted Graph", x(t)), I(t, 0), e.trace(w);
}, "adjustClustersAndEdges"), I = /* @__PURE__ */ t((t, n) => {
	if (e.warn("extractor - ", n, x(t), t.children("D")), n > 10) {
		e.error("Bailing out");
		return;
	}
	let r = t.nodes(), i = !1;
	for (let e of r) {
		let n = t.children(e);
		i ||= n.length > 0;
	}
	if (!i) {
		e.debug("Done, no node has children", t.nodes());
		return;
	}
	e.debug("Nodes = ", r, n);
	for (let i of r) if (e.debug("Extracting node", i, w, w.has(i) && !w.get(i).externalConnections, !t.parent(i), t.node(i), t.children("D"), " Depth ", n), !w.has(i)) e.debug("Not a cluster", i, n);
	else if (!w.get(i).externalConnections && t.children(i) && t.children(i).length > 0) {
		e.warn("Cluster without external connections, without a parent and with children", i, n);
		let r = t.graph().rankdir === "TB" ? "LR" : "TB";
		w.get(i)?.clusterData?.dir && (r = w.get(i).clusterData.dir, e.warn("Fixing dir", w.get(i).clusterData.dir, r));
		let a = new o({
			multigraph: !0,
			compound: !0
		}).setGraph({
			rankdir: r,
			nodesep: 50,
			ranksep: 50,
			marginx: 8,
			marginy: 8
		}).setDefaultEdgeLabel(function() {
			return {};
		});
		e.warn("Old graph before copy", x(t)), A(i, t, a, i), t.setNode(i, {
			clusterNode: !0,
			id: i,
			clusterData: w.get(i).clusterData,
			label: w.get(i).label,
			graph: a
		}), e.warn("New graph after copy node: (", i, ")", x(a)), e.debug("Old graph after copy", x(t));
	} else e.warn("Cluster ** ", i, " **not meeting the criteria !externalConnections:", !w.get(i).externalConnections, " no parent: ", !t.parent(i), " children ", t.children(i) && t.children(i).length > 0, t.children("D"), n), e.debug(w);
	r = t.nodes(), e.warn("New list of nodes", r);
	for (let i of r) {
		let r = t.node(i);
		e.warn(" Now next level", i, r), r?.clusterNode && I(r.graph, n + 1);
	}
}, "extractor"), L = /* @__PURE__ */ t((e, t) => {
	if (t.length === 0) return [];
	let n = Object.assign([], t);
	return t.forEach((t) => {
		let r = L(e, e.children(t));
		n = [...n, ...r];
	}), n;
}, "sorter"), R = /* @__PURE__ */ t((e) => L(e, e.children()), "sortNodesByHierarchy"), z = /* @__PURE__ */ t(async (n, r, a, o, s, p) => {
	e.warn("Graph in recursive render:XAX", x(r), s);
	let m = r.graph().rankdir;
	e.trace("Dir in recursive render - dir:", m);
	let _ = n.insert("g").attr("class", "root");
	r.nodes() ? e.info("Recursive render XXX", r.nodes()) : e.info("No nodes found for", r), r.edges().length > 0 && e.info("Recursive edges", r.edge(r.edges()[0]));
	let b = _.insert("g").attr("class", "clusters"), S = _.insert("g").attr("class", "edgePaths"), C = _.insert("g").attr("class", "edgeLabels"), T = _.insert("g").attr("class", "nodes");
	await Promise.all(r.nodes().map(async function(t) {
		let n = r.node(t);
		if (s !== void 0) {
			let n = JSON.parse(JSON.stringify(s.clusterData));
			e.trace("Setting data for parent cluster XXX\n Node.id = ", t, "\n data=", n.height, "\nParent cluster", s.height), r.setNode(s.id, n), r.parent(t) || (e.trace("Setting parent", t, s.id), r.setParent(t, s.id, n));
		}
		if (e.info("(Insert) Node XXX" + t + ": " + JSON.stringify(r.node(t))), n?.clusterNode) {
			e.info("Cluster identified XBX", t, n.width, r.node(t));
			let { ranksep: i, nodesep: s } = r.graph();
			n.graph.setGraph({
				...n.graph.graph(),
				ranksep: i + 25,
				nodesep: s
			});
			let c = await z(T, n.graph, a, o, r.node(t), p), l = c.elem;
			h(n, l), n.diff = c.diff || 0, e.info("New compound node after recursive render XAX", t, "width", n.width, "height", n.height), f(l, n);
		} else r.children(t).length > 0 ? (e.trace("Cluster - the non recursive path XBX", t, n.id, n, n.width, "Graph:", r), e.trace(N(n.id, r)), w.set(n.id, {
			id: N(n.id, r),
			node: n
		})) : (e.trace("Node - the non recursive path XAX", t, T, r.node(t), m), await l(T, r.node(t), {
			config: p,
			dir: m
		}));
	})), await (/* @__PURE__ */ t(async () => {
		let t = r.edges().map(async function(t) {
			let n = r.edge(t.v, t.w, t.name);
			e.info("Edge " + t.v + " -> " + t.w + ": " + JSON.stringify(t)), e.info("Edge " + t.v + " -> " + t.w + ": ", t, " ", JSON.stringify(r.edge(t))), e.info("Fix", w, "ids:", t.v, t.w, "Translating: ", w.get(t.v), w.get(t.w)), await y(C, n);
		});
		await Promise.all(t);
	}, "processEdges"))(), e.info("Graph before layout:", JSON.stringify(x(r))), e.info("############################################# XXX"), e.info("###                Layout                 ### XXX"), e.info("############################################# XXX"), c(r), e.info("Graph after layout:", JSON.stringify(x(r)));
	let E = 0, { subGraphTitleTotalMargin: D } = i(p);
	return await Promise.all(R(r).map(async function(t) {
		let n = r.node(t);
		if (e.info("Position XBX => " + t + ": (" + n.x, "," + n.y, ") width: ", n.width, " height: ", n.height), n?.clusterNode) n.y += D, e.info("A tainted cluster node XBX1", t, n.id, n.width, n.height, n.x, n.y, r.parent(t)), w.get(n.id).node = n, u(n);
		else if (r.children(t).length > 0) {
			e.info("A pure cluster node XBX1", t, n.id, n.x, n.y, n.width, n.height, r.parent(t)), n.height += D, r.node(n.parentId);
			let i = n?.padding / 2 || 0, a = n?.labelBBox?.height || 0, o = a - i || 0;
			e.debug("OffsetY", o, "labelHeight", a, "halfPadding", i), await d(b, n), w.get(n.id).node = n;
		} else {
			let t = r.node(n.parentId);
			n.y += D / 2, e.info("A regular node XBX1 - using the padding", n.id, "parent", n.parentId, n.width, n.height, n.x, n.y, "offsetY", n.offsetY, "parent", t, t?.offsetY, n), u(n);
		}
	})), r.edges().forEach(function(t) {
		let n = r.edge(t);
		e.info("Edge " + t.v + " -> " + t.w + ": " + JSON.stringify(n), n), n.points.forEach((e) => e.y += D / 2), g(n, v(S, n, w, a, r.node(t.v), r.node(t.w), o));
	}), r.nodes().forEach(function(t) {
		let n = r.node(t);
		e.info(t, n.type, n.diff), n.isGroup && (E = n.diff);
	}), e.warn("Returning from recursive render XAX", _, E), {
		elem: _,
		diff: E
	};
}, "recursiveRender"), B = /* @__PURE__ */ t(async (t, n) => {
	let i = new o({
		multigraph: !0,
		compound: !0
	}).setGraph({
		rankdir: t.direction,
		nodesep: t.config?.nodeSpacing || t.config?.flowchart?.nodeSpacing || t.nodeSpacing,
		ranksep: t.config?.rankSpacing || t.config?.flowchart?.rankSpacing || t.rankSpacing,
		marginx: 8,
		marginy: 8
	}).setDefaultEdgeLabel(function() {
		return {};
	}), a = n.select("g");
	_(a, t.markers, t.type, t.diagramId), p(), b(), m(), D(), t.nodes.forEach((e) => {
		i.setNode(e.id, { ...e }), e.parentId && i.setParent(e.id, e.parentId);
	}), e.debug("Edges:", t.edges), t.edges.forEach((e) => {
		if (e.start === e.end) {
			let t = e.start, n = t + "---" + t + "---1", r = t + "---" + t + "---2", a = i.node(t);
			i.setNode(n, {
				domId: n,
				id: n,
				parentId: a.parentId,
				labelStyle: "",
				label: "",
				padding: 0,
				shape: "labelRect",
				style: "",
				width: 10,
				height: 10
			}), i.setParent(n, a.parentId), i.setNode(r, {
				domId: r,
				id: r,
				parentId: a.parentId,
				labelStyle: "",
				padding: 0,
				shape: "labelRect",
				label: "",
				style: "",
				width: 10,
				height: 10
			}), i.setParent(r, a.parentId);
			let o = structuredClone(e), s = structuredClone(e), c = structuredClone(e);
			o.label = "", o.arrowTypeEnd = "none", o.id = t + "-cyclic-special-1", s.arrowTypeStart = "none", s.arrowTypeEnd = "none", s.id = t + "-cyclic-special-mid", c.label = "", a.isGroup && (o.fromCluster = t, c.toCluster = t), c.id = t + "-cyclic-special-2", c.arrowTypeStart = "none", i.setEdge(t, n, o, t + "-cyclic-special-0"), i.setEdge(n, r, s, t + "-cyclic-special-1"), i.setEdge(r, t, c, t + "-cyc<lic-special-2");
		} else i.setEdge(e.start, e.end, { ...e }, e.id);
	}), e.warn("Graph at first:", JSON.stringify(x(i))), F(i), e.warn("Graph after XAX:", JSON.stringify(x(i)));
	let s = r();
	await z(a, i, t.type, t.diagramId, void 0, s);
}, "render");
//#endregion
export { B as render };
