import { __toESM } from "../../../../../_virtual/rolldown_runtime.js";
import { __name, log } from "./chunk-AGHRB4JF.js";
import { clear, defaultConfig_default, getAccDescription, getAccTitle, getConfig, getConfig2, getDiagramTitle, sanitizeText, setAccDescription, setAccTitle, setDiagramTitle, setupGraphViewbox } from "./chunk-ABZYJK2D.js";
import select_default from "../../../../d3-selection/src/select.js";
import "../../../../d3/src/index.js";
import { selectSvgElement } from "./chunk-EXTU4WIE.js";
import { cleanAndMerge, getEdgeId } from "./chunk-S3R3BYOJ.js";
import { createText, getIconSVG, registerIconPacks, unknownIcon } from "./chunk-JA3XYJ7Z.js";
import { populateCommonDb } from "./chunk-4BX2VUAB.js";
import { parse } from "../../../../@mermaid-js/parser/dist/mermaid-parser.core.js";
import { cytoscape } from "../../../../cytoscape/dist/cytoscape.esm.js";
import { require_cytoscape_fcose } from "../../../../cytoscape-fcose/cytoscape-fcose.js";
var import_cytoscape_fcose = /* @__PURE__ */ __toESM(require_cytoscape_fcose(), 1), ArchitectureDirectionName = {
	L: "left",
	R: "right",
	T: "top",
	B: "bottom"
}, ArchitectureDirectionArrow = {
	L: /* @__PURE__ */ __name((n) => `${n},${n / 2} 0,${n} 0,0`, "L"),
	R: /* @__PURE__ */ __name((n) => `0,${n / 2} ${n},0 ${n},${n}`, "R"),
	T: /* @__PURE__ */ __name((n) => `0,0 ${n},0 ${n / 2},${n}`, "T"),
	B: /* @__PURE__ */ __name((n) => `${n / 2},0 ${n},${n} 0,${n}`, "B")
}, ArchitectureDirectionArrowShift = {
	L: /* @__PURE__ */ __name((n, j) => n - j + 2, "L"),
	R: /* @__PURE__ */ __name((n, j) => n - 2, "R"),
	T: /* @__PURE__ */ __name((n, j) => n - j + 2, "T"),
	B: /* @__PURE__ */ __name((n, j) => n - 2, "B")
}, getOppositeArchitectureDirection = /* @__PURE__ */ __name(function(n) {
	return isArchitectureDirectionX(n) ? n === "L" ? "R" : "L" : n === "T" ? "B" : "T";
}, "getOppositeArchitectureDirection"), isArchitectureDirection = /* @__PURE__ */ __name(function(n) {
	let j = n;
	return j === "L" || j === "R" || j === "T" || j === "B";
}, "isArchitectureDirection"), isArchitectureDirectionX = /* @__PURE__ */ __name(function(n) {
	let j = n;
	return j === "L" || j === "R";
}, "isArchitectureDirectionX"), isArchitectureDirectionY = /* @__PURE__ */ __name(function(n) {
	let j = n;
	return j === "T" || j === "B";
}, "isArchitectureDirectionY"), isArchitectureDirectionXY = /* @__PURE__ */ __name(function(n, j) {
	let M = isArchitectureDirectionX(n) && isArchitectureDirectionY(j), N = isArchitectureDirectionY(n) && isArchitectureDirectionX(j);
	return M || N;
}, "isArchitectureDirectionXY"), isArchitecturePairXY = /* @__PURE__ */ __name(function(n) {
	let j = n[0], M = n[1], N = isArchitectureDirectionX(j) && isArchitectureDirectionY(M), P = isArchitectureDirectionY(j) && isArchitectureDirectionX(M);
	return N || P;
}, "isArchitecturePairXY"), isValidArchitectureDirectionPair = /* @__PURE__ */ __name(function(n) {
	return n !== "LL" && n !== "RR" && n !== "TT" && n !== "BB";
}, "isValidArchitectureDirectionPair"), getArchitectureDirectionPair = /* @__PURE__ */ __name(function(n, j) {
	let M = `${n}${j}`;
	return isValidArchitectureDirectionPair(M) ? M : void 0;
}, "getArchitectureDirectionPair"), shiftPositionByArchitectureDirectionPair = /* @__PURE__ */ __name(function([n, j], M) {
	let N = M[0], P = M[1];
	return isArchitectureDirectionX(N) ? isArchitectureDirectionY(P) ? [n + (N === "L" ? -1 : 1), j + (P === "T" ? 1 : -1)] : [n + (N === "L" ? -1 : 1), j] : isArchitectureDirectionX(P) ? [n + (P === "L" ? 1 : -1), j + (N === "T" ? 1 : -1)] : [n, j + (N === "T" ? 1 : -1)];
}, "shiftPositionByArchitectureDirectionPair"), getArchitectureDirectionXYFactors = /* @__PURE__ */ __name(function(n) {
	return n === "LT" || n === "TL" ? [1, 1] : n === "BL" || n === "LB" ? [1, -1] : n === "BR" || n === "RB" ? [-1, -1] : [-1, 1];
}, "getArchitectureDirectionXYFactors"), getArchitectureDirectionAlignment = /* @__PURE__ */ __name(function(n, j) {
	return isArchitectureDirectionXY(n, j) ? "bend" : isArchitectureDirectionX(n) ? "horizontal" : "vertical";
}, "getArchitectureDirectionAlignment"), isArchitectureService = /* @__PURE__ */ __name(function(n) {
	return n.type === "service";
}, "isArchitectureService"), isArchitectureJunction = /* @__PURE__ */ __name(function(n) {
	return n.type === "junction";
}, "isArchitectureJunction"), edgeData = /* @__PURE__ */ __name((n) => n.data(), "edgeData"), nodeData = /* @__PURE__ */ __name((n) => n.data(), "nodeData"), DEFAULT_ARCHITECTURE_CONFIG = defaultConfig_default.architecture, ArchitectureDB = class {
	constructor() {
		this.nodes = {}, this.groups = {}, this.edges = [], this.registeredIds = {}, this.elements = {}, this.setAccTitle = setAccTitle, this.getAccTitle = getAccTitle, this.setDiagramTitle = setDiagramTitle, this.getDiagramTitle = getDiagramTitle, this.getAccDescription = getAccDescription, this.setAccDescription = setAccDescription, this.clear();
	}
	static #e = __name(this, "ArchitectureDB");
	clear() {
		this.nodes = {}, this.groups = {}, this.edges = [], this.registeredIds = {}, this.dataStructures = void 0, this.elements = {}, clear();
	}
	addService({ id: n, icon: j, in: M, title: N, iconText: P }) {
		if (this.registeredIds[n] !== void 0) throw Error(`The service id [${n}] is already in use by another ${this.registeredIds[n]}`);
		if (M !== void 0) {
			if (n === M) throw Error(`The service [${n}] cannot be placed within itself`);
			if (this.registeredIds[M] === void 0) throw Error(`The service [${n}]'s parent does not exist. Please make sure the parent is created before this service`);
			if (this.registeredIds[M] === "node") throw Error(`The service [${n}]'s parent is not a group`);
		}
		this.registeredIds[n] = "node", this.nodes[n] = {
			id: n,
			type: "service",
			icon: j,
			iconText: P,
			title: N,
			edges: [],
			in: M
		};
	}
	getServices() {
		return Object.values(this.nodes).filter(isArchitectureService);
	}
	addJunction({ id: n, in: j }) {
		this.registeredIds[n] = "node", this.nodes[n] = {
			id: n,
			type: "junction",
			edges: [],
			in: j
		};
	}
	getJunctions() {
		return Object.values(this.nodes).filter(isArchitectureJunction);
	}
	getNodes() {
		return Object.values(this.nodes);
	}
	getNode(n) {
		return this.nodes[n] ?? null;
	}
	addGroup({ id: n, icon: j, in: M, title: N }) {
		if (this.registeredIds?.[n] !== void 0) throw Error(`The group id [${n}] is already in use by another ${this.registeredIds[n]}`);
		if (M !== void 0) {
			if (n === M) throw Error(`The group [${n}] cannot be placed within itself`);
			if (this.registeredIds?.[M] === void 0) throw Error(`The group [${n}]'s parent does not exist. Please make sure the parent is created before this group`);
			if (this.registeredIds?.[M] === "node") throw Error(`The group [${n}]'s parent is not a group`);
		}
		this.registeredIds[n] = "group", this.groups[n] = {
			id: n,
			icon: j,
			title: N,
			in: M
		};
	}
	getGroups() {
		return Object.values(this.groups);
	}
	addEdge({ lhsId: n, rhsId: j, lhsDir: M, rhsDir: N, lhsInto: P, rhsInto: F, lhsGroup: I, rhsGroup: L, title: R }) {
		if (!isArchitectureDirection(M)) throw Error(`Invalid direction given for left hand side of edge ${n}--${j}. Expected (L,R,T,B) got ${String(M)}`);
		if (!isArchitectureDirection(N)) throw Error(`Invalid direction given for right hand side of edge ${n}--${j}. Expected (L,R,T,B) got ${String(N)}`);
		if (this.nodes[n] === void 0 && this.groups[n] === void 0) throw Error(`The left-hand id [${n}] does not yet exist. Please create the service/group before declaring an edge to it.`);
		if (this.nodes[j] === void 0 && this.groups[j] === void 0) throw Error(`The right-hand id [${j}] does not yet exist. Please create the service/group before declaring an edge to it.`);
		let z = this.nodes[n].in, B = this.nodes[j].in;
		if (I && z && B && z == B) throw Error(`The left-hand id [${n}] is modified to traverse the group boundary, but the edge does not pass through two groups.`);
		if (L && z && B && z == B) throw Error(`The right-hand id [${j}] is modified to traverse the group boundary, but the edge does not pass through two groups.`);
		let V = {
			lhsId: n,
			lhsDir: M,
			lhsInto: P,
			lhsGroup: I,
			rhsId: j,
			rhsDir: N,
			rhsInto: F,
			rhsGroup: L,
			title: R
		};
		this.edges.push(V), this.nodes[n] && this.nodes[j] && (this.nodes[n].edges.push(this.edges[this.edges.length - 1]), this.nodes[j].edges.push(this.edges[this.edges.length - 1]));
	}
	getEdges() {
		return this.edges;
	}
	getDataStructures() {
		if (this.dataStructures === void 0) {
			let n = {}, M = Object.entries(this.nodes).reduce((j, [M, N]) => (j[M] = N.edges.reduce((j, N) => {
				let P = this.getNode(N.lhsId)?.in, F = this.getNode(N.rhsId)?.in;
				if (P && F && P !== F) {
					let j = getArchitectureDirectionAlignment(N.lhsDir, N.rhsDir);
					j !== "bend" && (n[P] ??= {}, n[P][F] = j, n[F] ??= {}, n[F][P] = j);
				}
				if (N.lhsId === M) {
					let n = getArchitectureDirectionPair(N.lhsDir, N.rhsDir);
					n && (j[n] = N.rhsId);
				} else {
					let n = getArchitectureDirectionPair(N.rhsDir, N.lhsDir);
					n && (j[n] = N.lhsId);
				}
				return j;
			}, {}), j), {}), N = Object.keys(M)[0], P = { [N]: 1 }, F = Object.keys(M).reduce((n, j) => j === N ? n : {
				...n,
				[j]: 1
			}, {}), I = /* @__PURE__ */ __name((n) => {
				let j = { [n]: [0, 0] }, N = [n];
				for (; N.length > 0;) {
					let n = N.shift();
					if (n) {
						P[n] = 1, delete F[n];
						let I = M[n], [L, R] = j[n];
						Object.entries(I).forEach(([n, M]) => {
							P[M] || (j[M] = shiftPositionByArchitectureDirectionPair([L, R], n), N.push(M));
						});
					}
				}
				return j;
			}, "BFS"), L = [I(N)];
			for (; Object.keys(F).length > 0;) L.push(I(Object.keys(F)[0]));
			this.dataStructures = {
				adjList: M,
				spatialMaps: L,
				groupAlignments: n
			};
		}
		return this.dataStructures;
	}
	setElementForId(n, j) {
		this.elements[n] = j;
	}
	getElementById(n) {
		return this.elements[n];
	}
	getConfig() {
		return cleanAndMerge({
			...DEFAULT_ARCHITECTURE_CONFIG,
			...getConfig().architecture
		});
	}
	getConfigField(n) {
		return this.getConfig()[n];
	}
}, populateDb = /* @__PURE__ */ __name((n, j) => {
	populateCommonDb(n, j), n.groups.map((n) => j.addGroup(n)), n.services.map((n) => j.addService({
		...n,
		type: "service"
	})), n.junctions.map((n) => j.addJunction({
		...n,
		type: "junction"
	})), n.edges.map((n) => j.addEdge(n));
}, "populateDb"), parser = {
	parser: { yy: void 0 },
	parse: /* @__PURE__ */ __name(async (n) => {
		let j = await parse("architecture", n);
		log.debug(j);
		let N = parser.parser?.yy;
		if (!(N instanceof ArchitectureDB)) 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.");
		populateDb(j, N);
	}, "parse")
}, architectureStyles_default = /* @__PURE__ */ __name((n) => `
  .edge {
    stroke-width: ${n.archEdgeWidth};
    stroke: ${n.archEdgeColor};
    fill: none;
  }

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

  .node-bkg {
    fill: none;
    stroke: ${n.archGroupBorderColor};
    stroke-width: ${n.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"), wrapIcon = /* @__PURE__ */ __name((n) => `<g><rect width="80" height="80" style="fill: #087ebf; stroke-width: 0px;"/>${n}</g>`, "wrapIcon"), architectureIcons = {
	prefix: "mermaid-architecture",
	height: 80,
	width: 80,
	icons: {
		database: { body: wrapIcon("<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: wrapIcon("<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: wrapIcon("<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: wrapIcon("<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: wrapIcon("<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: unknownIcon,
		blank: { body: wrapIcon("") }
	}
}, drawEdges = /* @__PURE__ */ __name(async function(n, j, M) {
	let N = M.getConfigField("padding"), P = M.getConfigField("iconSize"), F = P / 2, I = P / 6, L = I / 2;
	await Promise.all(j.edges().map(async (j) => {
		let { source: P, sourceDir: z, sourceArrow: B, sourceGroup: V, target: H, targetDir: U, targetArrow: W, targetGroup: G, label: K } = edgeData(j), { x: q, y: J } = j[0].sourceEndpoint(), { x: Y, y: X } = j[0].midpoint(), { x: Z, y: Q } = j[0].targetEndpoint(), $ = N + 4;
		if (V && (isArchitectureDirectionX(z) ? q += z === "L" ? -$ : $ : J += z === "T" ? -$ : $ + 18), G && (isArchitectureDirectionX(U) ? Z += U === "L" ? -$ : $ : Q += U === "T" ? -$ : $ + 18), !V && M.getNode(P)?.type === "junction" && (isArchitectureDirectionX(z) ? q += z === "L" ? F : -F : J += z === "T" ? F : -F), !G && M.getNode(H)?.type === "junction" && (isArchitectureDirectionX(U) ? Z += U === "L" ? F : -F : Q += U === "T" ? F : -F), j[0]._private.rscratch) {
			let j = n.insert("g");
			if (j.insert("path").attr("d", `M ${q},${J} L ${Y},${X} L${Z},${Q} `).attr("class", "edge").attr("id", getEdgeId(P, H, { prefix: "L" })), B) {
				let n = isArchitectureDirectionX(z) ? ArchitectureDirectionArrowShift[z](q, I) : q - L, M = isArchitectureDirectionY(z) ? ArchitectureDirectionArrowShift[z](J, I) : J - L;
				j.insert("polygon").attr("points", ArchitectureDirectionArrow[z](I)).attr("transform", `translate(${n},${M})`).attr("class", "arrow");
			}
			if (W) {
				let n = isArchitectureDirectionX(U) ? ArchitectureDirectionArrowShift[U](Z, I) : Z - L, M = isArchitectureDirectionY(U) ? ArchitectureDirectionArrowShift[U](Q, I) : Q - L;
				j.insert("polygon").attr("points", ArchitectureDirectionArrow[U](I)).attr("transform", `translate(${n},${M})`).attr("class", "arrow");
			}
			if (K) {
				let n = isArchitectureDirectionXY(z, U) ? "XY" : isArchitectureDirectionX(z) ? "X" : "Y", M = 0;
				M = n === "X" ? Math.abs(q - Z) : n === "Y" ? Math.abs(J - Q) / 1.5 : Math.abs(q - Z) / 2;
				let N = j.append("g");
				if (await createText(N, K, {
					useHtmlLabels: !1,
					width: M,
					classes: "architecture-service-label"
				}, getConfig2()), N.attr("dy", "1em").attr("alignment-baseline", "middle").attr("dominant-baseline", "middle").attr("text-anchor", "middle"), n === "X") N.attr("transform", "translate(" + Y + ", " + X + ")");
				else if (n === "Y") N.attr("transform", "translate(" + Y + ", " + X + ") rotate(-90)");
				else if (n === "XY") {
					let n = getArchitectureDirectionPair(z, U);
					if (n && isArchitecturePairXY(n)) {
						let j = N.node().getBoundingClientRect(), [M, P] = getArchitectureDirectionXYFactors(n);
						N.attr("dominant-baseline", "auto").attr("transform", `rotate(${-1 * M * P * 45})`);
						let F = N.node().getBoundingClientRect();
						N.attr("transform", `
                translate(${Y}, ${X - j.height / 2})
                translate(${M * F.width / 2}, ${P * F.height / 2})
                rotate(${-1 * M * P * 45}, 0, ${j.height / 2})
              `);
					}
				}
			}
		}
	}));
}, "drawEdges"), drawGroups = /* @__PURE__ */ __name(async function(n, j, M) {
	let N = M.getConfigField("padding") * .75, P = M.getConfigField("fontSize"), F = M.getConfigField("iconSize") / 2;
	await Promise.all(j.nodes().map(async (j) => {
		let I = nodeData(j);
		if (I.type === "group") {
			let { h: L, w: z, x1: B, y1: V } = j.boundingBox(), H = n.append("rect");
			H.attr("id", `group-${I.id}`).attr("x", B + F).attr("y", V + F).attr("width", z).attr("height", L).attr("class", "node-bkg");
			let U = n.append("g"), W = B, G = V;
			if (I.icon) {
				let n = U.append("g");
				n.html(`<g>${await getIconSVG(I.icon, {
					height: N,
					width: N,
					fallbackPrefix: architectureIcons.prefix
				})}</g>`), n.attr("transform", "translate(" + (W + F + 1) + ", " + (G + F + 1) + ")"), W += N, G += P / 2 - 1 - 2;
			}
			if (I.label) {
				let n = U.append("g");
				await createText(n, I.label, {
					useHtmlLabels: !1,
					width: z,
					classes: "architecture-service-label"
				}, getConfig2()), n.attr("dy", "1em").attr("alignment-baseline", "middle").attr("dominant-baseline", "start").attr("text-anchor", "start"), n.attr("transform", "translate(" + (W + F + 4) + ", " + (G + F + 2) + ")");
			}
			M.setElementForId(I.id, H);
		}
	}));
}, "drawGroups"), drawServices = /* @__PURE__ */ __name(async function(n, j, M) {
	let N = getConfig2();
	for (let P of M) {
		let M = j.append("g"), F = n.getConfigField("iconSize");
		if (P.title) {
			let n = M.append("g");
			await createText(n, P.title, {
				useHtmlLabels: !1,
				width: F * 1.5,
				classes: "architecture-service-label"
			}, N), n.attr("dy", "1em").attr("alignment-baseline", "middle").attr("dominant-baseline", "middle").attr("text-anchor", "middle"), n.attr("transform", "translate(" + F / 2 + ", " + F + ")");
		}
		let I = M.append("g");
		if (P.icon) I.html(`<g>${await getIconSVG(P.icon, {
			height: F,
			width: F,
			fallbackPrefix: architectureIcons.prefix
		})}</g>`);
		else if (P.iconText) {
			I.html(`<g>${await getIconSVG("blank", {
				height: F,
				width: F,
				fallbackPrefix: architectureIcons.prefix
			})}</g>`);
			let n = I.append("g").append("foreignObject").attr("width", F).attr("height", F).append("div").attr("class", "node-icon-text").attr("style", `height: ${F}px;`).append("div").html(sanitizeText(P.iconText, N)), j = parseInt(window.getComputedStyle(n.node(), null).getPropertyValue("font-size").replace(/\D/g, "")) ?? 16;
			n.attr("style", `-webkit-line-clamp: ${Math.floor((F - 2) / j)};`);
		} else I.append("path").attr("class", "node-bkg").attr("id", "node-" + P.id).attr("d", `M0 ${F} v${-F} q0,-5 5,-5 h${F} q5,0 5,5 v${F} H0 Z`);
		M.attr("id", `service-${P.id}`).attr("class", "architecture-service");
		let { width: L, height: R } = M.node().getBBox();
		P.width = L, P.height = R, n.setElementForId(P.id, M);
	}
	return 0;
}, "drawServices"), drawJunctions = /* @__PURE__ */ __name(function(n, j, M) {
	M.forEach((M) => {
		let N = j.append("g"), P = n.getConfigField("iconSize");
		N.append("g").append("rect").attr("id", "node-" + M.id).attr("fill-opacity", "0").attr("width", P).attr("height", P), N.attr("class", "architecture-junction");
		let { width: F, height: I } = N._groups[0][0].getBBox();
		N.width = F, N.height = I, n.setElementForId(M.id, N);
	});
}, "drawJunctions");
registerIconPacks([{
	name: architectureIcons.prefix,
	icons: architectureIcons
}]), cytoscape.use(import_cytoscape_fcose.default);
function addServices(n, j, M) {
	n.forEach((n) => {
		j.add({
			group: "nodes",
			data: {
				type: "service",
				id: n.id,
				icon: n.icon,
				label: n.title,
				parent: n.in,
				width: M.getConfigField("iconSize"),
				height: M.getConfigField("iconSize")
			},
			classes: "node-service"
		});
	});
}
__name(addServices, "addServices");
function addJunctions(n, j, M) {
	n.forEach((n) => {
		j.add({
			group: "nodes",
			data: {
				type: "junction",
				id: n.id,
				parent: n.in,
				width: M.getConfigField("iconSize"),
				height: M.getConfigField("iconSize")
			},
			classes: "node-junction"
		});
	});
}
__name(addJunctions, "addJunctions");
function positionNodes(n, j) {
	j.nodes().map((j) => {
		let M = nodeData(j);
		M.type !== "group" && (M.x = j.position().x, M.y = j.position().y, n.getElementById(M.id).attr("transform", "translate(" + (M.x || 0) + "," + (M.y || 0) + ")"));
	});
}
__name(positionNodes, "positionNodes");
function addGroups(n, j) {
	n.forEach((n) => {
		j.add({
			group: "nodes",
			data: {
				type: "group",
				id: n.id,
				icon: n.icon,
				label: n.title,
				parent: n.in
			},
			classes: "node-group"
		});
	});
}
__name(addGroups, "addGroups");
function addEdges(n, j) {
	n.forEach((n) => {
		let { lhsId: M, rhsId: N, lhsInto: P, lhsGroup: F, rhsInto: I, lhsDir: L, rhsDir: R, rhsGroup: z, title: B } = n, V = isArchitectureDirectionXY(n.lhsDir, n.rhsDir) ? "segments" : "straight", H = {
			id: `${M}-${N}`,
			label: B,
			source: M,
			sourceDir: L,
			sourceArrow: P,
			sourceGroup: F,
			sourceEndpoint: L === "L" ? "0 50%" : L === "R" ? "100% 50%" : L === "T" ? "50% 0" : "50% 100%",
			target: N,
			targetDir: R,
			targetArrow: I,
			targetGroup: z,
			targetEndpoint: R === "L" ? "0 50%" : R === "R" ? "100% 50%" : R === "T" ? "50% 0" : "50% 100%"
		};
		j.add({
			group: "edges",
			data: H,
			classes: V
		});
	});
}
__name(addEdges, "addEdges");
function getAlignments(n, M, N) {
	let P = /* @__PURE__ */ __name((n, j) => Object.entries(n).reduce((n, [M, P]) => {
		let F = 0, I = Object.entries(P);
		if (I.length === 1) return n[M] = I[0][1], n;
		for (let P = 0; P < I.length - 1; P++) for (let L = P + 1; L < I.length; L++) {
			let [R, z] = I[P], [B, V] = I[L];
			if (N[R]?.[B] === j) n[M] ??= [], n[M] = [
				...n[M],
				...z,
				...V
			];
			else if (R === "default" || B === "default") n[M] ??= [], n[M] = [
				...n[M],
				...z,
				...V
			];
			else {
				let j = `${M}-${F++}`;
				n[j] = z;
				let N = `${M}-${F++}`;
				n[N] = V;
			}
		}
		return n;
	}, {}), "flattenAlignments"), [F, I] = M.map((j) => {
		let M = {}, N = {};
		return Object.entries(j).forEach(([j, [P, F]]) => {
			let I = n.getNode(j)?.in ?? "default";
			M[F] ??= {}, M[F][I] ??= [], M[F][I].push(j), N[P] ??= {}, N[P][I] ??= [], N[P][I].push(j);
		}), {
			horiz: Object.values(P(M, "horizontal")).filter((n) => n.length > 1),
			vert: Object.values(P(N, "vertical")).filter((n) => n.length > 1)
		};
	}).reduce(([n, j], { horiz: M, vert: N }) => [[...n, ...M], [...j, ...N]], [[], []]);
	return {
		horizontal: F,
		vertical: I
	};
}
__name(getAlignments, "getAlignments");
function getRelativeConstraints(n, M) {
	let N = [], P = /* @__PURE__ */ __name((n) => `${n[0]},${n[1]}`, "posToStr"), F = /* @__PURE__ */ __name((n) => n.split(",").map((n) => parseInt(n)), "strToPos");
	return n.forEach((n) => {
		let j = Object.fromEntries(Object.entries(n).map(([n, j]) => [P(j), n])), I = [P([0, 0])], L = {}, R = {
			L: [-1, 0],
			R: [1, 0],
			T: [0, 1],
			B: [0, -1]
		};
		for (; I.length > 0;) {
			let n = I.shift();
			if (n) {
				L[n] = 1;
				let z = j[n];
				if (z) {
					let B = F(n);
					Object.entries(R).forEach(([n, F]) => {
						let R = P([B[0] + F[0], B[1] + F[1]]), V = j[R];
						V && !L[R] && (I.push(R), N.push({
							[ArchitectureDirectionName[n]]: V,
							[ArchitectureDirectionName[getOppositeArchitectureDirection(n)]]: z,
							gap: 1.5 * M.getConfigField("iconSize")
						}));
					});
				}
			}
		}
	}), N;
}
__name(getRelativeConstraints, "getRelativeConstraints");
function layoutArchitecture(n, N, P, F, I, { spatialMaps: L, groupAlignments: R }) {
	return new Promise((z) => {
		let B = select_default("body").append("div").attr("id", "cy").attr("style", "display:none"), V = cytoscape({
			container: document.getElementById("cy"),
			style: [
				{
					selector: "edge",
					style: {
						"curve-style": "straight",
						label: "data(label)",
						"source-endpoint": "data(sourceEndpoint)",
						"target-endpoint": "data(targetEndpoint)"
					}
				},
				{
					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": `${I.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: `${I.getConfigField("padding")}px` }
				}
			],
			layout: {
				name: "grid",
				boundingBox: {
					x1: 0,
					x2: 100,
					y1: 0,
					y2: 100
				}
			}
		});
		B.remove(), addGroups(P, V), addServices(n, V, I), addJunctions(N, V, I), addEdges(F, V);
		let H = getAlignments(I, L, R), U = getRelativeConstraints(L, I), W = V.layout({
			name: "fcose",
			quality: "proof",
			styleEnabled: !1,
			animate: !1,
			nodeDimensionsIncludeLabels: !1,
			idealEdgeLength(n) {
				let [j, M] = n.connectedNodes(), { parent: N } = nodeData(j), { parent: P } = nodeData(M);
				return N === P ? 1.5 * I.getConfigField("iconSize") : .5 * I.getConfigField("iconSize");
			},
			edgeElasticity(n) {
				let [j, M] = n.connectedNodes(), { parent: N } = nodeData(j), { parent: P } = nodeData(M);
				return N === P ? .45 : .001;
			},
			alignmentConstraint: H,
			relativePlacementConstraint: U
		});
		W.one("layoutstop", () => {
			function n(n, j, M, N) {
				let P, F, { x: I, y: L } = n, { x: R, y: z } = j;
				F = (N - L + (I - M) * (L - z) / (I - R)) / Math.sqrt(1 + ((L - z) / (I - R)) ** 2), P = Math.sqrt((N - L) ** 2 + (M - I) ** 2 - F ** 2);
				let B = Math.sqrt((R - I) ** 2 + (z - L) ** 2);
				P /= B;
				let V = (R - I) * (N - L) - (z - L) * (M - I);
				switch (!0) {
					case V >= 0:
						V = 1;
						break;
					case V < 0:
						V = -1;
						break;
				}
				let H = (R - I) * (M - I) + (z - L) * (N - L);
				switch (!0) {
					case H >= 0:
						H = 1;
						break;
					case H < 0:
						H = -1;
						break;
				}
				return F = Math.abs(F) * V, P *= H, {
					distances: F,
					weights: P
				};
			}
			__name(n, "getSegmentWeights"), V.startBatch();
			for (let j of Object.values(V.edges())) if (j.data?.()) {
				let { x: M, y: N } = j.source().position(), { x: P, y: F } = j.target().position();
				if (M !== P && N !== F) {
					let M = j.sourceEndpoint(), N = j.targetEndpoint(), { sourceDir: P } = edgeData(j), [F, I] = isArchitectureDirectionY(P) ? [M.x, N.y] : [N.x, M.y], { weights: L, distances: R } = n(M, N, F, I);
					j.style("segment-distances", R), j.style("segment-weights", L);
				}
			}
			V.endBatch(), W.run();
		}), W.run(), V.ready((n) => {
			log.info("Ready", n), z(V);
		});
	});
}
__name(layoutArchitecture, "layoutArchitecture");
var diagram = {
	parser,
	get db() {
		return new ArchitectureDB();
	},
	renderer: { draw: /* @__PURE__ */ __name(async (n, j, M, N) => {
		let P = N.db, F = P.getServices(), I = P.getJunctions(), L = P.getGroups(), R = P.getEdges(), z = P.getDataStructures(), B = selectSvgElement(j), V = B.append("g");
		V.attr("class", "architecture-edges");
		let H = B.append("g");
		H.attr("class", "architecture-services");
		let U = B.append("g");
		U.attr("class", "architecture-groups"), await drawServices(P, H, F), drawJunctions(P, H, I);
		let G = await layoutArchitecture(F, I, L, R, P, z);
		await drawEdges(V, G, P), await drawGroups(U, G, P), positionNodes(P, G), setupGraphViewbox(void 0, B, P.getConfigField("padding"), P.getConfigField("useMaxWidth"));
	}, "draw") },
	styles: architectureStyles_default
};
export { diagram };
