import { fromHtmlIsomorphic as e } from "../../hast-util-from-html-isomorphic/lib/browser.js";
import { toText as t } from "../../hast-util-to-text/lib/index.js";
import n from "../../katex/dist/katex.js";
import { SKIP as r, visitParents as i } from "../../unist-util-visit-parents/lib/index.js";
//#region ../../node_modules/rehype-katex/lib/index.js
var a = {}, o = [];
function s(s) {
	let c = s || a;
	return function(a, s) {
		i(a, "element", function(i, a) {
			let l = Array.isArray(i.properties.className) ? i.properties.className : o, u = l.includes("language-math"), d = l.includes("math-display"), f = l.includes("math-inline"), p = d;
			if (!u && !d && !f) return;
			let m = a[a.length - 1], h = i;
			/* c8 ignore next -- verbose to test. */
			if (i.tagName === "code" && u && m && m.type === "element" && m.tagName === "pre" && (h = m, m = a[a.length - 2], p = !0), !m) return;
			let g = t(h, { whitespace: "pre" }), _;
			try {
				_ = n.renderToString(g, {
					...c,
					displayMode: p,
					throwOnError: !0
				});
			} catch (e) {
				let t = e, r = t.name.toLowerCase();
				s.message("Could not render math with KaTeX", {
					ancestors: [...a, i],
					cause: t,
					place: i.position,
					ruleId: r,
					source: "rehype-katex"
				});
				try {
					_ = n.renderToString(g, {
						...c,
						displayMode: p,
						strict: "ignore",
						throwOnError: !1
					});
				} catch {
					_ = [{
						type: "element",
						tagName: "span",
						properties: {
							className: ["katex-error"],
							style: "color:" + (c.errorColor || "#cc0000"),
							title: String(e)
						},
						children: [{
							type: "text",
							value: g
						}]
					}];
				}
			}
			typeof _ == "string" && (_ = e(_, { fragment: !0 }).children);
			let v = m.children.indexOf(h);
			return m.children.splice(v, 1, ..._), r;
		});
	};
}
//#endregion
export { s as default };
