import { isEmResetFrame as e } from "../../../../@mermaid-js/parser/dist/chunks/mermaid-parser.core/chunk-NNHCCRGN.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, setupGraphViewbox2 as m } from "./chunk-CSCIHK7Q.js";
import h from "../../../../d3-selection/src/select.js";
import "../../../../d3/src/index.js";
import { calculateTextDimensions as ee, cleanAndMerge as te, wrapLabel as g } from "./chunk-5ZQYHXKU.js";
import { populateCommonDb as ne } from "./chunk-4BX2VUAB.js";
import { parse as re } from "../../../../@mermaid-js/parser/dist/mermaid-parser.core.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/diagram-KO2AKTUF.mjs
var _ = "position frame", v = "frame positioned", y = "position relation", b = "relation positioned", ie = /* @__PURE__ */ t(function(e) {
	n.debug("options str", e);
}, "setOptions"), ae = /* @__PURE__ */ t(function() {
	return {};
}, "getOptions"), oe = /* @__PURE__ */ t(function() {
	x(), r();
}, "clear");
function x() {
	S = {};
}
t(x, "reset");
var se = i.eventmodeling, ce = /* @__PURE__ */ t(() => te({
	...se,
	...s().eventmodeling
}), "getConfig"), S = {};
function C() {
	let e = le, { ast: t } = S, r = E();
	if (!t) throw Error("No data for EventModel");
	return t.frames.forEach((i, a) => {
		let o = N(i, t.dataEntities, r);
		e = q(e, {
			$kind: _,
			index: a,
			frame: i,
			textProps: o
		});
		let s;
		B(i) ? (n.debug("source frame", i.sourceFrames), s = t.frames.filter((e) => i.sourceFrames.some((t) => t.$refText === e.name)), s.forEach((t) => {
			e = q(e, {
				$kind: y,
				index: a,
				frame: i,
				sourceFrame: t
			});
		})) : e = q(e, {
			$kind: y,
			index: a,
			frame: i
		});
	}), e = {
		...e,
		sortedSwimlanesArray: L(e.swimlanes)
	}, e;
}
t(C, "getState");
function w(e) {
	S.ast = e;
}
t(w, "setAst");
var T = {
	swimlaneMinHeight: 70,
	swimlanePadding: 15,
	swimlaneGap: 10,
	boxPadding: 10,
	boxOverlap: 90,
	boxDefaultY: 0,
	boxMinWidth: 80,
	boxMaxWidth: 450,
	boxMinHeight: 80,
	boxMaxHeight: 750,
	contentStartX: 250,
	textMaxWidth: 430,
	boxTextFontWeight: "bold",
	boxTextPadding: 10,
	swimlaneTextFontWeight: "bold",
	labelUiAutomation: "UI/Automation",
	labelUiAutomationPrefix: "UI/A: ",
	labelCommandReadModel: "Command/Read Model",
	labelCommandReadModelPrefix: "C/RM: ",
	labelEvents: "Events",
	labelEventsPrefix: "Stream: "
};
function E() {
	return T;
}
t(E, "getDiagramProps");
var le = {
	boxes: [],
	swimlanes: {},
	relations: [],
	maxR: 0,
	sortedSwimlanesArray: []
};
function D(e) {
	let t = e.split(".");
	if (t.length === 2) return t[0];
}
t(D, "extractNamespace");
function O(e) {
	let t = e.split(".");
	return t.length === 2 ? t[1] : e;
}
t(O, "extractName");
function k(e, t) {
	if (!(!t || t.length === 0)) return Object.values(e).find((e) => e.namespace === t);
}
t(k, "findSwimlaneByNamespace");
function A(e, t, n) {
	return Math.max(t, ...Object.keys(e).filter((e) => {
		let r = Number.parseInt(e);
		return r > t && r < n;
	}).map((e) => Number.parseInt(e))) + 1;
}
t(A, "findNextAvailableIndex");
function j(e, t) {
	let n = D(e.entityIdentifier), r = k(t, n);
	switch (e.modelEntityType) {
		case "ui":
		case "pcr":
		case "processor": return r ? {
			index: r.index,
			label: r.namespace || T.labelUiAutomation
		} : n ? {
			index: A(t, 0, 100),
			label: T.labelUiAutomationPrefix + n
		} : {
			index: 0,
			label: T.labelUiAutomation
		};
		case "rmo":
		case "readmodel":
		case "cmd":
		case "command": return r ? {
			index: r.index,
			label: r.namespace || T.labelCommandReadModel
		} : n ? {
			index: A(t, 100, 200),
			label: T.labelCommandReadModelPrefix + n
		} : {
			index: 100,
			label: T.labelCommandReadModel
		};
		default: return r ? {
			index: r.index,
			label: r.namespace || T.labelEvents
		} : n ? {
			index: A(t, 200, 300),
			label: T.labelEventsPrefix + n
		} : {
			index: 200,
			label: T.labelEvents
		};
	}
}
t(j, "calculateSwimlaneProps");
function M(e) {
	let { themeVariables: t } = s();
	switch (e.modelEntityType) {
		case "ui": return {
			fill: t.emUiFill ?? "white",
			stroke: t.emUiStroke ?? "#dbdada"
		};
		case "pcr":
		case "processor": return {
			fill: t.emProcessorFill ?? "#edb3f6",
			stroke: t.emProcessorStroke ?? "#b88cbf"
		};
		case "rmo":
		case "readmodel": return {
			fill: t.emReadModelFill ?? "#d3f1a2",
			stroke: t.emReadModelStroke ?? "#a3b732"
		};
		case "cmd":
		case "command": return {
			fill: t.emCommandFill ?? "#bcd6fe",
			stroke: t.emCommandStroke ?? "#679ac3"
		};
		case "evt":
		case "event": return {
			fill: t.emEventFill ?? "#ffb778",
			stroke: t.emEventStroke ?? "#c19a0f"
		};
		default: return {
			fill: "red",
			stroke: "black"
		};
	}
}
t(M, "calculateEntityVisualProps");
function N(e, t, r) {
	let i = s(), a = u(O(e.entityIdentifier) ?? "", i), o, c = {
		fontSize: 16,
		fontWeight: 700,
		fontFamily: "\"trebuchet ms\", verdana, arial, sans-serif",
		joinWith: "<br/>"
	}, l = `<b>${g(a, r.textMaxWidth, c)}</b>`;
	if (e.dataInlineValue && (o = e.dataInlineValue, o = o.substring(o.indexOf("{") + 1), o = o.substring(0, o.lastIndexOf("}") - 1), o = u(o, i), o = g(o, r.textMaxWidth, c), o = o.replaceAll(" ", "&nbsp;")), e.dataReference) {
		let n = t.find((t) => t.name === e.dataReference?.$refText);
		n && (o = n.dataBlockValue, o = o.substring(o.indexOf("{\n") + 2), o = o.substring(0, o.lastIndexOf("}") - 1), o = u(o, i), o = g(o, r.textMaxWidth, c), o = o.replaceAll(" ", "&nbsp;"), o += "<br/>");
	}
	let d = o !== void 0;
	d && (l += `<br/><br/><code style="text-align: left; display: block;max-width:${r.textMaxWidth}px">${o}</code>`);
	let f = {
		fontSize: c.fontSize,
		fontWeight: c.fontWeight,
		fontFamily: c.fontFamily
	}, p = ee(l, f), m = d ? p.width / 3 : p.width, h = {
		content: l,
		width: m,
		height: p.height
	};
	return n.debug(`[${e.name}] ${e.entityIdentifier} text`, h), h;
}
t(N, "calculateTextProps");
function P(e, t) {
	let n = t, r = M(n.frame), i = {
		width: n.textProps.width + 2 * T.boxTextPadding,
		height: n.textProps.height + 2 * T.boxTextPadding
	};
	return [{
		$kind: v,
		frame: n.frame,
		index: n.index,
		visual: r,
		dimension: i,
		textProps: n.textProps
	}];
}
t(P, "decidePositionFrame");
function F(e, t, n) {
	return t === void 0 ? T.contentStartX : t.index === e.index && e.r ? e.r + T.boxPadding : n === void 0 ? T.contentStartX : n.r - T.boxOverlap + T.boxPadding;
}
t(F, "calculateX");
function I(e, t) {
	let n = [...e.map((e) => e.r), t];
	return Math.max(...n);
}
t(I, "calculateMaxRight");
function L(e) {
	return Object.values(e).sort((e, t) => e.index - t.index);
}
t(L, "sortedSwimlanesArray");
function R(e, t) {
	let n = t, r = j(n.frame, e.swimlanes), i;
	i = r.index in e.swimlanes ? e.swimlanes[r.index] : {
		index: r.index,
		label: r.label,
		r: 0,
		y: r.index * T.swimlaneMinHeight + T.swimlaneGap,
		height: T.swimlaneMinHeight,
		maxHeight: T.swimlaneMinHeight
	};
	let a = e.boxes.length > 0 ? e.boxes[e.boxes.length - 1] : void 0, o = e.previousSwimlaneNumber === void 0 ? void 0 : e.swimlanes[e.previousSwimlaneNumber], s = {
		width: Math.max(T.boxMinWidth, Math.min(T.boxMaxWidth, n.dimension.width)) + 2 * T.boxPadding,
		height: Math.max(T.boxMinHeight, Math.min(T.boxMaxHeight, n.dimension.height)) + 2 * T.boxPadding
	}, c = F(i, o, a), l = c + s.width + T.boxPadding, u = I(Object.values(e.swimlanes), l);
	i.r = c + s.width, i.maxHeight = Math.max(i.maxHeight, s.height), i.height = Math.max(T.swimlaneMinHeight, i.maxHeight) + 2 * T.swimlanePadding;
	let d = {
		x: c,
		y: T.swimlanePadding + i.y,
		r: l,
		dimension: s,
		leftSibling: !1,
		swimlane: i,
		visual: n.visual,
		text: n.textProps.content,
		frame: n.frame,
		index: n.index
	}, f = {
		...e,
		boxes: [...e.boxes, d],
		swimlanes: {
			...e.swimlanes,
			[`${i.index}`]: i
		},
		previousSwimlaneNumber: r.index,
		previousFrame: n.frame,
		maxR: u
	}, p = L(f.swimlanes);
	p.length > 0 && (p[0].y = 0);
	for (let e = 1; e < p.length; e++) {
		let t = p[e], n = p[e - 1];
		t.y = n.y + n.height + T.swimlaneGap;
	}
	return f;
}
t(R, "evolveFramePositioned");
function z(e, t) {
	return e === 0 && t.sourceFrames.length === 0;
}
t(z, "isFirstFrame");
function B(e) {
	return e.sourceFrames !== void 0 && e.sourceFrames !== null && e.sourceFrames.length > 0;
}
t(B, "hasSourceFrame");
function V(e, t) {
	if (t != null) return e.find((e) => e.frame.name === t.name);
}
t(V, "findBoxByFrame");
function H(e, t, n) {
	if (!(n < 0)) for (let r = n; r >= 0; r--) {
		let n = e[r];
		if (n.swimlane.index !== t) return n;
	}
}
t(H, "findBoxByLineIndex");
function U(t, n) {
	let r = n;
	if (e(r.frame) || z(r.index, r.frame)) return [];
	let i = V(t.boxes, r.frame);
	if (i === void 0) throw Error(`Target box not found for frame ${r.frame.name}`);
	let a;
	return a = r.sourceFrame ? V(t.boxes, r.sourceFrame) : H(t.boxes, i.swimlane.index, r.index - 1), a === void 0 ? [] : [{
		$kind: b,
		frame: r.frame,
		index: r.index,
		sourceBox: a,
		targetBox: i
	}];
}
t(U, "decidePositionRelation");
function W(e, t) {
	let n = t, r = {
		visual: {
			fill: "none",
			stroke: "#000"
		},
		source: {
			x: n.sourceBox.x,
			y: n.sourceBox.y
		},
		target: {
			x: n.targetBox.x,
			y: n.targetBox.y
		},
		sourceBox: n.sourceBox,
		targetBox: n.targetBox
	};
	return {
		...e,
		relations: [...e.relations, r]
	};
}
t(W, "evolveRelationPositioned");
var ue = {
	[_]: P,
	[y]: U
}, de = {
	[v]: R,
	[b]: W
};
function G(e, t) {
	let r = ue[t.$kind];
	if (r == null) return [];
	let i = r(e, t);
	return n.debug("decided events", i), i;
}
t(G, "decide");
function K(e, t) {
	let r = t.reduce((e, t) => {
		let n = de[t.$kind];
		return n == null ? e : n(e, t);
	}, e);
	return n.debug("evolve events", {
		state: e,
		newState: r,
		events: t
	}), r;
}
t(K, "evolve");
function q(e, t) {
	return K(e, G(e, t));
}
t(q, "dispatch");
var J = {
	getConfig: ce,
	setOptions: ie,
	getOptions: ae,
	clear: oe,
	setAccTitle: f,
	getAccTitle: o,
	getAccDescription: a,
	setAccDescription: d,
	setDiagramTitle: p,
	getDiagramTitle: l,
	setAst: w,
	getDiagramProps: E,
	getState: C
}, Y = { parse: /* @__PURE__ */ t(async (e) => {
	let t = await re("eventmodeling", e);
	n.debug(t), J.setAst(t), ne(t, J);
}, "parse") }, fe = c()?.eventmodeling;
function X(e, t) {
	return (n) => {
		let r = n.swimlane.y + t.swimlanePadding, i = e.append("g").attr("class", "em-box");
		i.append("rect").attr("x", n.x).attr("y", r).attr("rx", "3").attr("width", n.dimension.width).attr("height", n.dimension.height).attr("stroke", n.visual.stroke).attr("fill", n.visual.fill), i.append("foreignObject").attr("x", n.x + t.boxPadding).attr("y", r + 10).attr("width", n.dimension.width - 2 * t.boxPadding).attr("height", n.dimension.height - 2 * t.boxPadding).append("xhtml:div").style("display", "table").style("height", "100%").style("width", "100%").append("span").style("display", "table-cell").style("text-align", "center").style("vertical-align", "middle").html(n.text);
	};
}
t(X, "renderD3Box");
function Z(e, t) {
	return e > t;
}
t(Z, "dirUpwards");
function Q(e, t, r, i) {
	return (a) => {
		let o = a.sourceBox.swimlane.y + t.swimlanePadding, s = a.targetBox.swimlane.y + t.swimlanePadding, c = Z(o, s), l = a.sourceBox.x + a.sourceBox.dimension.width * 2 / 3, u = a.targetBox.x + a.targetBox.dimension.width / 3, d, f;
		n.debug(`rendering relation up=${c} for `, {
			sourceBox: a.sourceBox,
			targetBox: a.targetBox
		}), c ? (d = o, f = s + a.targetBox.dimension.height) : (d = o + a.sourceBox.dimension.height, f = s);
		let p = i.emRelationStroke ?? a.visual.stroke;
		e.append("path").attr("class", "em-relation").attr("fill", a.visual.fill).attr("stroke", p).attr("stroke-width", "1").attr("marker-end", `url(#${r})`).attr("d", `M${l} ${d} L${u} ${f}`);
	};
}
t(Q, "renderD3Relation");
function $(e, t, n, r) {
	return (i) => {
		let a = e.append("g").attr("class", "em-swimlane"), o = r.emSwimlaneBackgroundOdd ?? "rgb(250,250,250)", s = r.emSwimlaneBackgroundStroke ?? "rgb(240,240,240)";
		a.append("rect").attr("x", 0).attr("y", i.y).attr("rx", "3").attr("width", t + n.swimlanePadding).attr("height", i.height).attr("fill", o).attr("stroke", s), a.append("text").attr("font-weight", n.swimlaneTextFontWeight).attr("x", 30).attr("y", i.y + 30).text(i.label);
	};
}
t($, "renderD3Swimlane");
var pe = {
	parser: Y,
	db: J,
	renderer: { draw: /* @__PURE__ */ t(function(e, t, r, i) {
		if (n.debug("in eventmodeling renderer", e + "\n", "id:", t, r), !fe) throw Error("EventModeling config not found");
		let a = i.db, { themeVariables: o, eventmodeling: s } = c(), l = h(`[id="${t}"]`), u = a.getDiagramProps(), d = a.getState(), f = `em-arrowhead-${t}`, p = o.emArrowhead ?? "#000000";
		d.sortedSwimlanesArray.forEach($(l, d.maxR, u, o)), d.boxes.forEach(X(l, u)), d.relations.forEach(Q(l, u, f, o)), l.append("defs").append("marker").attr("id", f).attr("markerWidth", "10").attr("markerHeight", "7").attr("refX", "10").attr("refY", "3.5").attr("orient", "auto").append("polygon").attr("points", "0 0, 10 3.5, 0 7").attr("fill", p), m(void 0, l, s?.padding ?? 30, s?.useMaxWidth);
	}, "draw") },
	styles: /* @__PURE__ */ t((e) => "", "getStyles")
};
//#endregion
export { pe as diagram };

//# sourceMappingURL=diagram-KO2AKTUF.js.map