import { __name as e, log as t } from "./chunk-AGHRB4JF.js";
import { assignWithDepth_default as n, common_default as r, detectType as i, directiveRegex as a, sanitizeDirective as o } from "./chunk-ICPOFSXX.js";
import s from "../../../../d3-selection/src/select.js";
import c from "../../../../d3-shape/src/curve/linear.js";
import { bumpX as l, bumpY as u } from "../../../../d3-shape/src/curve/bump.js";
import d from "../../../../d3-shape/src/curve/basis.js";
import f from "../../../../d3-shape/src/curve/basisClosed.js";
import ee from "../../../../d3-shape/src/curve/basisOpen.js";
import te from "../../../../d3-shape/src/curve/bundle.js";
import ne from "../../../../d3-shape/src/curve/cardinal.js";
import re from "../../../../d3-shape/src/curve/cardinalClosed.js";
import p from "../../../../d3-shape/src/curve/cardinalOpen.js";
import m from "../../../../d3-shape/src/curve/catmullRom.js";
import h from "../../../../d3-shape/src/curve/catmullRomClosed.js";
import g from "../../../../d3-shape/src/curve/catmullRomOpen.js";
import _ from "../../../../d3-shape/src/curve/linearClosed.js";
import { monotoneX as v, monotoneY as y } from "../../../../d3-shape/src/curve/monotone.js";
import b from "../../../../d3-shape/src/curve/natural.js";
import x, { stepAfter as S, stepBefore as C } from "../../../../d3-shape/src/curve/step.js";
import "../../../../d3/src/index.js";
import { require_dist as ie } from "../../../../@braintree/sanitize-url/dist/index.js";
import w from "../../../../lodash-es/memoize.js";
import ae from "../../../../lodash-es/merge.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/chunk-5PVQY5BW.mjs
var oe = ie(), se = {
	curveBasis: d,
	curveBasisClosed: f,
	curveBasisOpen: ee,
	curveBumpX: l,
	curveBumpY: u,
	curveBundle: te,
	curveCardinalClosed: re,
	curveCardinalOpen: p,
	curveCardinal: ne,
	curveCatmullRomClosed: h,
	curveCatmullRomOpen: g,
	curveCatmullRom: m,
	curveLinear: c,
	curveLinearClosed: _,
	curveMonotoneX: v,
	curveMonotoneY: y,
	curveNatural: b,
	curveStep: x,
	curveStepAfter: S,
	curveStepBefore: C
}, ce = /\s*(?:(\w+)(?=:):|(\w+))\s*(?:(\w+)|((?:(?!}%{2}).|\r?\n)*))?\s*(?:}%{2})?/gi, le = /* @__PURE__ */ e(function(e, t) {
	let r = T(e, /(?:init\b)|(?:initialize\b)/), a = {};
	if (Array.isArray(r)) {
		let e = r.map((e) => e.args);
		o(e), a = n(a, [...e]);
	} else a = r.args;
	if (!a) return;
	let s = i(e, t), c = "config";
	return a[c] !== void 0 && (s === "flowchart-v2" && (s = "flowchart"), a[s] = a[c], delete a[c]), a;
}, "detectInit"), T = /* @__PURE__ */ e(function(e, n = null) {
	try {
		let r = RegExp(`[%]{2}(?![{]${ce.source})(?=[}][%]{2}).*
`, "ig");
		e = e.trim().replace(r, "").replace(/'/gm, "\""), t.debug(`Detecting diagram directive${n === null ? "" : " type:" + n} based on the text:${e}`);
		let i, o = [];
		for (; (i = a.exec(e)) !== null;) if (i.index === a.lastIndex && a.lastIndex++, i && !n || n && i[1]?.match(n) || n && i[2]?.match(n)) {
			let e = i[1] ? i[1] : i[2], t = i[3] ? i[3].trim() : i[4] ? JSON.parse(i[4].trim()) : null;
			o.push({
				type: e,
				args: t
			});
		}
		return o.length === 0 ? {
			type: e,
			args: null
		} : o.length === 1 ? o[0] : o;
	} catch (r) {
		return t.error(`ERROR: ${r.message} - Unable to parse directive type: '${n}' based on the text: '${e}'`), {
			type: void 0,
			args: null
		};
	}
}, "detectDirective"), ue = /* @__PURE__ */ e(function(e) {
	return e.replace(a, "");
}, "removeDirectives"), de = /* @__PURE__ */ e(function(e, t) {
	for (let [n, r] of t.entries()) if (r.match(e)) return n;
	return -1;
}, "isSubstringInArray");
function E(e, t) {
	return e ? se[`curve${e.charAt(0).toUpperCase() + e.slice(1)}`] ?? t : t;
}
e(E, "interpolateToCurve");
function D(e, t) {
	let n = e.trim();
	if (n) return t.securityLevel === "loose" ? n : (0, oe.sanitizeUrl)(n);
}
e(D, "formatUrl");
var O = /* @__PURE__ */ e((e, ...n) => {
	let r = e.split("."), i = r.length - 1, a = r[i], o = window;
	for (let n = 0; n < i; n++) if (o = o[r[n]], !o) {
		t.error(`Function name: ${e} not found in window`);
		return;
	}
	o[a](...n);
}, "runFunc");
function k(e, t) {
	return !e || !t ? 0 : Math.sqrt((t.x - e.x) ** 2 + (t.y - e.y) ** 2);
}
e(k, "distance");
function A(e) {
	let t, n = 0;
	return e.forEach((e) => {
		n += k(e, t), t = e;
	}), N(e, n / 2);
}
e(A, "traverseEdge");
function j(e) {
	return e.length === 1 ? e[0] : A(e);
}
e(j, "calcLabelPosition");
var M = /* @__PURE__ */ e((e, t = 2) => {
	let n = 10 ** t;
	return Math.round(e * n) / n;
}, "roundNumber"), N = /* @__PURE__ */ e((e, t) => {
	let n, r = t;
	for (let t of e) {
		if (n) {
			let e = k(t, n);
			if (e === 0) return n;
			if (e < r) r -= e;
			else {
				let i = r / e;
				if (i <= 0) return n;
				if (i >= 1) return {
					x: t.x,
					y: t.y
				};
				if (i > 0 && i < 1) return {
					x: M((1 - i) * n.x + i * t.x, 5),
					y: M((1 - i) * n.y + i * t.y, 5)
				};
			}
		}
		n = t;
	}
	throw Error("Could not find a suitable point for the given distance");
}, "calculatePoint"), P = /* @__PURE__ */ e((e, n, r) => {
	t.info(`our points ${JSON.stringify(n)}`), n[0] !== r && (n = n.reverse());
	let i = N(n, 25), a = e ? 10 : 5, o = Math.atan2(n[0].y - i.y, n[0].x - i.x), s = {
		x: 0,
		y: 0
	};
	return s.x = Math.sin(o) * a + (n[0].x + i.x) / 2, s.y = -Math.cos(o) * a + (n[0].y + i.y) / 2, s;
}, "calcCardinalityPosition");
function F(e, n, r) {
	let i = structuredClone(r);
	t.info("our points", i), n !== "start_left" && n !== "start_right" && i.reverse();
	let a = N(i, 25 + e), o = 10 + e * .5, s = Math.atan2(i[0].y - a.y, i[0].x - a.x), c = {
		x: 0,
		y: 0
	};
	return n === "start_left" ? (c.x = Math.sin(s + Math.PI) * o + (i[0].x + a.x) / 2, c.y = -Math.cos(s + Math.PI) * o + (i[0].y + a.y) / 2) : n === "end_right" ? (c.x = Math.sin(s - Math.PI) * o + (i[0].x + a.x) / 2 - 5, c.y = -Math.cos(s - Math.PI) * o + (i[0].y + a.y) / 2 - 5) : n === "end_left" ? (c.x = Math.sin(s) * o + (i[0].x + a.x) / 2 - 5, c.y = -Math.cos(s) * o + (i[0].y + a.y) / 2 - 5) : (c.x = Math.sin(s) * o + (i[0].x + a.x) / 2, c.y = -Math.cos(s) * o + (i[0].y + a.y) / 2), c;
}
e(F, "calcTerminalLabelPosition");
function I(e) {
	let t = "", n = "";
	for (let r of e) r !== void 0 && (r.startsWith("color:") || r.startsWith("text-align:") ? n = n + r + ";" : t = t + r + ";");
	return {
		style: t,
		labelStyle: n
	};
}
e(I, "getStylesFromArray");
var L = 0, R = /* @__PURE__ */ e(() => (L++, "id-" + Math.random().toString(36).substr(2, 12) + "-" + L), "generateId");
function z(e) {
	let t = "";
	for (let n = 0; n < e; n++) t += "0123456789abcdef".charAt(Math.floor(Math.random() * 16));
	return t;
}
e(z, "makeRandomHex");
var B = /* @__PURE__ */ e((e) => z(e.length), "random"), V = /* @__PURE__ */ e(function() {
	return {
		x: 0,
		y: 0,
		fill: void 0,
		anchor: "start",
		style: "#666",
		width: 100,
		height: 100,
		textMargin: 0,
		rx: 0,
		ry: 0,
		valign: void 0,
		text: ""
	};
}, "getTextObj"), H = /* @__PURE__ */ e(function(e, t) {
	let n = t.text.replace(r.lineBreakRegex, " "), [, i] = Y(t.fontSize), a = e.append("text");
	a.attr("x", t.x), a.attr("y", t.y), a.style("text-anchor", t.anchor), a.style("font-family", t.fontFamily), a.style("font-size", i), a.style("font-weight", t.fontWeight), a.attr("fill", t.fill), t.class !== void 0 && a.attr("class", t.class);
	let o = a.append("tspan");
	return o.attr("x", t.x + t.textMargin * 2), o.attr("fill", t.fill), o.text(n), a;
}, "drawSimpleText"), U = w((e, t, n) => {
	if (!e || (n = Object.assign({
		fontSize: 12,
		fontWeight: 400,
		fontFamily: "Arial",
		joinWith: "<br/>"
	}, n), r.lineBreakRegex.test(e))) return e;
	let i = e.split(" ").filter(Boolean), a = [], o = "";
	return i.forEach((e, r) => {
		let s = G(`${e} `, n), c = G(o, n);
		if (s > t) {
			let { hyphenatedStrings: r, remainingWord: i } = fe(e, t, "-", n);
			a.push(o, ...r), o = i;
		} else c + s >= t ? (a.push(o), o = e) : o = [o, e].filter(Boolean).join(" ");
		r + 1 === i.length && a.push(o);
	}), a.filter((e) => e !== "").join(n.joinWith);
}, (e, t, n) => `${e}${t}${n.fontSize}${n.fontWeight}${n.fontFamily}${n.joinWith}`), fe = w((e, t, n = "-", r) => {
	r = Object.assign({
		fontSize: 12,
		fontWeight: 400,
		fontFamily: "Arial",
		margin: 0
	}, r);
	let i = [...e], a = [], o = "";
	return i.forEach((e, s) => {
		let c = `${o}${e}`;
		if (G(c, r) >= t) {
			let e = s + 1, t = i.length === e, r = `${c}${n}`;
			a.push(t ? c : r), o = "";
		} else o = c;
	}), {
		hyphenatedStrings: a,
		remainingWord: o
	};
}, (e, t, n = "-", r) => `${e}${t}${n}${r.fontSize}${r.fontWeight}${r.fontFamily}`);
function W(e, t) {
	return K(e, t).height;
}
e(W, "calculateTextHeight");
function G(e, t) {
	return K(e, t).width;
}
e(G, "calculateTextWidth");
var K = w((e, t) => {
	let { fontSize: n = 12, fontFamily: i = "Arial", fontWeight: a = 400 } = t;
	if (!e) return {
		width: 0,
		height: 0
	};
	let [, o] = Y(n), c = ["sans-serif", i], l = e.split(r.lineBreakRegex), u = [], d = s("body");
	if (!d.remove) return {
		width: 0,
		height: 0,
		lineHeight: 0
	};
	let f = d.append("svg");
	for (let e of c) {
		let t = 0, n = {
			width: 0,
			height: 0,
			lineHeight: 0
		};
		for (let r of l) {
			let i = V();
			i.text = r || "​";
			let s = H(f, i).style("font-size", o).style("font-weight", a).style("font-family", e), c = (s._groups || s)[0][0].getBBox();
			if (c.width === 0 && c.height === 0) throw Error("svg element not in render tree");
			n.width = Math.round(Math.max(n.width, c.width)), t = Math.round(c.height), n.height += t, n.lineHeight = Math.round(Math.max(n.lineHeight, t));
		}
		u.push(n);
	}
	return f.remove(), u[isNaN(u[1].height) || isNaN(u[1].width) || isNaN(u[1].lineHeight) || u[0].height > u[1].height && u[0].width > u[1].width && u[0].lineHeight > u[1].lineHeight ? 0 : 1];
}, (e, t) => `${e}${t.fontSize}${t.fontWeight}${t.fontFamily}`), pe = class {
	constructor(e = !1, t) {
		this.count = 0, this.count = t ? t.length : 0, this.next = e ? () => this.count++ : () => Date.now();
	}
	static {
		e(this, "InitIDGenerator");
	}
}, q, me = /* @__PURE__ */ e(function(e) {
	return q ||= document.createElement("div"), e = escape(e).replace(/%26/g, "&").replace(/%23/g, "#").replace(/%3B/g, ";"), q.innerHTML = e, unescape(q.textContent);
}, "entityDecode");
function J(e) {
	return "str" in e;
}
e(J, "isDetailedError");
var he = /* @__PURE__ */ e((e, t, n, r) => {
	if (!r) return;
	let i = e.node()?.getBBox();
	i && e.append("text").text(r).attr("text-anchor", "middle").attr("x", i.x + i.width / 2).attr("y", -n).attr("class", t);
}, "insertTitle"), Y = /* @__PURE__ */ e((e) => {
	if (typeof e == "number") return [e, e + "px"];
	let t = parseInt(e ?? "", 10);
	return Number.isNaN(t) ? [void 0, void 0] : e === String(t) ? [t, e + "px"] : [t, e];
}, "parseFontSize");
function X(e, t) {
	return ae({}, e, t);
}
e(X, "cleanAndMerge");
var ge = {
	assignWithDepth: n,
	wrapLabel: U,
	calculateTextHeight: W,
	calculateTextWidth: G,
	calculateTextDimensions: K,
	cleanAndMerge: X,
	detectInit: le,
	detectDirective: T,
	isSubstringInArray: de,
	interpolateToCurve: E,
	calcLabelPosition: j,
	calcCardinalityPosition: P,
	calcTerminalLabelPosition: F,
	formatUrl: D,
	getStylesFromArray: I,
	generateId: R,
	random: B,
	runFunc: O,
	entityDecode: me,
	insertTitle: he,
	isLabelCoordinateInPath: $,
	parseFontSize: Y,
	InitIDGenerator: pe
}, _e = /* @__PURE__ */ e(function(e) {
	let t = e;
	return t = t.replace(/style.*:\S*#.*;/g, function(e) {
		return e.substring(0, e.length - 1);
	}), t = t.replace(/classDef.*:\S*#.*;/g, function(e) {
		return e.substring(0, e.length - 1);
	}), t = t.replace(/#\w+;/g, function(e) {
		let t = e.substring(1, e.length - 1);
		return /^\+?\d+$/.test(t) ? "ﬂ°°" + t + "¶ß" : "ﬂ°" + t + "¶ß";
	}), t;
}, "encodeEntities"), ve = /* @__PURE__ */ e(function(e) {
	return e.replace(/ﬂ°°/g, "&#").replace(/ﬂ°/g, "&").replace(/¶ß/g, ";");
}, "decodeEntities"), Z = /* @__PURE__ */ e((e, t, { counter: n = 0, prefix: r, suffix: i }, a) => a || `${r ? `${r}_` : ""}${e}_${t}_${n}${i ? `_${i}` : ""}`, "getEdgeId");
function Q(e) {
	return e ?? null;
}
e(Q, "handleUndefinedAttr");
function $(e, t) {
	let n = Math.round(e.x), r = Math.round(e.y), i = t.replace(/(\d+\.\d+)/g, (e) => Math.round(parseFloat(e)).toString());
	return i.includes(n.toString()) || i.includes(r.toString());
}
e($, "isLabelCoordinateInPath");
//#endregion
export { W as calculateTextHeight, G as calculateTextWidth, X as cleanAndMerge, ve as decodeEntities, _e as encodeEntities, R as generateId, Z as getEdgeId, I as getStylesFromArray, Q as handleUndefinedAttr, E as interpolateToCurve, J as isDetailedError, Y as parseFontSize, B as random, ue as removeDirectives, ge as utils_default, U as wrapLabel };
