import { vars as e } from "../../../../theme/themeContract.js";
import { colors as t } from "../../../../theme/colors.js";
import { useUiTranslation as n } from "../../../../i18n/useUiTranslation.js";
import { HighlightCode as r } from "../../highlight/HighlightCode.js";
import { diagram as i, diagramSvg as a, error as o, errorCode as s, errorTitle as c } from "./MarkdownMermaidBlock.css.js";
import { useEffect as l, useId as u, useMemo as d, useRef as f, useState as p } from "react";
import { jsx as m, jsxs as h } from "react/jsx-runtime";
//#region src/atomic/molecules/markdown/components/MarkdownMermaidBlock.tsx
var g = /^var\((--[^,)+]+)(?:,[^)]+)?\)$/, _ = (e, t) => {
	let n = e.trim(), r = g.exec(n);
	if (r == null) return n === "" ? t : n;
	let i = r[1];
	if (i == null || i === "" || typeof window > "u") return t;
	let a = getComputedStyle(document.documentElement).getPropertyValue(i).trim();
	return a === "" ? t : a;
}, v = () => {
	let n = {
		background: "transparent",
		fontFamily: _(e.fontFamily.sans),
		lineColor: _(e.colors.brandDarkGray, t.brandDarkGray),
		primaryBorderColor: _(e.colors.primaryLight, t.primaryLight),
		primaryColor: _(e.colors.primaryLight, t.primaryLight),
		primaryTextColor: _(e.colors.brandDarkGray, t.brandDarkGray)
	};
	return {
		startOnLoad: !1,
		securityLevel: "strict",
		suppressErrorRendering: !0,
		theme: "base",
		themeVariables: Object.fromEntries(Object.entries(n).filter(([, e]) => e != null && e !== ""))
	};
}, y = null, b = !1, x = async () => {
	y ??= import("../../../../node_modules/mermaid/dist/mermaid.core.js").then((e) => e.default);
	let e = await y;
	return b ||= (e.initialize(v()), !0), e;
}, S = /<svg\b([^>]*)>/i, C = /\bclass="([^"]*)"/i, w = (e, t) => e.replace(S, (e, n) => {
	let r = String(n ?? "");
	return C.test(r) ? `<svg${r.replace(C, (e, n) => ` class="${n} ${t}"`)}>` : `<svg class="${t}"${r}>`;
}), T = ({ code: e }) => {
	let { t } = n(), g = u(), _ = d(() => `mermaid-${g.replace(/:/g, "")}`, [g]), v = f(null), y = f(null), [b, S] = p(null), [C, T] = p(null), [E, D] = p(!0);
	l(() => {
		let n = !1;
		return y.current = null, S(null), T(null), D(!0), e.trim() === "" ? (T(t("markdown.mermaid.empty")), D(!1), () => {
			n = !0;
		}) : ((async () => {
			try {
				let t = await (await x()).render(_, e);
				if (n) return;
				y.current = t.bindFunctions ?? null, S(w(t.svg, a));
			} catch {
				n || T(t("markdown.mermaid.renderFailed"));
			} finally {
				n || D(!1);
			}
		})().catch(() => {
			n || (T(t("markdown.mermaid.renderFailed")), D(!1));
		}), () => {
			n = !0;
		});
	}, [
		e,
		_,
		t
	]), l(() => {
		let e = v.current, t = y.current;
		e == null || t == null || t(e);
	}, [b]);
	let O = null;
	E && (O = t("markdown.mermaid.rendering"));
	let k = null;
	return b == null ? C != null && (k = /* @__PURE__ */ h("div", {
		className: o,
		children: [/* @__PURE__ */ m("p", {
			className: c,
			children: C
		}), /* @__PURE__ */ m("pre", {
			className: s,
			children: e
		})]
	})) : k = /* @__PURE__ */ m("div", {
		className: i,
		ref: v,
		role: "img",
		"aria-label": t("markdown.mermaid.diagramLabel"),
		dangerouslySetInnerHTML: { __html: b }
	}), /* @__PURE__ */ m(r, {
		badgeLabel: "MERMAID",
		copyCode: e,
		highlighted: k,
		fallbackCodeText: e,
		placeholderText: O
	});
};
//#endregion
export { T as MarkdownMermaidBlock, T as default };

//# sourceMappingURL=MarkdownMermaidBlock.js.map