import { __toESM as e } from "../../../../../_virtual/_rolldown/runtime.js";
import { __name as t, log as n } from "./chunk-AGHRB4JF.js";
import r from "../../../../d3-selection/src/select.js";
import "../../../../d3/src/index.js";
import { cytoscape as i } from "../../../../cytoscape/dist/cytoscape.esm.js";
import { require_cytoscape_cose_bilkent as a } from "../../../../cytoscape-cose-bilkent/cytoscape-cose-bilkent.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/cose-bilkent-S5V4N54A.mjs
var o = /* @__PURE__ */ e(a(), 1);
i.use(o.default);
function s(e, t) {
	e.forEach((e) => {
		let n = {
			id: e.id,
			labelText: e.label,
			height: e.height,
			width: e.width,
			padding: e.padding ?? 0
		};
		Object.keys(e).forEach((t) => {
			[
				"id",
				"label",
				"height",
				"width",
				"padding",
				"x",
				"y"
			].includes(t) || (n[t] = e[t]);
		}), t.add({
			group: "nodes",
			data: n,
			position: {
				x: e.x ?? 0,
				y: e.y ?? 0
			}
		});
	});
}
t(s, "addNodes");
function c(e, t) {
	e.forEach((e) => {
		let n = {
			id: e.id,
			source: e.start,
			target: e.end
		};
		Object.keys(e).forEach((t) => {
			[
				"id",
				"start",
				"end"
			].includes(t) || (n[t] = e[t]);
		}), t.add({
			group: "edges",
			data: n
		});
	});
}
t(c, "addEdges");
function l(e) {
	return new Promise((t) => {
		let a = r("body").append("div").attr("id", "cy").attr("style", "display:none"), o = i({
			container: document.getElementById("cy"),
			style: [{
				selector: "edge",
				style: { "curve-style": "bezier" }
			}]
		});
		a.remove(), s(e.nodes, o), c(e.edges, o), o.nodes().forEach(function(e) {
			e.layoutDimensions = () => {
				let t = e.data();
				return {
					w: t.width,
					h: t.height
				};
			};
		}), o.layout({
			name: "cose-bilkent",
			quality: "proof",
			styleEnabled: !1,
			animate: !1
		}).run(), o.ready((e) => {
			n.info("Cytoscape ready", e), t(o);
		});
	});
}
t(l, "createCytoscapeInstance");
function u(e) {
	return e.nodes().map((e) => {
		let t = e.data(), n = e.position(), r = {
			id: t.id,
			x: n.x,
			y: n.y
		};
		return Object.keys(t).forEach((e) => {
			e !== "id" && (r[e] = t[e]);
		}), r;
	});
}
t(u, "extractPositionedNodes");
function d(e) {
	return e.edges().map((e) => {
		let t = e.data(), n = e._private.rscratch, r = {
			id: t.id,
			source: t.source,
			target: t.target,
			startX: n.startX,
			startY: n.startY,
			midX: n.midX,
			midY: n.midY,
			endX: n.endX,
			endY: n.endY
		};
		return Object.keys(t).forEach((e) => {
			[
				"id",
				"source",
				"target"
			].includes(e) || (r[e] = t[e]);
		}), r;
	});
}
t(d, "extractPositionedEdges");
async function f(e, t) {
	n.debug("Starting cose-bilkent layout algorithm");
	try {
		p(e);
		let t = await l(e), r = u(t), i = d(t);
		return n.debug(`Layout completed: ${r.length} nodes, ${i.length} edges`), {
			nodes: r,
			edges: i
		};
	} catch (e) {
		throw n.error("Error in cose-bilkent layout algorithm:", e), e;
	}
}
t(f, "executeCoseBilkentLayout");
function p(e) {
	if (!e) throw Error("Layout data is required");
	if (!e.config) throw Error("Configuration is required in layout data");
	if (!e.rootNode) throw Error("Root node is required");
	if (!e.nodes || !Array.isArray(e.nodes)) throw Error("No nodes found in layout data");
	if (!Array.isArray(e.edges)) throw Error("Edges array is required in layout data");
	return !0;
}
t(p, "validateLayoutData");
var m = /* @__PURE__ */ t(async (e, t, { insertCluster: n, insertEdge: r, insertEdgeLabel: i, insertMarkers: a, insertNode: o, log: s, positionEdgeLabel: c }, { algorithm: l }) => {
	let u = {}, d = {}, p = t.select("g");
	a(p, e.markers, e.type, e.diagramId);
	let m = p.insert("g").attr("class", "subgraphs"), h = p.insert("g").attr("class", "edgePaths"), g = p.insert("g").attr("class", "edgeLabels"), _ = p.insert("g").attr("class", "nodes");
	s.debug("Inserting nodes into DOM for dimension calculation"), await Promise.all(e.nodes.map(async (t) => {
		if (t.isGroup) {
			let e = { ...t };
			d[t.id] = e, u[t.id] = e, await n(m, t);
		} else {
			let n = { ...t };
			u[t.id] = n;
			let r = await o(_, t, {
				config: e.config,
				dir: e.direction || "TB"
			}), i = r.node().getBBox();
			n.width = i.width, n.height = i.height, n.domId = r, s.debug(`Node ${t.id} dimensions: ${i.width}x${i.height}`);
		}
	})), s.debug("Running cose-bilkent layout algorithm");
	let v = await f({
		...e,
		nodes: e.nodes.map((e) => {
			let t = u[e.id];
			return {
				...e,
				width: t.width,
				height: t.height
			};
		})
	}, e.config);
	s.debug("Positioning nodes based on layout results"), v.nodes.forEach((e) => {
		let t = u[e.id];
		t?.domId && (t.domId.attr("transform", `translate(${e.x}, ${e.y})`), t.x = e.x, t.y = e.y, s.debug(`Positioned node ${t.id} at center (${e.x}, ${e.y})`));
	}), v.edges.forEach((t) => {
		let n = e.edges.find((e) => e.id === t.id);
		n && (n.points = [
			{
				x: t.startX,
				y: t.startY
			},
			{
				x: t.midX,
				y: t.midY
			},
			{
				x: t.endX,
				y: t.endY
			}
		]);
	}), s.debug("Inserting and positioning edges"), await Promise.all(e.edges.map(async (t) => {
		await i(g, t);
		let n = u[t.start ?? ""], a = u[t.end ?? ""];
		if (n && a) {
			let i = v.edges.find((e) => e.id === t.id);
			if (i) {
				s.debug("APA01 positionedEdge", i);
				let o = { ...t };
				c(o, r(h, o, d, e.type, n, a, e.diagramId));
			} else {
				let i = {
					...t,
					points: [{
						x: n.x || 0,
						y: n.y || 0
					}, {
						x: a.x || 0,
						y: a.y || 0
					}]
				};
				c(i, r(h, i, d, e.type, n, a, e.diagramId));
			}
		}
	})), s.debug("Cose-bilkent rendering completed");
}, "render");
//#endregion
export { m as render };
