import { __name as e, log as t } from "./chunk-AGHRB4JF.js";
import { common_default as n, getConfig as r, hasKatex as i, renderKatexSanitized as a, sanitizeText as o } from "./chunk-ICPOFSXX.js";
import s from "../../../../d3-selection/src/select.js";
import "../../../../d3/src/index.js";
import { decodeEntities as c } from "./chunk-5PVQY5BW.js";
import { stringToIcon as l } from "../../../../@iconify/utils/lib/icon/name.js";
import { getIconData as u } from "../../../../@iconify/utils/lib/icon-set/get-icon.js";
import { iconToSVG as d } from "../../../../@iconify/utils/lib/svg/build.js";
import { replaceIDs as f } from "../../../../@iconify/utils/lib/svg/id.js";
import { iconToHTML as p } from "../../../../@iconify/utils/lib/svg/html.js";
import { k as m } from "../../../../marked/lib/marked.esm.js";
import h from "../../../../ts-dedent/esm/index.js";
//#region ../../node_modules/mermaid/dist/chunks/mermaid.core/chunk-U2HBQHQK.mjs
var g = {
	body: "<g><rect width=\"80\" height=\"80\" style=\"fill: #087ebf; stroke-width: 0px;\"/><text transform=\"translate(21.16 64.67)\" style=\"fill: #fff; font-family: ArialMT, Arial; font-size: 67.75px;\"><tspan x=\"0\" y=\"0\">?</tspan></text></g>",
	height: 80,
	width: 80
}, _ = /* @__PURE__ */ new Map(), v = /* @__PURE__ */ new Map(), y = /* @__PURE__ */ e((e) => {
	for (let n of e) {
		if (!n.name) throw Error("Invalid icon loader. Must have a \"name\" property with non-empty string value.");
		if (t.debug("Registering icon pack:", n.name), "loader" in n) v.set(n.name, n.loader);
		else if ("icons" in n) _.set(n.name, n.icons);
		else throw t.error("Invalid icon loader:", n), Error("Invalid icon loader. Must have either \"icons\" or \"loader\" property.");
	}
}, "registerIconPacks"), b = /* @__PURE__ */ e(async (e, n) => {
	let r = l(e, !0, n !== void 0);
	if (!r) throw Error(`Invalid icon name: ${e}`);
	let i = r.prefix || n;
	if (!i) throw Error(`Icon name must contain a prefix: ${e}`);
	let a = _.get(i);
	if (!a) {
		let e = v.get(i);
		if (!e) throw Error(`Icon set not found: ${r.prefix}`);
		try {
			a = {
				...await e(),
				prefix: i
			}, _.set(i, a);
		} catch (e) {
			throw t.error(e), Error(`Failed to load icon set: ${r.prefix}`);
		}
	}
	let o = u(a, r.name);
	if (!o) throw Error(`Icon not found: ${e}`);
	return o;
}, "getRegisteredIconData"), x = /* @__PURE__ */ e(async (e) => {
	try {
		return await b(e), !0;
	} catch {
		return !1;
	}
}, "isIconAvailable"), S = /* @__PURE__ */ e(async (e, n, i) => {
	let a;
	try {
		a = await b(e, n?.fallbackPrefix);
	} catch (e) {
		t.error(e), a = g;
	}
	let s = d(a, n);
	return o(p(f(s.body), {
		...s.attributes,
		...i
	}), r());
}, "getIconSVG");
function C(e, { markdownAutoWrap: t }) {
	return h(e.replace(/<br\/>/g, "\n").replace(/\n{2,}/g, "\n"));
}
e(C, "preprocessMarkdown");
function w(e) {
	return e.split(/\\n|\n|<br\s*\/?>/gi).map((e) => e.trim().match(/<[^>]+>|[^\s<>]+/g)?.map((e) => ({
		content: e,
		type: "normal"
	})) ?? []);
}
e(w, "nonMarkdownToLines");
function T(t, n = {}) {
	let r = C(t, n), i = m.lexer(r), a = [[]], o = 0;
	function s(e, t = "normal") {
		e.type === "text" ? e.text.split("\n").forEach((e, n) => {
			n !== 0 && (o++, a.push([])), e.split(" ").forEach((e) => {
				e = e.replace(/&#39;/g, "'"), e && a[o].push({
					content: e,
					type: t
				});
			});
		}) : e.type === "strong" || e.type === "em" ? e.tokens.forEach((t) => {
			s(t, e.type);
		}) : e.type === "html" && a[o].push({
			content: e.text,
			type: "normal"
		});
	}
	return e(s, "processNode"), i.forEach((e) => {
		e.type === "paragraph" ? e.tokens?.forEach((e) => {
			s(e);
		}) : e.type === "html" ? a[o].push({
			content: e.text,
			type: "normal"
		}) : a[o].push({
			content: e.raw,
			type: "normal"
		});
	}), a;
}
e(T, "markdownToLines");
function E(e) {
	return e ? `<p>${e.replace(/\\n|\n/g, "<br />")}</p>` : "";
}
e(E, "nonMarkdownToHTML");
function D(n, { markdownAutoWrap: r } = {}) {
	let i = m.lexer(n);
	function a(e) {
		return e.type === "text" ? r === !1 ? e.text.replace(/\n */g, "<br/>").replace(/ /g, "&nbsp;") : e.text.replace(/\n */g, "<br/>") : e.type === "strong" ? `<strong>${e.tokens?.map(a).join("")}</strong>` : e.type === "em" ? `<em>${e.tokens?.map(a).join("")}</em>` : e.type === "paragraph" ? `<p>${e.tokens?.map(a).join("")}</p>` : e.type === "space" ? "" : e.type === "html" ? `${e.text}` : e.type === "escape" ? e.text : (t.warn(`Unsupported markdown: ${e.type}`), e.raw);
	}
	return e(a, "output"), i.map(a).join("");
}
e(D, "markdownToHTML");
function O(e) {
	return Intl.Segmenter ? [...new Intl.Segmenter().segment(e)].map((e) => e.segment) : [...e];
}
e(O, "splitTextToChars");
function k(e, t) {
	return A(e, [], O(t.content), t.type);
}
e(k, "splitWordToFitWidth");
function A(e, t, n, r) {
	if (n.length === 0) return [{
		content: t.join(""),
		type: r
	}, {
		content: "",
		type: r
	}];
	let [i, ...a] = n, o = [...t, i];
	return e([{
		content: o.join(""),
		type: r
	}]) ? A(e, o, a, r) : (t.length === 0 && i && (t.push(i), n.shift()), [{
		content: t.join(""),
		type: r
	}, {
		content: n.join(""),
		type: r
	}]);
}
e(A, "splitWordToFitWidthRecursion");
function j(e, t) {
	if (e.some(({ content: e }) => e.includes("\n"))) throw Error("splitLineToFitWidth does not support newlines in the line");
	return M(e, t);
}
e(j, "splitLineToFitWidth");
function M(e, t, n = [], r = []) {
	if (e.length === 0) return r.length > 0 && n.push(r), n.length > 0 ? n : [];
	let i = "";
	e[0].content === " " && (i = " ", e.shift());
	let a = e.shift() ?? {
		content: " ",
		type: "normal"
	}, o = [...r];
	if (i !== "" && o.push({
		content: i,
		type: "normal"
	}), o.push(a), t(o)) return M(e, t, n, o);
	if (r.length > 0) n.push(r), e.unshift(a);
	else if (a.content) {
		let [r, i] = k(t, a);
		n.push([r]), i.content && e.unshift(i);
	}
	return M(e, t, n);
}
e(M, "splitLineToFitWidthRecursion");
function N(e, t) {
	t && e.attr("style", t);
}
e(N, "applyStyle");
var P = 16384;
async function F(e, t, s, c, l = !1, u = r()) {
	let d = e.append("foreignObject");
	d.attr("width", `${Math.min(10 * s, P)}px`), d.attr("height", `${Math.min(10 * s, P)}px`);
	let f = d.append("xhtml:div"), p = i(t.label) ? await a(t.label.replace(n.lineBreakRegex, "\n"), u) : o(t.label, u), m = t.isNode ? "nodeLabel" : "edgeLabel", h = f.append("span");
	h.html(p), N(h, t.labelStyle), h.attr("class", `${m} ${c}`), N(f, t.labelStyle), f.style("display", "table-cell"), f.style("white-space", "nowrap"), f.style("line-height", "1.5"), s !== Infinity && (f.style("max-width", s + "px"), f.style("text-align", "center")), f.attr("xmlns", "http://www.w3.org/1999/xhtml"), l && f.attr("class", "labelBkg");
	let g = f.node().getBoundingClientRect();
	return g.width === s && (f.style("display", "table"), f.style("white-space", "break-spaces"), f.style("width", s + "px"), g = f.node().getBoundingClientRect()), d.node();
}
e(F, "addHtmlSpan");
function I(e, t, n, r = !1) {
	let i = e.append("tspan").attr("class", "text-outer-tspan").attr("x", 0).attr("y", t * n - .1 + "em").attr("dy", n + "em");
	return r && i.attr("text-anchor", "middle"), i;
}
e(I, "createTspan");
function L(e, t, n) {
	let r = e.append("text"), i = I(r, 1, t);
	V(i, n);
	let a = i.node().getComputedTextLength();
	return r.remove(), a;
}
e(L, "computeWidthOfText");
function R(e, t, n) {
	let r = e.append("text"), i = I(r, 1, t);
	V(i, [{
		content: n,
		type: "normal"
	}]);
	let a = i.node()?.getBoundingClientRect();
	return a && r.remove(), a;
}
e(R, "computeDimensionOfText");
function z(t, n, r, i = !1, a = !1) {
	let o = 1.1, s = n.append("g"), c = s.insert("rect").attr("class", "background").attr("style", "stroke: none"), l = s.append("text").attr("y", "-10.1");
	a && l.attr("text-anchor", "middle");
	let u = 0;
	for (let n of r) {
		let r = /* @__PURE__ */ e((e) => L(s, o, e) <= t, "checkWidth"), i = r(n) ? [n] : j(n, r);
		for (let e of i) V(I(l, u, o, a), e), u++;
	}
	if (i) {
		let e = l.node().getBBox();
		return c.attr("x", e.x - 2).attr("y", e.y - 2).attr("width", e.width + 4).attr("height", e.height + 4), s.node();
	} else return l.node();
}
e(z, "createFormattedText");
function B(e) {
	return e.replace(/&(amp|lt|gt);/g, (e, t) => {
		switch (t) {
			case "amp": return "&";
			case "lt": return "<";
			case "gt": return ">";
			default: return e;
		}
	});
}
e(B, "decodeHTMLEntities");
function V(e, t) {
	e.text(""), t.forEach((t, n) => {
		let r = e.append("tspan").attr("font-style", t.type === "em" ? "italic" : "normal").attr("class", "text-inner-tspan").attr("font-weight", t.type === "strong" ? "bold" : "normal");
		n === 0 ? r.text(B(t.content)) : r.text(" " + B(t.content));
	});
}
e(V, "updateTextContentAndStyles");
async function H(e, t = {}) {
	let n = [];
	e.replace(/(fa[bklrs]?):fa-([\w-]+)/g, (e, r, i) => (n.push((async () => {
		let n = `${r}:${i}`;
		return await x(n) ? await S(n, void 0, { class: "label-icon" }) : `<i class='${o(e, t).replace(":", " ")}'></i>`;
	})()), e));
	let r = await Promise.all(n);
	return e.replace(/(fa[bklrs]?):fa-([\w-]+)/g, () => r.shift() ?? "");
}
e(H, "replaceIconSubstring");
var U = /* @__PURE__ */ e(async (e, n = "", { style: r = "", isTitle: a = !1, classes: o = "", useHtmlLabels: l = !0, markdown: u = !0, isNode: d = !0, width: f = 200, addSvgBackground: p = !1 } = {}, m) => {
	if (t.debug("XYZ createText", n, r, a, o, l, d, "addSvgBackground: ", p), l) {
		let t = await H(c(u ? D(n, m) : E(n)), m), a = n.replace(/\\\\/g, "\\");
		return await F(e, {
			isNode: d,
			label: i(n) ? a : t,
			labelStyle: r.replace("fill:", "color:")
		}, f, o, p, m);
	} else {
		let t = c(n.replace(/<br\s*\/?>/g, "<br/>")), i = z(f, e, u ? T(t.replace("<br>", "<br/>"), m) : w(t), n ? p : !1, !d);
		if (d) {
			/stroke:/.exec(r) && (r = r.replace("stroke:", "lineColor:"));
			let e = r.replace(/stroke:[^;]+;?/g, "").replace(/stroke-width:[^;]+;?/g, "").replace(/fill:[^;]+;?/g, "").replace(/color:/g, "fill:");
			s(i).attr("style", e);
		} else {
			let e = r.replace(/stroke:[^;]+;?/g, "").replace(/stroke-width:[^;]+;?/g, "").replace(/fill:[^;]+;?/g, "").replace(/background:/g, "fill:");
			s(i).select("rect").attr("style", e.replace(/background:/g, "fill:"));
			let t = r.replace(/stroke:[^;]+;?/g, "").replace(/stroke-width:[^;]+;?/g, "").replace(/fill:[^;]+;?/g, "").replace(/color:/g, "fill:");
			s(i).select("text").attr("style", t);
		}
		return a ? s(i).selectAll("tspan.text-outer-tspan").classed("title-row", !0) : s(i).selectAll("tspan.text-outer-tspan").classed("row", !0), i;
	}
}, "createText");
//#endregion
export { R as computeDimensionOfText, U as createText, S as getIconSVG, y as registerIconPacks, g as unknownIcon };
