//#region ../../node_modules/remend/dist/index.js
var e = Object.defineProperty, t = Object.defineProperties, n = Object.getOwnPropertyDescriptors, r = Object.getOwnPropertySymbols, i = Object.prototype.hasOwnProperty, a = Object.prototype.propertyIsEnumerable, o = (t, n, r) => n in t ? e(t, n, {
	enumerable: !0,
	configurable: !0,
	writable: !0,
	value: r
}) : t[n] = r, s = (e, t) => {
	for (var n in t ||= {}) i.call(t, n) && o(e, n, t[n]);
	if (r) for (var n of r(t)) a.call(t, n) && o(e, n, t[n]);
	return e;
}, c = (e, r) => t(e, n(r)), l = (e, t) => {
	let n = !1, r = !1;
	for (let i = 0; i < t; i += 1) {
		if (e[i] === "\\" && i + 1 < e.length && e[i + 1] === "`") {
			i += 1;
			continue;
		}
		if (e.substring(i, i + 3) === "```") {
			r = !r, i += 2;
			continue;
		}
		!r && e[i] === "`" && (n = !n);
	}
	return n || r;
}, ee = (e, t) => {
	let n = e.substring(t, t + 3) === "```", r = t > 0 && e.substring(t - 1, t + 2) === "```", i = t > 1 && e.substring(t - 2, t + 1) === "```";
	return n || r || i;
}, u = (e) => {
	let t = 0;
	for (let n = 0; n < e.length; n += 1) {
		if (e[n] === "\\" && n + 1 < e.length && e[n + 1] === "`") {
			n += 1;
			continue;
		}
		e[n] === "`" && !ee(e, n) && (t += 1);
	}
	return t;
}, d = (e, t) => {
	let n = !1, r = !1, i = -1;
	for (let a = 0; a < e.length; a += 1) {
		if (e[a] === "\\" && a + 1 < e.length && e[a + 1] === "`") {
			a += 1;
			continue;
		}
		if (e.substring(a, a + 3) === "```") {
			r = !r, a += 2;
			continue;
		}
		if (!r && e[a] === "`") if (n) {
			if (i < t && t < a) return !0;
			n = !1, i = -1;
		} else n = !0, i = a;
	}
	return !1;
}, te = /^(\s*(?:[-*+]|\d+[.)]) +)>(=?\s*[$]?\d)/gm, f = (e) => !e || typeof e != "string" || !e.includes(">") ? e : e.replace(te, (t, n, r, i) => l(e, i) ? t : `${n}\\>${r}`), p = /(\*\*)([^*]*\*?)$/, m = /(__)([^_]*?)$/, h = /(\*\*\*)([^*]*?)$/, g = /(\*)([^*]*?)$/, _ = /(_)([^_]*?)$/, v = /(`)([^`]*?)$/, y = /(~~)([^~]*?)$/, b = /^[\s_~*`]*$/, x = /^[\s]*[-*+][\s]+$/, ne = /[\p{L}\p{N}_]/u, re = /^```[^`\n]*```?$/, ie = /^\*{4,}$/, ae = /(__)([^_]+)_$/, oe = /(~~)([^~]+)~$/, S = /~~/g, C = (e) => {
	if (!e) return !1;
	let t = e.charCodeAt(0);
	return t >= 48 && t <= 57 || t >= 65 && t <= 90 || t >= 97 && t <= 122 || t === 95 ? !0 : ne.test(e);
}, w = (e, t) => {
	let n = 1;
	for (let r = t - 1; r >= 0; --r) if (e[r] === "]") n += 1;
	else if (e[r] === "[" && (--n, n === 0)) return r;
	return -1;
}, T = (e, t) => {
	let n = 1;
	for (let r = t + 1; r < e.length; r += 1) if (e[r] === "[") n += 1;
	else if (e[r] === "]" && (--n, n === 0)) return r;
	return -1;
}, E = (e, t) => {
	let n = !1, r = !1;
	for (let i = 0; i < e.length && i < t; i += 1) {
		if (e[i] === "\\" && e[i + 1] === "$") {
			i += 1;
			continue;
		}
		e[i] === "$" && (e[i + 1] === "$" ? (r = !r, i += 1, n = !1) : r || (n = !n));
	}
	return n || r;
}, D = (e, t) => {
	for (let n = t; n < e.length; n += 1) {
		if (e[n] === ")") return !0;
		if (e[n] === "\n") return !1;
	}
	return !1;
}, O = (e, t) => {
	for (let n = t - 1; n >= 0; --n) {
		if (e[n] === ")") return !1;
		if (e[n] === "(") return n > 0 && e[n - 1] === "]" ? D(e, t) : !1;
		if (e[n] === "\n") return !1;
	}
	return !1;
}, k = (e, t) => {
	for (let n = t - 1; n >= 0; --n) {
		if (e[n] === ">") return !1;
		if (e[n] === "<") {
			let t = n + 1 < e.length ? e[n + 1] : "";
			return t >= "a" && t <= "z" || t >= "A" && t <= "Z" || t === "/";
		}
		if (e[n] === "\n") return !1;
	}
	return !1;
}, A = (e, t, n) => {
	let r = 0;
	for (let n = t - 1; n >= 0; --n) if (e[n] === "\n") {
		r = n + 1;
		break;
	}
	let i = e.length;
	for (let n = t; n < e.length; n += 1) if (e[n] === "\n") {
		i = n;
		break;
	}
	let a = e.substring(r, i), o = 0, s = !1;
	for (let e of a) if (e === n) o += 1;
	else if (e !== " " && e !== "	") {
		s = !0;
		break;
	}
	return o >= 3 && !s;
}, j = (e, t, n, r) => n === "\\" || e.includes("$") && E(e, t) ? !0 : n !== "*" && r === "*" ? (t < e.length - 2 ? e[t + 2] : "") !== "*" : !!(n === "*" || n && r && C(n) && C(r) || (!n || n === " " || n === "	" || n === "\n") && (!r || r === " " || r === "	" || r === "\n")), M = (e) => {
	let t = 0, n = !1, r = e.length;
	for (let i = 0; i < r; i += 1) {
		if (e[i] === "`" && i + 2 < r && e[i + 1] === "`" && e[i + 2] === "`") {
			n = !n, i += 2;
			continue;
		}
		if (n || e[i] !== "*") continue;
		let a = i > 0 ? e[i - 1] : "", o = i < r - 1 ? e[i + 1] : "";
		j(e, i, a, o) || (t += 1);
	}
	return t;
}, N = (e, t, n, r) => !!(n === "\\" || e.includes("$") && E(e, t) || O(e, t) || k(e, t) || n === "_" || r === "_" || n && r && C(n) && C(r)), P = (e) => {
	let t = 0, n = !1, r = e.length;
	for (let i = 0; i < r; i += 1) {
		if (e[i] === "`" && i + 2 < r && e[i + 1] === "`" && e[i + 2] === "`") {
			n = !n, i += 2;
			continue;
		}
		if (n || e[i] !== "_") continue;
		let a = i > 0 ? e[i - 1] : "", o = i < r - 1 ? e[i + 1] : "";
		N(e, i, a, o) || (t += 1);
	}
	return t;
}, F = (e) => {
	let t = 0, n = 0, r = !1;
	for (let i = 0; i < e.length; i += 1) {
		if (e[i] === "`" && i + 2 < e.length && e[i + 1] === "`" && e[i + 2] === "`") {
			n >= 3 && (t += Math.floor(n / 3)), n = 0, r = !r, i += 2;
			continue;
		}
		r || (e[i] === "*" ? n += 1 : (n >= 3 && (t += Math.floor(n / 3)), n = 0));
	}
	return n >= 3 && (t += Math.floor(n / 3)), t;
}, I = (e) => {
	let t = 0, n = !1;
	for (let r = 0; r < e.length; r += 1) {
		if (e[r] === "`" && r + 2 < e.length && e[r + 1] === "`" && e[r + 2] === "`") {
			n = !n, r += 2;
			continue;
		}
		n || e[r] === "*" && r + 1 < e.length && e[r + 1] === "*" && (t += 1, r += 1);
	}
	return t;
}, L = (e) => {
	let t = 0, n = !1;
	for (let r = 0; r < e.length; r += 1) {
		if (e[r] === "`" && r + 2 < e.length && e[r + 1] === "`" && e[r + 2] === "`") {
			n = !n, r += 2;
			continue;
		}
		n || e[r] === "_" && r + 1 < e.length && e[r + 1] === "_" && (t += 1, r += 1);
	}
	return t;
}, R = (e, t, n) => {
	if (!t || b.test(t)) return !0;
	let r = e.substring(0, n).lastIndexOf("\n"), i = r === -1 ? 0 : r + 1, a = e.substring(i, n);
	return x.test(a) && t.includes("\n") ? !0 : A(e, n, "*");
}, z = (e) => {
	let t = e.match(p);
	if (!t) return e;
	let n = t[2], r = e.lastIndexOf(t[1]);
	return l(e, r) || d(e, r) || R(e, n, r) ? e : I(e) % 2 == 1 ? n.endsWith("*") ? `${e}*` : `${e}**` : e;
}, B = (e, t, n) => {
	if (!t || b.test(t)) return !0;
	let r = e.substring(0, n).lastIndexOf("\n"), i = r === -1 ? 0 : r + 1, a = e.substring(i, n);
	return x.test(a) && t.includes("\n") ? !0 : A(e, n, "_");
}, V = (e) => {
	let t = e.match(m);
	if (!t) {
		let t = e.match(ae);
		if (t) {
			let n = e.lastIndexOf(t[1]);
			if (!(l(e, n) || d(e, n)) && L(e) % 2 == 1) return `${e}_`;
		}
		return e;
	}
	let n = t[2], r = e.lastIndexOf(t[1]);
	return l(e, r) || d(e, r) || B(e, n, r) ? e : L(e) % 2 == 1 ? `${e}__` : e;
}, H = (e) => {
	let t = !1;
	for (let n = 0; n < e.length; n += 1) {
		if (e[n] === "`" && n + 2 < e.length && e[n + 1] === "`" && e[n + 2] === "`") {
			t = !t, n += 2;
			continue;
		}
		if (!t && e[n] === "*" && e[n - 1] !== "*" && e[n + 1] !== "*" && e[n - 1] !== "\\" && !E(e, n)) {
			let t = n > 0 ? e[n - 1] : "", r = n < e.length - 1 ? e[n + 1] : "";
			if ((!t || t === " " || t === "	" || t === "\n") && (!r || r === " " || r === "	" || r === "\n") || t && r && C(t) && C(r)) continue;
			return n;
		}
	}
	return -1;
}, U = (e) => {
	if (!e.match(g)) return e;
	let t = H(e);
	if (t === -1 || l(e, t) || d(e, t)) return e;
	let n = e.substring(t + 1);
	return !n || b.test(n) ? e : M(e) % 2 == 1 ? `${e}*` : e;
}, W = (e) => {
	let t = !1;
	for (let n = 0; n < e.length; n += 1) {
		if (e[n] === "`" && n + 2 < e.length && e[n + 1] === "`" && e[n + 2] === "`") {
			t = !t, n += 2;
			continue;
		}
		if (!t && e[n] === "_" && e[n - 1] !== "_" && e[n + 1] !== "_" && e[n - 1] !== "\\" && !E(e, n) && !O(e, n)) {
			let t = n > 0 ? e[n - 1] : "", r = n < e.length - 1 ? e[n + 1] : "";
			if (t && r && C(t) && C(r)) continue;
			return n;
		}
	}
	return -1;
}, G = (e) => {
	let t = e.length;
	for (; t > 0 && e[t - 1] === "\n";) --t;
	return t < e.length ? `${e.slice(0, t)}_${e.slice(t)}` : `${e}_`;
}, K = (e) => {
	if (!e.endsWith("**")) return null;
	let t = e.slice(0, -2);
	if (I(t) % 2 != 1) return null;
	let n = t.indexOf("**"), r = W(t);
	return n !== -1 && r !== -1 && n < r ? `${t}_**` : null;
}, q = (e) => {
	if (!e.match(_)) return e;
	let t = W(e);
	if (t === -1) return e;
	let n = e.substring(t + 1);
	if (!n || b.test(n) || l(e, t) || d(e, t)) return e;
	if (P(e) % 2 == 1) {
		let t = K(e);
		return t === null ? G(e) : t;
	}
	return e;
}, se = (e) => {
	let t = I(e), n = M(e);
	return t % 2 == 0 && n % 2 == 0;
}, ce = (e, t, n) => !t || b.test(t) || l(e, n) || d(e, n) ? !0 : A(e, n, "*"), le = (e) => {
	if (ie.test(e)) return e;
	let t = e.match(h);
	if (!t) return e;
	let n = t[2];
	return ce(e, n, e.lastIndexOf(t[1])) ? e : F(e) % 2 == 1 ? se(e) ? e : `${e}***` : e;
}, ue = /<[a-zA-Z/][^>]*$/, de = (e) => {
	let t = e.match(ue);
	return !t || t.index === void 0 || l(e, t.index) ? e : e.substring(0, t.index).trimEnd();
}, fe = (e) => !e.match(re) || e.includes("\n") ? null : e.endsWith("``") && !e.endsWith("```") ? `${e}\`` : e, pe = (e) => (e.match(/```/g) || []).length % 2 == 1, me = (e) => {
	let t = fe(e);
	if (t !== null) return t;
	let n = e.match(v);
	if (n && !pe(e)) {
		let t = n[2];
		if (!t || b.test(t)) return e;
		if (u(e) % 2 == 1) return `${e}\``;
	}
	return e;
}, J = (e, t) => t >= 2 && e.substring(t - 2, t + 1) === "```" || t >= 1 && e.substring(t - 1, t + 2) === "```" || t <= e.length - 3 && e.substring(t, t + 3) === "```", he = (e) => {
	let t = 0, n = !1;
	for (let r = 0; r < e.length - 1; r += 1) e[r] === "`" && !J(e, r) && (n = !n), !n && e[r] === "$" && e[r + 1] === "$" && (t += 1, r += 1);
	return t;
}, ge = (e) => {
	let t = 0, n = !1;
	for (let r = 0; r < e.length; r += 1) {
		if (e[r] === "\\") {
			r += 1;
			continue;
		}
		if (e[r] === "`" && !J(e, r)) {
			n = !n;
			continue;
		}
		!n && e[r] === "$" && (r + 1 < e.length && e[r + 1] === "$" ? r += 1 : t += 1);
	}
	return t;
}, _e = (e) => {
	if (e.endsWith("$") && !e.endsWith("$$")) return `${e}$`;
	let t = e.indexOf("$$");
	return t !== -1 && e.indexOf("\n", t) !== -1 && !e.endsWith("\n") ? `${e}
$$` : `${e}$$`;
}, ve = (e) => he(e) % 2 == 0 ? e : _e(e), ye = (e) => ge(e) % 2 == 1 ? `${e}$` : e, be = (e, t, n) => {
	if (e.substring(t + 2).includes(")")) return null;
	let r = w(e, t);
	if (r === -1 || l(e, r)) return null;
	let i = r > 0 && e[r - 1] === "!", a = i ? r - 1 : r, o = e.substring(0, a);
	if (i) return o;
	let s = e.substring(r + 1, t);
	return n === "text-only" ? `${o}${s}` : `${o}[${s}](streamdown:incomplete-link)`;
}, Y = (e, t) => {
	for (let n = 0; n < t; n++) if (e[n] === "[" && !l(e, n)) {
		if (n > 0 && e[n - 1] === "!") continue;
		let t = T(e, n);
		if (t === -1) return n;
		if (t + 1 < e.length && e[t + 1] === "(") {
			let r = e.indexOf(")", t + 2);
			r !== -1 && (n = r);
		}
	}
	return t;
}, xe = (e, t, n) => {
	let r = t > 0 && e[t - 1] === "!", i = r ? t - 1 : t;
	if (!e.substring(t + 1).includes("]")) {
		let a = e.substring(0, i);
		if (r) return a;
		if (n === "text-only") {
			let n = Y(e, t);
			return e.substring(0, n) + e.substring(n + 1);
		}
		return `${e}](streamdown:incomplete-link)`;
	}
	if (T(e, t) === -1) {
		let a = e.substring(0, i);
		if (r) return a;
		if (n === "text-only") {
			let n = Y(e, t);
			return e.substring(0, n) + e.substring(n + 1);
		}
		return `${e}](streamdown:incomplete-link)`;
	}
	return null;
}, X = (e, t = "protocol") => {
	let n = e.lastIndexOf("](");
	if (n !== -1 && !l(e, n)) {
		let r = be(e, n, t);
		if (r !== null) return r;
	}
	for (let n = e.length - 1; n >= 0; --n) if (e[n] === "[" && !l(e, n)) {
		let r = xe(e, n, t);
		if (r !== null) return r;
	}
	return e;
}, Se = /^-{1,2}$/, Ce = /^[\s]*-{1,2}[\s]+$/, we = /^={1,2}$/, Te = /^[\s]*={1,2}[\s]+$/, Z = (e) => {
	if (!e || typeof e != "string") return e;
	let t = e.lastIndexOf("\n");
	if (t === -1) return e;
	let n = e.substring(t + 1), r = e.substring(0, t), i = n.trim();
	if (Se.test(i) && !n.match(Ce)) {
		let t = r.split("\n").at(-1);
		if (t && t.trim().length > 0) return `${e}\u200B`;
	}
	if (we.test(i) && !n.match(Te)) {
		let t = r.split("\n").at(-1);
		if (t && t.trim().length > 0) return `${e}\u200B`;
	}
	return e;
}, Ee = /* @__PURE__ */ RegExp("(?<=[\\p{L}\\p{N}_])~(?!~)(?=[\\p{L}\\p{N}_])", "gu"), De = (e) => !e || typeof e != "string" || !e.includes("~") ? e : e.replace(Ee, (t, n) => l(e, n) ? t : "\\~"), Oe = (e) => {
	let t = e.match(y);
	if (t) {
		let n = t[2];
		if (!n || b.test(n)) return e;
		let r = e.lastIndexOf(t[1]);
		if (l(e, r) || d(e, r)) return e;
		if (e.match(S)?.length % 2 == 1) return `${e}~~`;
	} else {
		let t = e.match(oe);
		if (t) {
			let n = e.lastIndexOf(t[0].slice(0, 2));
			if (l(e, n) || d(e, n)) return e;
			if (e.match(S)?.length % 2 == 1) return `${e}~`;
		}
	}
	return e;
}, Q = (e) => e !== !1, ke = (e) => e === !0, $ = {
	SINGLE_TILDE: 0,
	COMPARISON_OPERATORS: 5,
	HTML_TAGS: 10,
	SETEXT_HEADINGS: 15,
	LINKS: 20,
	BOLD_ITALIC: 30,
	BOLD: 35,
	ITALIC_DOUBLE_UNDERSCORE: 40,
	ITALIC_SINGLE_ASTERISK: 41,
	ITALIC_SINGLE_UNDERSCORE: 42,
	INLINE_CODE: 50,
	STRIKETHROUGH: 60,
	KATEX: 70,
	INLINE_KATEX: 75,
	DEFAULT: 100
}, Ae = [
	{
		handler: {
			name: "singleTilde",
			handle: De,
			priority: $.SINGLE_TILDE
		},
		optionKey: "singleTilde"
	},
	{
		handler: {
			name: "comparisonOperators",
			handle: f,
			priority: $.COMPARISON_OPERATORS
		},
		optionKey: "comparisonOperators"
	},
	{
		handler: {
			name: "htmlTags",
			handle: de,
			priority: $.HTML_TAGS
		},
		optionKey: "htmlTags"
	},
	{
		handler: {
			name: "setextHeadings",
			handle: Z,
			priority: $.SETEXT_HEADINGS
		},
		optionKey: "setextHeadings"
	},
	{
		handler: {
			name: "links",
			handle: X,
			priority: $.LINKS
		},
		optionKey: "links",
		earlyReturn: (e) => e.endsWith("](streamdown:incomplete-link)")
	},
	{
		handler: {
			name: "boldItalic",
			handle: le,
			priority: $.BOLD_ITALIC
		},
		optionKey: "boldItalic"
	},
	{
		handler: {
			name: "bold",
			handle: z,
			priority: $.BOLD
		},
		optionKey: "bold"
	},
	{
		handler: {
			name: "italicDoubleUnderscore",
			handle: V,
			priority: $.ITALIC_DOUBLE_UNDERSCORE
		},
		optionKey: "italic"
	},
	{
		handler: {
			name: "italicSingleAsterisk",
			handle: U,
			priority: $.ITALIC_SINGLE_ASTERISK
		},
		optionKey: "italic"
	},
	{
		handler: {
			name: "italicSingleUnderscore",
			handle: q,
			priority: $.ITALIC_SINGLE_UNDERSCORE
		},
		optionKey: "italic"
	},
	{
		handler: {
			name: "inlineCode",
			handle: me,
			priority: $.INLINE_CODE
		},
		optionKey: "inlineCode"
	},
	{
		handler: {
			name: "strikethrough",
			handle: Oe,
			priority: $.STRIKETHROUGH
		},
		optionKey: "strikethrough"
	},
	{
		handler: {
			name: "katex",
			handle: ve,
			priority: $.KATEX
		},
		optionKey: "katex"
	},
	{
		handler: {
			name: "inlineKatex",
			handle: ye,
			priority: $.INLINE_KATEX
		},
		optionKey: "inlineKatex"
	}
], je = (e) => {
	let t = e?.linkMode ?? "protocol";
	return Ae.filter(({ handler: t, optionKey: n }) => t.name === "links" ? Q(e?.links) || Q(e?.images) : t.name === "inlineKatex" ? ke(e?.inlineKatex) : Q(e?.[n])).map(({ handler: e, earlyReturn: n }) => e.name === "links" ? {
		handler: c(s({}, e), { handle: (e) => X(e, t) }),
		earlyReturn: t === "protocol" ? n : void 0
	} : {
		handler: e,
		earlyReturn: n
	});
}, Me = (e, t) => {
	if (!e || typeof e != "string") return e;
	let n = e.endsWith(" ") && !e.endsWith("  ") ? e.slice(0, -1) : e, r = je(t), i = (t?.handlers ?? []).map((e) => ({
		handler: c(s({}, e), { priority: e.priority ?? $.DEFAULT }),
		earlyReturn: void 0
	})), a = [...r, ...i].sort((e, t) => (e.handler.priority ?? 0) - (t.handler.priority ?? 0));
	for (let { handler: e, earlyReturn: t } of a) if (n = e.handle(n), t != null && t(n)) return n;
	return n;
};
//#endregion
export { Me as $e };
