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

//# sourceMappingURL=dagre-BM42HDAG-Q1Y4kpzi.js.map