import { __toESM as e } from "../../../../../_virtual/_rolldown/runtime.js";
import { __name as t, log as n } from "./chunk-AGHRB4JF.js";
import { clear as r, defaultConfig_default as i, getAccDescription as a, getAccTitle as o, getConfig as s, getConfig2 as c, getDiagramTitle as l, sanitizeText as u, setAccDescription as d, setAccTitle as f, setDiagramTitle as p, setupGraphViewbox as m } from "./chunk-ICPOFSXX.js";
import h from "../../../../d3-selection/src/select.js";
import "../../../../d3/src/index.js";
import { selectSvgElement as g } from "./chunk-426QAEUC.js";
import { cleanAndMerge as _, getEdgeId as v } from "./chunk-5PVQY5BW.js";
import { createText as y, getIconSVG as b, registerIconPacks as x, unknownIcon as S } from "./chunk-U2HBQHQK.js";
import { populateCommonDb as C } from "./chunk-4BX2VUAB.js";
import { parse as w } from "../../../../@mermaid-js/parser/dist/mermaid-parser.core.js";
import { cytoscape as T } from "../../../../cytoscape/dist/cytoscape.esm.js";
import { require_cytoscape_fcose as E } from "../../../../cytoscape-fcose/cytoscape-fcose.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/architectureDiagram-Q4EWVU46.mjs
var ee = /* @__PURE__ */ e(E(), 1), D = {
	L: "left",
	R: "right",
	T: "top",
	B: "bottom"
}, O = {
	L: /* @__PURE__ */ t((e) => `${e},${e / 2} 0,${e} 0,0`, "L"),
	R: /* @__PURE__ */ t((e) => `0,${e / 2} ${e},0 ${e},${e}`, "R"),
	T: /* @__PURE__ */ t((e) => `0,0 ${e},0 ${e / 2},${e}`, "T"),
	B: /* @__PURE__ */ t((e) => `${e / 2},0 ${e},${e} 0,${e}`, "B")
}, k = {
	L: /* @__PURE__ */ t((e, t) => e - t + 2, "L"),
	R: /* @__PURE__ */ t((e, t) => e - 2, "R"),
	T: /* @__PURE__ */ t((e, t) => e - t + 2, "T"),
	B: /* @__PURE__ */ t((e, t) => e - 2, "B")
}, te = /* @__PURE__ */ t(function(e) {
	return j(e) ? e === "L" ? "R" : "L" : e === "T" ? "B" : "T";
}, "getOppositeArchitectureDirection"), A = /* @__PURE__ */ t(function(e) {
	let t = e;
	return t === "L" || t === "R" || t === "T" || t === "B";
}, "isArchitectureDirection"), j = /* @__PURE__ */ t(function(e) {
	let t = e;
	return t === "L" || t === "R";
}, "isArchitectureDirectionX"), M = /* @__PURE__ */ t(function(e) {
	let t = e;
	return t === "T" || t === "B";
}, "isArchitectureDirectionY"), N = /* @__PURE__ */ t(function(e, t) {
	let n = j(e) && M(t), r = M(e) && j(t);
	return n || r;
}, "isArchitectureDirectionXY"), P = /* @__PURE__ */ t(function(e) {
	let t = e[0], n = e[1], r = j(t) && M(n), i = M(t) && j(n);
	return r || i;
}, "isArchitecturePairXY"), F = /* @__PURE__ */ t(function(e) {
	return e !== "LL" && e !== "RR" && e !== "TT" && e !== "BB";
}, "isValidArchitectureDirectionPair"), I = /* @__PURE__ */ t(function(e, t) {
	let n = `${e}${t}`;
	return F(n) ? n : void 0;
}, "getArchitectureDirectionPair"), L = /* @__PURE__ */ t(function([e, t], n) {
	let r = n[0], i = n[1];
	return j(r) ? M(i) ? [e + (r === "L" ? -1 : 1), t + (i === "T" ? 1 : -1)] : [e + (r === "L" ? -1 : 1), t] : j(i) ? [e + (i === "L" ? 1 : -1), t + (r === "T" ? 1 : -1)] : [e, t + (r === "T" ? 1 : -1)];
}, "shiftPositionByArchitectureDirectionPair"), R = /* @__PURE__ */ t(function(e) {
	return e === "LT" || e === "TL" ? [1, 1] : e === "BL" || e === "LB" ? [1, -1] : e === "BR" || e === "RB" ? [-1, -1] : [-1, 1];
}, "getArchitectureDirectionXYFactors"), z = /* @__PURE__ */ t(function(e, t) {
	return N(e, t) ? "bend" : j(e) ? "horizontal" : "vertical";
}, "getArchitectureDirectionAlignment"), ne = /* @__PURE__ */ t(function(e) {
	return e.type === "service";
}, "isArchitectureService"), re = /* @__PURE__ */ t(function(e) {
	return e.type === "junction";
}, "isArchitectureJunction"), B = /* @__PURE__ */ t((e) => e.data(), "edgeData"), V = /* @__PURE__ */ t((e) => e.data(), "nodeData"), ie = i.architecture, H = class {
	constructor() {
		this.nodes = {}, this.groups = {}, this.edges = [], this.registeredIds = {}, this.elements = {}, this.diagramId = "", this.setAccTitle = f, this.getAccTitle = o, this.setDiagramTitle = p, this.getDiagramTitle = l, this.getAccDescription = a, this.setAccDescription = d, this.clear();
	}
	static {
		t(this, "ArchitectureDB");
	}
	setDiagramId(e) {
		this.diagramId = e;
	}
	getDiagramId() {
		return this.diagramId;
	}
	clear() {
		this.nodes = {}, this.groups = {}, this.edges = [], this.registeredIds = {}, this.dataStructures = void 0, this.elements = {}, this.diagramId = "", r();
	}
	addService({ id: e, icon: t, in: n, title: r, iconText: i }) {
		if (this.registeredIds[e] !== void 0) throw Error(`The service id [${e}] is already in use by another ${this.registeredIds[e]}`);
		if (n !== void 0) {
			if (e === n) throw Error(`The service [${e}] cannot be placed within itself`);
			if (this.registeredIds[n] === void 0) throw Error(`The service [${e}]'s parent does not exist. Please make sure the parent is created before this service`);
			if (this.registeredIds[n] === "node") throw Error(`The service [${e}]'s parent is not a group`);
		}
		this.registeredIds[e] = "node", this.nodes[e] = {
			id: e,
			type: "service",
			icon: t,
			iconText: i,
			title: r,
			edges: [],
			in: n
		};
	}
	getServices() {
		return Object.values(this.nodes).filter(ne);
	}
	addJunction({ id: e, in: t }) {
		if (this.registeredIds[e] !== void 0) throw Error(`The junction id [${e}] is already in use by another ${this.registeredIds[e]}`);
		if (t !== void 0) {
			if (e === t) throw Error(`The junction [${e}] cannot be placed within itself`);
			if (this.registeredIds[t] === void 0) throw Error(`The junction [${e}]'s parent does not exist. Please make sure the parent is created before this junction`);
			if (this.registeredIds[t] === "node") throw Error(`The junction [${e}]'s parent is not a group`);
		}
		this.registeredIds[e] = "node", this.nodes[e] = {
			id: e,
			type: "junction",
			edges: [],
			in: t
		};
	}
	getJunctions() {
		return Object.values(this.nodes).filter(re);
	}
	getNodes() {
		return Object.values(this.nodes);
	}
	getNode(e) {
		return this.nodes[e] ?? null;
	}
	addGroup({ id: e, icon: t, in: n, title: r }) {
		if (this.registeredIds?.[e] !== void 0) throw Error(`The group id [${e}] is already in use by another ${this.registeredIds[e]}`);
		if (n !== void 0) {
			if (e === n) throw Error(`The group [${e}] cannot be placed within itself`);
			if (this.registeredIds?.[n] === void 0) throw Error(`The group [${e}]'s parent does not exist. Please make sure the parent is created before this group`);
			if (this.registeredIds?.[n] === "node") throw Error(`The group [${e}]'s parent is not a group`);
		}
		this.registeredIds[e] = "group", this.groups[e] = {
			id: e,
			icon: t,
			title: r,
			in: n
		};
	}
	getGroups() {
		return Object.values(this.groups);
	}
	addEdge({ lhsId: e, rhsId: t, lhsDir: n, rhsDir: r, lhsInto: i, rhsInto: a, lhsGroup: o, rhsGroup: s, title: c }) {
		if (!A(n)) throw Error(`Invalid direction given for left hand side of edge ${e}--${t}. Expected (L,R,T,B) got ${String(n)}`);
		if (!A(r)) throw Error(`Invalid direction given for right hand side of edge ${e}--${t}. Expected (L,R,T,B) got ${String(r)}`);
		if (this.nodes[e] === void 0 && this.groups[e] === void 0) throw Error(`The left-hand id [${e}] does not yet exist. Please create the service/group before declaring an edge to it.`);
		if (this.nodes[t] === void 0 && this.groups[t] === void 0) throw Error(`The right-hand id [${t}] does not yet exist. Please create the service/group before declaring an edge to it.`);
		let l = this.nodes[e].in, u = this.nodes[t].in;
		if (o && l && u && l == u) throw Error(`The left-hand id [${e}] is modified to traverse the group boundary, but the edge does not pass through two groups.`);
		if (s && l && u && l == u) throw Error(`The right-hand id [${t}] is modified to traverse the group boundary, but the edge does not pass through two groups.`);
		let d = {
			lhsId: e,
			lhsDir: n,
			lhsInto: i,
			lhsGroup: o,
			rhsId: t,
			rhsDir: r,
			rhsInto: a,
			rhsGroup: s,
			title: c
		};
		this.edges.push(d), this.nodes[e] && this.nodes[t] && (this.nodes[e].edges.push(this.edges[this.edges.length - 1]), this.nodes[t].edges.push(this.edges[this.edges.length - 1]));
	}
	getEdges() {
		return this.edges;
	}
	getDataStructures() {
		if (this.dataStructures === void 0) {
			let e = {}, n = Object.entries(this.nodes).reduce((t, [n, r]) => (t[n] = r.edges.reduce((t, r) => {
				let i = this.getNode(r.lhsId)?.in, a = this.getNode(r.rhsId)?.in;
				if (i && a && i !== a) {
					let t = z(r.lhsDir, r.rhsDir);
					t !== "bend" && (e[i] ??= {}, e[i][a] = t, e[a] ??= {}, e[a][i] = t);
				}
				if (r.lhsId === n) {
					let e = I(r.lhsDir, r.rhsDir);
					e && (t[e] = r.rhsId);
				} else {
					let e = I(r.rhsDir, r.lhsDir);
					e && (t[e] = r.lhsId);
				}
				return t;
			}, {}), t), {}), r = Object.keys(n)[0], i = { [r]: 1 }, a = Object.keys(n).reduce((e, t) => t === r ? e : {
				...e,
				[t]: 1
			}, {}), o = /* @__PURE__ */ t((e) => {
				let t = { [e]: [0, 0] }, r = [e];
				for (; r.length > 0;) {
					let e = r.shift();
					if (e) {
						i[e] = 1, delete a[e];
						let o = n[e], [s, c] = t[e];
						Object.entries(o).forEach(([e, n]) => {
							i[n] || (t[n] = L([s, c], e), r.push(n));
						});
					}
				}
				return t;
			}, "BFS"), s = [o(r)];
			for (; Object.keys(a).length > 0;) s.push(o(Object.keys(a)[0]));
			this.dataStructures = {
				adjList: n,
				spatialMaps: s,
				groupAlignments: e
			};
		}
		return this.dataStructures;
	}
	setElementForId(e, t) {
		this.elements[e] = t;
	}
	getElementById(e) {
		return this.elements[e];
	}
	getConfig() {
		return _({
			...ie,
			...s().architecture
		});
	}
	getConfigField(e) {
		return this.getConfig()[e];
	}
}, ae = /* @__PURE__ */ t((e, t) => {
	C(e, t), e.groups.map((e) => t.addGroup(e)), e.services.map((e) => t.addService({
		...e,
		type: "service"
	})), e.junctions.map((e) => t.addJunction({
		...e,
		type: "junction"
	})), e.edges.map((e) => t.addEdge(e));
}, "populateDb"), U = {
	parser: { yy: void 0 },
	parse: /* @__PURE__ */ t(async (e) => {
		let t = await w("architecture", e);
		n.debug(t);
		let r = U.parser?.yy;
		if (!(r instanceof H)) throw Error("parser.parser?.yy was not a ArchitectureDB. This is due to a bug within Mermaid, please report this issue at https://github.com/mermaid-js/mermaid/issues.");
		ae(t, r);
	}, "parse")
}, oe = /* @__PURE__ */ t((e) => `
  .edge {
    stroke-width: ${e.archEdgeWidth};
    stroke: ${e.archEdgeColor};
    fill: none;
  }

  .arrow {
    fill: ${e.archEdgeArrowColor};
  }

  .node-bkg {
    fill: none;
    stroke: ${e.archGroupBorderColor};
    stroke-width: ${e.archGroupBorderWidth};
    stroke-dasharray: 8;
  }
  .node-icon-text {
    display: flex; 
    align-items: center;
  }
  
  .node-icon-text > div {
    color: #fff;
    margin: 1px;
    height: fit-content;
    text-align: center;
    overflow: hidden;
    display: -webkit-box;
    -webkit-box-orient: vertical;
  }
`, "getStyles"), W = /* @__PURE__ */ t((e) => `<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/>${e}</g>`, "wrapIcon"), G = {
	prefix: "mermaid-architecture",
	height: 80,
	width: 80,
	icons: {
		database: { body: W("<path id=\"b\" data-name=\"4\" d=\"m20,57.86c0,3.94,8.95,7.14,20,7.14s20-3.2,20-7.14\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><path id=\"c\" data-name=\"3\" d=\"m20,45.95c0,3.94,8.95,7.14,20,7.14s20-3.2,20-7.14\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><path id=\"d\" data-name=\"2\" d=\"m20,34.05c0,3.94,8.95,7.14,20,7.14s20-3.2,20-7.14\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse id=\"e\" data-name=\"1\" cx=\"40\" cy=\"22.14\" rx=\"20\" ry=\"7.14\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"20\" y1=\"57.86\" x2=\"20\" y2=\"22.14\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"60\" y1=\"57.86\" x2=\"60\" y2=\"22.14\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/>") },
		server: { body: W("<rect x=\"17.5\" y=\"17.5\" width=\"45\" height=\"45\" rx=\"2\" ry=\"2\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"17.5\" y1=\"32.5\" x2=\"62.5\" y2=\"32.5\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"17.5\" y1=\"47.5\" x2=\"62.5\" y2=\"47.5\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><g><path d=\"m56.25,25c0,.27-.45.5-1,.5h-10.5c-.55,0-1-.23-1-.5s.45-.5,1-.5h10.5c.55,0,1,.23,1,.5Z\" style=\"fill: #fff; stroke-width: 0px;\"/><path d=\"m56.25,25c0,.27-.45.5-1,.5h-10.5c-.55,0-1-.23-1-.5s.45-.5,1-.5h10.5c.55,0,1,.23,1,.5Z\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10;\"/></g><g><path d=\"m56.25,40c0,.27-.45.5-1,.5h-10.5c-.55,0-1-.23-1-.5s.45-.5,1-.5h10.5c.55,0,1,.23,1,.5Z\" style=\"fill: #fff; stroke-width: 0px;\"/><path d=\"m56.25,40c0,.27-.45.5-1,.5h-10.5c-.55,0-1-.23-1-.5s.45-.5,1-.5h10.5c.55,0,1,.23,1,.5Z\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10;\"/></g><g><path d=\"m56.25,55c0,.27-.45.5-1,.5h-10.5c-.55,0-1-.23-1-.5s.45-.5,1-.5h10.5c.55,0,1,.23,1,.5Z\" style=\"fill: #fff; stroke-width: 0px;\"/><path d=\"m56.25,55c0,.27-.45.5-1,.5h-10.5c-.55,0-1-.23-1-.5s.45-.5,1-.5h10.5c.55,0,1,.23,1,.5Z\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10;\"/></g><g><circle cx=\"32.5\" cy=\"25\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/><circle cx=\"27.5\" cy=\"25\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/><circle cx=\"22.5\" cy=\"25\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/></g><g><circle cx=\"32.5\" cy=\"40\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/><circle cx=\"27.5\" cy=\"40\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/><circle cx=\"22.5\" cy=\"40\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/></g><g><circle cx=\"32.5\" cy=\"55\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/><circle cx=\"27.5\" cy=\"55\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/><circle cx=\"22.5\" cy=\"55\" r=\".75\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10;\"/></g>") },
		disk: { body: W("<rect x=\"20\" y=\"15\" width=\"40\" height=\"50\" rx=\"1\" ry=\"1\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse cx=\"24\" cy=\"19.17\" rx=\".8\" ry=\".83\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse cx=\"56\" cy=\"19.17\" rx=\".8\" ry=\".83\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse cx=\"24\" cy=\"60.83\" rx=\".8\" ry=\".83\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse cx=\"56\" cy=\"60.83\" rx=\".8\" ry=\".83\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse cx=\"40\" cy=\"33.75\" rx=\"14\" ry=\"14.58\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><ellipse cx=\"40\" cy=\"33.75\" rx=\"4\" ry=\"4.17\" style=\"fill: #fff; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><path d=\"m37.51,42.52l-4.83,13.22c-.26.71-1.1,1.02-1.76.64l-4.18-2.42c-.66-.38-.81-1.26-.33-1.84l9.01-10.8c.88-1.05,2.56-.08,2.09,1.2Z\" style=\"fill: #fff; stroke-width: 0px;\"/>") },
		internet: { body: W("<circle cx=\"40\" cy=\"40\" r=\"22.5\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"40\" y1=\"17.5\" x2=\"40\" y2=\"62.5\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"17.5\" y1=\"40\" x2=\"62.5\" y2=\"40\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><path d=\"m39.99,17.51c-15.28,11.1-15.28,33.88,0,44.98\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><path d=\"m40.01,17.51c15.28,11.1,15.28,33.88,0,44.98\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"19.75\" y1=\"30.1\" x2=\"60.25\" y2=\"30.1\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/><line x1=\"19.75\" y1=\"49.9\" x2=\"60.25\" y2=\"49.9\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/>") },
		cloud: { body: W("<path d=\"m65,47.5c0,2.76-2.24,5-5,5H20c-2.76,0-5-2.24-5-5,0-1.87,1.03-3.51,2.56-4.36-.04-.21-.06-.42-.06-.64,0-2.6,2.48-4.74,5.65-4.97,1.65-4.51,6.34-7.76,11.85-7.76.86,0,1.69.08,2.5.23,2.09-1.57,4.69-2.5,7.5-2.5,6.1,0,11.19,4.38,12.28,10.17,2.14.56,3.72,2.51,3.72,4.83,0,.03,0,.07-.01.1,2.29.46,4.01,2.48,4.01,4.9Z\" style=\"fill: none; stroke: #fff; stroke-miterlimit: 10; stroke-width: 2px;\"/>") },
		unknown: S,
		blank: { body: W("") }
	}
}, se = /* @__PURE__ */ t(async function(e, t, n, r) {
	let i = n.getConfigField("padding"), a = n.getConfigField("iconSize"), o = a / 2, s = a / 6, l = s / 2;
	await Promise.all(t.edges().map(async (t) => {
		let { source: a, sourceDir: u, sourceArrow: d, sourceGroup: f, target: p, targetDir: m, targetArrow: h, targetGroup: g, label: _ } = B(t), { x: b, y: x } = t[0].sourceEndpoint(), { x: S, y: C } = t[0].midpoint(), { x: w, y: T } = t[0].targetEndpoint(), E = i + 4;
		if (f && (j(u) ? b += u === "L" ? -E : E : x += u === "T" ? -E : E + 18), g && (j(m) ? w += m === "L" ? -E : E : T += m === "T" ? -E : E + 18), !f && n.getNode(a)?.type === "junction" && (j(u) ? b += u === "L" ? o : -o : x += u === "T" ? o : -o), !g && n.getNode(p)?.type === "junction" && (j(m) ? w += m === "L" ? o : -o : T += m === "T" ? o : -o), t[0]._private.rscratch) {
			let t = e.insert("g");
			if (t.insert("path").attr("d", `M ${b},${x} L ${S},${C} L${w},${T} `).attr("class", "edge").attr("id", `${r}-${v(a, p, { prefix: "L" })}`), d) {
				let e = j(u) ? k[u](b, s) : b - l, n = M(u) ? k[u](x, s) : x - l;
				t.insert("polygon").attr("points", O[u](s)).attr("transform", `translate(${e},${n})`).attr("class", "arrow");
			}
			if (h) {
				let e = j(m) ? k[m](w, s) : w - l, n = M(m) ? k[m](T, s) : T - l;
				t.insert("polygon").attr("points", O[m](s)).attr("transform", `translate(${e},${n})`).attr("class", "arrow");
			}
			if (_) {
				let e = N(u, m) ? "XY" : j(u) ? "X" : "Y", n = 0;
				n = e === "X" ? Math.abs(b - w) : e === "Y" ? Math.abs(x - T) / 1.5 : Math.abs(b - w) / 2;
				let r = t.append("g");
				if (await y(r, _, {
					useHtmlLabels: !1,
					width: n,
					classes: "architecture-service-label"
				}, c()), r.attr("dy", "1em").attr("alignment-baseline", "middle").attr("dominant-baseline", "middle").attr("text-anchor", "middle"), e === "X") r.attr("transform", "translate(" + S + ", " + C + ")");
				else if (e === "Y") r.attr("transform", "translate(" + S + ", " + C + ") rotate(-90)");
				else if (e === "XY") {
					let e = I(u, m);
					if (e && P(e)) {
						let t = r.node().getBoundingClientRect(), [n, i] = R(e);
						r.attr("dominant-baseline", "auto").attr("transform", `rotate(${-1 * n * i * 45})`);
						let a = r.node().getBoundingClientRect();
						r.attr("transform", `
                translate(${S}, ${C - t.height / 2})
                translate(${n * a.width / 2}, ${i * a.height / 2})
                rotate(${-1 * n * i * 45}, 0, ${t.height / 2})
              `);
					}
				}
			}
		}
	}));
}, "drawEdges"), ce = /* @__PURE__ */ t(async function(e, t, n, r) {
	let i = n.getConfigField("padding") * .75, a = n.getConfigField("fontSize"), o = n.getConfigField("iconSize") / 2;
	await Promise.all(t.nodes().map(async (t) => {
		let s = V(t);
		if (s.type === "group") {
			let { h: l, w: u, x1: d, y1: f } = t.boundingBox(), p = e.append("rect");
			p.attr("id", `${r}-group-${s.id}`).attr("x", d + o).attr("y", f + o).attr("width", u).attr("height", l).attr("class", "node-bkg");
			let m = e.append("g"), h = d, g = f;
			if (s.icon) {
				let e = m.append("g");
				e.html(`<g>${await b(s.icon, {
					height: i,
					width: i,
					fallbackPrefix: G.prefix
				})}</g>`), e.attr("transform", "translate(" + (h + o + 1) + ", " + (g + o + 1) + ")"), h += i, g += a / 2 - 1 - 2;
			}
			if (s.label) {
				let e = m.append("g");
				await y(e, s.label, {
					useHtmlLabels: !1,
					width: u,
					classes: "architecture-service-label"
				}, c()), e.attr("dy", "1em").attr("alignment-baseline", "middle").attr("dominant-baseline", "start").attr("text-anchor", "start"), e.attr("transform", "translate(" + (h + o + 4) + ", " + (g + o + 2) + ")");
			}
			n.setElementForId(s.id, p);
		}
	}));
}, "drawGroups"), le = /* @__PURE__ */ t(async function(e, t, n, r) {
	let i = c();
	for (let a of n) {
		let n = t.append("g"), o = e.getConfigField("iconSize");
		if (a.title) {
			let e = n.append("g");
			await y(e, a.title, {
				useHtmlLabels: !1,
				width: o * 1.5,
				classes: "architecture-service-label"
			}, i), e.attr("dy", "1em").attr("alignment-baseline", "middle").attr("dominant-baseline", "middle").attr("text-anchor", "middle"), e.attr("transform", "translate(" + o / 2 + ", " + o + ")");
		}
		let s = n.append("g");
		if (a.icon) s.html(`<g>${await b(a.icon, {
			height: o,
			width: o,
			fallbackPrefix: G.prefix
		})}</g>`);
		else if (a.iconText) {
			s.html(`<g>${await b("blank", {
				height: o,
				width: o,
				fallbackPrefix: G.prefix
			})}</g>`);
			let e = s.append("g").append("foreignObject").attr("width", o).attr("height", o).append("div").attr("class", "node-icon-text").attr("style", `height: ${o}px;`).append("div").html(u(a.iconText, i)), t = parseInt(window.getComputedStyle(e.node(), null).getPropertyValue("font-size").replace(/\D/g, "")) ?? 16;
			e.attr("style", `-webkit-line-clamp: ${Math.floor((o - 2) / t)};`);
		} else s.append("path").attr("class", "node-bkg").attr("id", `${r}-node-${a.id}`).attr("d", `M0,${o} V5 Q0,0 5,0 H${o - 5} Q${o},0 ${o},5 V${o} Z`);
		n.attr("id", `${r}-service-${a.id}`).attr("class", "architecture-service");
		let { width: c, height: l } = n.node().getBBox();
		a.width = c, a.height = l, e.setElementForId(a.id, n);
	}
	return 0;
}, "drawServices"), ue = /* @__PURE__ */ t(function(e, t, n, r) {
	n.forEach((n) => {
		let i = t.append("g"), a = e.getConfigField("iconSize");
		i.append("g").append("rect").attr("id", `${r}-node-${n.id}`).attr("fill-opacity", "0").attr("width", a).attr("height", a), i.attr("class", "architecture-junction");
		let { width: o, height: s } = i._groups[0][0].getBBox();
		i.width = o, i.height = s, e.setElementForId(n.id, i);
	});
}, "drawJunctions");
x([{
	name: G.prefix,
	icons: G
}]), T.use(ee.default);
function K(e, t, n) {
	e.forEach((e) => {
		t.add({
			group: "nodes",
			data: {
				type: "service",
				id: e.id,
				icon: e.icon,
				label: e.title,
				parent: e.in,
				width: n.getConfigField("iconSize"),
				height: n.getConfigField("iconSize")
			},
			classes: "node-service"
		});
	});
}
t(K, "addServices");
function q(e, t, n) {
	e.forEach((e) => {
		t.add({
			group: "nodes",
			data: {
				type: "junction",
				id: e.id,
				parent: e.in,
				width: n.getConfigField("iconSize"),
				height: n.getConfigField("iconSize")
			},
			classes: "node-junction"
		});
	});
}
t(q, "addJunctions");
function J(e, t) {
	t.nodes().map((t) => {
		let n = V(t);
		n.type !== "group" && (n.x = t.position().x, n.y = t.position().y, e.getElementById(n.id).attr("transform", "translate(" + (n.x || 0) + "," + (n.y || 0) + ")"));
	});
}
t(J, "positionNodes");
function Y(e, t) {
	e.forEach((e) => {
		t.add({
			group: "nodes",
			data: {
				type: "group",
				id: e.id,
				icon: e.icon,
				label: e.title,
				parent: e.in
			},
			classes: "node-group"
		});
	});
}
t(Y, "addGroups");
function X(e, t) {
	e.forEach((e) => {
		let { lhsId: n, rhsId: r, lhsInto: i, lhsGroup: a, rhsInto: o, lhsDir: s, rhsDir: c, rhsGroup: l, title: u } = e, d = N(e.lhsDir, e.rhsDir) ? "segments" : "straight", f = {
			id: `${n}-${r}`,
			label: u,
			source: n,
			sourceDir: s,
			sourceArrow: i,
			sourceGroup: a,
			sourceEndpoint: s === "L" ? "0 50%" : s === "R" ? "100% 50%" : s === "T" ? "50% 0" : "50% 100%",
			target: r,
			targetDir: c,
			targetArrow: o,
			targetGroup: l,
			targetEndpoint: c === "L" ? "0 50%" : c === "R" ? "100% 50%" : c === "T" ? "50% 0" : "50% 100%"
		};
		t.add({
			group: "edges",
			data: f,
			classes: d
		});
	});
}
t(X, "addEdges");
function Z(e, n, r) {
	let i = /* @__PURE__ */ t((e, t) => Object.entries(e).reduce((e, [n, i]) => {
		let a = 0, o = Object.entries(i);
		if (o.length === 1) return e[n] = o[0][1], e;
		for (let i = 0; i < o.length - 1; i++) for (let s = i + 1; s < o.length; s++) {
			let [c, l] = o[i], [u, d] = o[s];
			if (r[c]?.[u] === t) e[n] ??= [], e[n] = [
				...e[n],
				...l,
				...d
			];
			else if (c === "default" || u === "default") e[n] ??= [], e[n] = [
				...e[n],
				...l,
				...d
			];
			else {
				let t = `${n}-${a++}`;
				e[t] = l;
				let r = `${n}-${a++}`;
				e[r] = d;
			}
		}
		return e;
	}, {}), "flattenAlignments"), [a, o] = n.map((t) => {
		let n = {}, r = {};
		return Object.entries(t).forEach(([t, [i, a]]) => {
			let o = e.getNode(t)?.in ?? "default";
			n[a] ??= {}, n[a][o] ??= [], n[a][o].push(t), r[i] ??= {}, r[i][o] ??= [], r[i][o].push(t);
		}), {
			horiz: Object.values(i(n, "horizontal")).filter((e) => e.length > 1),
			vert: Object.values(i(r, "vertical")).filter((e) => e.length > 1)
		};
	}).reduce(([e, t], { horiz: n, vert: r }) => [[...e, ...n], [...t, ...r]], [[], []]);
	return {
		horizontal: a,
		vertical: o
	};
}
t(Z, "getAlignments");
function Q(e, n) {
	let r = [], i = /* @__PURE__ */ t((e) => `${e[0]},${e[1]}`, "posToStr"), a = /* @__PURE__ */ t((e) => e.split(",").map((e) => parseInt(e)), "strToPos");
	return e.forEach((e) => {
		let t = Object.fromEntries(Object.entries(e).map(([e, t]) => [i(t), e])), o = [i([0, 0])], s = {}, c = {
			L: [-1, 0],
			R: [1, 0],
			T: [0, 1],
			B: [0, -1]
		};
		for (; o.length > 0;) {
			let e = o.shift();
			if (e) {
				s[e] = 1;
				let l = t[e];
				if (l) {
					let u = a(e);
					Object.entries(c).forEach(([e, a]) => {
						let c = i([u[0] + a[0], u[1] + a[1]]), d = t[c];
						d && !s[c] && (o.push(c), r.push({
							[D[e]]: d,
							[D[te(e)]]: l,
							gap: 1.5 * n.getConfigField("iconSize")
						}));
					});
				}
			}
		}
	}), r;
}
t(Q, "getRelativeConstraints");
function $(e, r, i, a, o, { spatialMaps: s, groupAlignments: c }) {
	return new Promise((l) => {
		let u = h("body").append("div").attr("id", "cy").attr("style", "display:none"), d = T({
			container: document.getElementById("cy"),
			style: [
				{
					selector: "edge",
					style: {
						"curve-style": "straight",
						"source-endpoint": "data(sourceEndpoint)",
						"target-endpoint": "data(targetEndpoint)"
					}
				},
				{
					selector: "edge[label]",
					style: { label: "data(label)" }
				},
				{
					selector: "edge.segments",
					style: {
						"curve-style": "segments",
						"segment-weights": "0",
						"segment-distances": [.5],
						"edge-distances": "endpoints",
						"source-endpoint": "data(sourceEndpoint)",
						"target-endpoint": "data(targetEndpoint)"
					}
				},
				{
					selector: "node",
					style: { "compound-sizing-wrt-labels": "include" }
				},
				{
					selector: "node[label]",
					style: {
						"text-valign": "bottom",
						"text-halign": "center",
						"font-size": `${o.getConfigField("fontSize")}px`
					}
				},
				{
					selector: ".node-service",
					style: {
						label: "data(label)",
						width: "data(width)",
						height: "data(height)"
					}
				},
				{
					selector: ".node-junction",
					style: {
						width: "data(width)",
						height: "data(height)"
					}
				},
				{
					selector: ".node-group",
					style: { padding: `${o.getConfigField("padding")}px` }
				}
			],
			layout: {
				name: "grid",
				boundingBox: {
					x1: 0,
					x2: 100,
					y1: 0,
					y2: 100
				}
			}
		});
		u.remove(), Y(i, d), K(e, d, o), q(r, d, o), X(a, d);
		let f = Z(o, s, c), p = Q(s, o), m = d.layout({
			name: "fcose",
			quality: "proof",
			randomize: o.getConfigField("randomize"),
			styleEnabled: !1,
			animate: !1,
			nodeDimensionsIncludeLabels: !1,
			idealEdgeLength(e) {
				let [t, n] = e.connectedNodes(), { parent: r } = V(t), { parent: i } = V(n);
				return r === i ? 1.5 * o.getConfigField("iconSize") : .5 * o.getConfigField("iconSize");
			},
			edgeElasticity(e) {
				let [t, n] = e.connectedNodes(), { parent: r } = V(t), { parent: i } = V(n);
				return r === i ? .45 : .001;
			},
			alignmentConstraint: f,
			relativePlacementConstraint: p
		});
		m.one("layoutstop", () => {
			function e(e, t, n, r) {
				let i, a, { x: o, y: s } = e, { x: c, y: l } = t;
				a = (r - s + (o - n) * (s - l) / (o - c)) / Math.sqrt(1 + ((s - l) / (o - c)) ** 2), i = Math.sqrt((r - s) ** 2 + (n - o) ** 2 - a ** 2);
				let u = Math.sqrt((c - o) ** 2 + (l - s) ** 2);
				i /= u;
				let d = (c - o) * (r - s) - (l - s) * (n - o);
				switch (!0) {
					case d >= 0:
						d = 1;
						break;
					case d < 0:
						d = -1;
						break;
				}
				let f = (c - o) * (n - o) + (l - s) * (r - s);
				switch (!0) {
					case f >= 0:
						f = 1;
						break;
					case f < 0:
						f = -1;
						break;
				}
				return a = Math.abs(a) * d, i *= f, {
					distances: a,
					weights: i
				};
			}
			t(e, "getSegmentWeights"), d.startBatch();
			for (let t of Object.values(d.edges())) if (t.data?.()) {
				let { x: n, y: r } = t.source().position(), { x: i, y: a } = t.target().position();
				if (n !== i && r !== a) {
					let n = t.sourceEndpoint(), r = t.targetEndpoint(), { sourceDir: i } = B(t), [a, o] = M(i) ? [n.x, r.y] : [r.x, n.y], { weights: s, distances: c } = e(n, r, a, o);
					t.style("segment-distances", c), t.style("segment-weights", s);
				}
			}
			d.endBatch(), m.run();
		}), m.run(), d.ready((e) => {
			n.info("Ready", e), l(d);
		});
	});
}
t($, "layoutArchitecture");
var de = {
	parser: U,
	get db() {
		return new H();
	},
	renderer: { draw: /* @__PURE__ */ t(async (e, t, n, r) => {
		let i = r.db;
		i.setDiagramId(t);
		let a = i.getServices(), o = i.getJunctions(), s = i.getGroups(), c = i.getEdges(), l = i.getDataStructures(), u = g(t), d = u.append("g");
		d.attr("class", "architecture-edges");
		let f = u.append("g");
		f.attr("class", "architecture-services");
		let p = u.append("g");
		p.attr("class", "architecture-groups"), await le(i, f, a, t), ue(i, f, o, t);
		let h = await $(a, o, s, c, i, l);
		await se(d, h, i, t), await ce(p, h, i, t), J(i, h), m(void 0, u, i.getConfigField("padding"), i.getConfigField("useMaxWidth"));
	}, "draw") },
	styles: oe
};
//#endregion
export { de as diagram };
