var tn = Object.defineProperty, ln = Object.defineProperties, cn = Object.getOwnPropertyDescriptors, M = Object.getOwnPropertySymbols, an = Object.prototype.hasOwnProperty, un = Object.prototype.propertyIsEnumerable, _ = (i, o, s) => o in i ? tn(i, o, {
	enumerable: !0,
	configurable: !0,
	writable: !0,
	value: s
}) : i[o] = s, k = (e, i) => {
	for (var o in i ||= {}) an.call(i, o) && _(e, o, i[o]);
	if (M) for (var o of M(i)) un.call(i, o) && _(e, o, i[o]);
	return e;
}, I = (e, s) => ln(e, cn(s)), $ = /(\*\*)([^*]*\*?)$/, L = /(__)([^_]*?)$/, E = /(\*\*\*)([^*]*?)$/, y = /(\*)([^*]*?)$/, R = /(_)([^_]*?)$/, N = /(`)([^`]*?)$/, U = /(~~)([^~]*?)$/, u = /^[\s_~*`]*$/, b = /^[\s]*[-*+][\s]+$/, W = /[\p{L}\p{N}_]/u, H = /^```[^`\n]*```?$/, D = /^\*{4,}$/, K = /(__)([^_]+)_$/, w = /(~~)([^~]+)~$/, A = /~~/g, f = (e) => {
	if (!e) return !1;
	let i = e.charCodeAt(0);
	return i >= 48 && i <= 57 || i >= 65 && i <= 90 || i >= 97 && i <= 122 || i === 95 ? !0 : W.test(e);
}, d = (e, i) => {
	let o = !1;
	for (let s = 0; s < i; s += 1) e[s] === "`" && e[s + 1] === "`" && e[s + 2] === "`" && (o = !o, s += 2);
	return o;
}, G = (e, i) => {
	let o = 1;
	for (let s = i - 1; s >= 0; --s) if (e[s] === "]") o += 1;
	else if (e[s] === "[" && (--o, o === 0)) return s;
	return -1;
}, B = (e, i) => {
	let o = 1;
	for (let s = i + 1; s < e.length; s += 1) if (e[s] === "[") o += 1;
	else if (e[s] === "]" && (--o, o === 0)) return s;
	return -1;
}, h = (e, i) => {
	let o = !1, s = !1;
	for (let c = 0; c < e.length && c < i; c += 1) {
		if (e[c] === "\\" && e[c + 1] === "$") {
			c += 1;
			continue;
		}
		e[c] === "$" && (e[c + 1] === "$" ? (s = !s, c += 1, o = !1) : s || (o = !o));
	}
	return o || s;
}, fn = (e, i) => {
	for (let o = i; o < e.length; o += 1) {
		if (e[o] === ")") return !0;
		if (e[o] === "\n") return !1;
	}
	return !1;
}, m = (e, i) => {
	for (let o = i - 1; o >= 0; --o) {
		if (e[o] === ")") return !1;
		if (e[o] === "(") return o > 0 && e[o - 1] === "]" ? fn(e, i) : !1;
		if (e[o] === "\n") return !1;
	}
	return !1;
}, F = (e, i) => {
	for (let o = i - 1; o >= 0; --o) {
		if (e[o] === ">") return !1;
		if (e[o] === "<") {
			let i = o + 1 < e.length ? e[o + 1] : "";
			return i >= "a" && i <= "z" || i >= "A" && i <= "Z" || i === "/";
		}
		if (e[o] === "\n") return !1;
	}
	return !1;
}, p = (e, i, o) => {
	let s = 0;
	for (let o = i - 1; o >= 0; --o) if (e[o] === "\n") {
		s = o + 1;
		break;
	}
	let c = e.length;
	for (let o = i; o < e.length; o += 1) if (e[o] === "\n") {
		c = o;
		break;
	}
	let l = e.substring(s, c), S = 0, O = !1;
	for (let e of l) if (e === o) S += 1;
	else if (e !== " " && e !== "	") {
		O = !0;
		break;
	}
	return S >= 3 && !O;
}, dn = /^(\s*(?:[-*+]|\d+[.)]) +)>(=?\s*[$]?\d)/gm, z = (e) => !e || typeof e != "string" || !e.includes(">") ? e : e.replace(dn, (i, o, s, c) => d(e, c) ? i : `${o}\\>${s}`), gn = (e, i, o, s) => o === "\\" || e.includes("$") && h(e, i) ? !0 : o !== "*" && s === "*" ? (i < e.length - 2 ? e[i + 2] : "") !== "*" : !!(o === "*" || o && s && f(o) && f(s) || (!o || o === " " || o === "	" || o === "\n") && (!s || s === " " || s === "	" || s === "\n")), v = (e) => {
	let i = 0, o = !1, s = e.length;
	for (let c = 0; c < s; c += 1) {
		if (e[c] === "`" && c + 2 < s && e[c + 1] === "`" && e[c + 2] === "`") {
			o = !o, c += 2;
			continue;
		}
		if (o || e[c] !== "*") continue;
		let l = c > 0 ? e[c - 1] : "", S = c < s - 1 ? e[c + 1] : "";
		gn(e, c, l, S) || (i += 1);
	}
	return i;
}, hn = (e, i, o, s) => !!(o === "\\" || e.includes("$") && h(e, i) || m(e, i) || F(e, i) || o === "_" || s === "_" || o && s && f(o) && f(s)), mn = (e) => {
	let i = 0, o = !1, s = e.length;
	for (let c = 0; c < s; c += 1) {
		if (e[c] === "`" && c + 2 < s && e[c + 1] === "`" && e[c + 2] === "`") {
			o = !o, c += 2;
			continue;
		}
		if (o || e[c] !== "_") continue;
		let l = c > 0 ? e[c - 1] : "", S = c < s - 1 ? e[c + 1] : "";
		hn(e, c, l, S) || (i += 1);
	}
	return i;
}, pn = (e) => {
	let i = 0, o = 0, s = !1;
	for (let c = 0; c < e.length; c += 1) {
		if (e[c] === "`" && c + 2 < e.length && e[c + 1] === "`" && e[c + 2] === "`") {
			o >= 3 && (i += Math.floor(o / 3)), o = 0, s = !s, c += 2;
			continue;
		}
		s || (e[c] === "*" ? o += 1 : (o >= 3 && (i += Math.floor(o / 3)), o = 0));
	}
	return o >= 3 && (i += Math.floor(o / 3)), i;
}, C = (e) => {
	let i = 0, o = !1;
	for (let s = 0; s < e.length; s += 1) {
		if (e[s] === "`" && s + 2 < e.length && e[s + 1] === "`" && e[s + 2] === "`") {
			o = !o, s += 2;
			continue;
		}
		o || e[s] === "*" && s + 1 < e.length && e[s + 1] === "*" && (i += 1, s += 1);
	}
	return i;
}, X = (e) => {
	let i = 0, o = !1;
	for (let s = 0; s < e.length; s += 1) {
		if (e[s] === "`" && s + 2 < e.length && e[s + 1] === "`" && e[s + 2] === "`") {
			o = !o, s += 2;
			continue;
		}
		o || e[s] === "_" && s + 1 < e.length && e[s + 1] === "_" && (i += 1, s += 1);
	}
	return i;
}, kn = (e, i, o) => {
	if (!i || u.test(i)) return !0;
	let s = e.substring(0, o).lastIndexOf("\n"), c = s === -1 ? 0 : s + 1, l = e.substring(c, o);
	return b.test(l) && i.includes("\n") ? !0 : p(e, o, "*");
}, Y = (e) => {
	let i = e.match($);
	if (!i) return e;
	let o = i[2], s = e.lastIndexOf(i[1]);
	return d(e, s) || kn(e, o, s) ? e : C(e) % 2 == 1 ? o.endsWith("*") ? `${e}*` : `${e}**` : e;
}, In = (e, i, o) => {
	if (!i || u.test(i)) return !0;
	let s = e.substring(0, o).lastIndexOf("\n"), c = s === -1 ? 0 : s + 1, l = e.substring(c, o);
	return b.test(l) && i.includes("\n") ? !0 : p(e, o, "_");
}, j = (e) => {
	let i = e.match(L);
	if (!i) {
		let i = e.match(K);
		return i && !d(e, e.lastIndexOf(i[1])) && X(e) % 2 == 1 ? `${e}_` : e;
	}
	let o = i[2], s = e.lastIndexOf(i[1]);
	return d(e, s) || In(e, o, s) ? e : X(e) % 2 == 1 ? `${e}__` : e;
}, bn = (e) => {
	let i = !1;
	for (let o = 0; o < e.length; o += 1) {
		if (e[o] === "`" && o + 2 < e.length && e[o + 1] === "`" && e[o + 2] === "`") {
			i = !i, o += 2;
			continue;
		}
		if (!i && e[o] === "*" && e[o - 1] !== "*" && e[o + 1] !== "*" && e[o - 1] !== "\\" && !h(e, o)) {
			let i = o > 0 ? e[o - 1] : "", s = o < e.length - 1 ? e[o + 1] : "";
			if ((!i || i === " " || i === "	" || i === "\n") && (!s || s === " " || s === "	" || s === "\n") || i && s && f(i) && f(s)) continue;
			return o;
		}
	}
	return -1;
}, Q = (e) => {
	if (!e.match(y)) return e;
	let i = bn(e);
	if (i === -1) return e;
	let o = e.substring(i + 1);
	return !o || u.test(o) ? e : v(e) % 2 == 1 ? `${e}*` : e;
}, Z = (e) => {
	let i = !1;
	for (let o = 0; o < e.length; o += 1) {
		if (e[o] === "`" && o + 2 < e.length && e[o + 1] === "`" && e[o + 2] === "`") {
			i = !i, o += 2;
			continue;
		}
		if (!i && e[o] === "_" && e[o - 1] !== "_" && e[o + 1] !== "_" && e[o - 1] !== "\\" && !h(e, o) && !m(e, o)) {
			let i = o > 0 ? e[o - 1] : "", s = o < e.length - 1 ? e[o + 1] : "";
			if (i && s && f(i) && f(s)) continue;
			return o;
		}
	}
	return -1;
}, An = (e) => {
	let i = e.length;
	for (; i > 0 && e[i - 1] === "\n";) --i;
	return i < e.length ? `${e.slice(0, i)}_${e.slice(i)}` : `${e}_`;
}, Bn = (e) => {
	if (!e.endsWith("**")) return null;
	let i = e.slice(0, -2);
	if (C(i) % 2 != 1) return null;
	let o = i.indexOf("**"), s = Z(i);
	return o !== -1 && s !== -1 && o < s ? `${i}_**` : null;
}, q = (e) => {
	if (!e.match(R)) return e;
	let i = Z(e);
	if (i === -1) return e;
	let o = e.substring(i + 1);
	if (!o || u.test(o)) return e;
	if (mn(e) % 2 == 1) {
		let i = Bn(e);
		return i === null ? An(e) : i;
	}
	return e;
}, Cn = (e) => {
	let i = C(e), o = v(e);
	return i % 2 == 0 && o % 2 == 0;
}, Tn = (e, i, o) => !i || u.test(i) || d(e, o) ? !0 : p(e, o, "*"), J = (e) => {
	if (D.test(e)) return e;
	let i = e.match(E);
	if (!i) return e;
	let o = i[2];
	return Tn(e, o, e.lastIndexOf(i[1])) ? e : pn(e) % 2 == 1 ? Cn(e) ? e : `${e}***` : e;
}, g = (e, i) => {
	let o = !1, s = !1;
	for (let c = 0; c < i; c += 1) {
		if (e.substring(c, c + 3) === "```") {
			s = !s, c += 2;
			continue;
		}
		!s && e[c] === "`" && (o = !o);
	}
	return o || s;
}, Pn = (e, i) => {
	let o = e.substring(i, i + 3) === "```", s = i > 0 && e.substring(i - 1, i + 2) === "```", c = i > 1 && e.substring(i - 2, i + 1) === "```";
	return o || s || c;
}, V = (e) => {
	let i = 0;
	for (let o = 0; o < e.length; o += 1) e[o] === "`" && !Pn(e, o) && (i += 1);
	return i;
}, On = /<[a-zA-Z/][^>]*$/, x = (e) => {
	let i = e.match(On);
	return !i || i.index === void 0 || g(e, i.index) ? e : e.substring(0, i.index).trimEnd();
}, Sn = (e) => !e.match(H) || e.includes("\n") ? null : e.endsWith("``") && !e.endsWith("```") ? `${e}\`` : e, Mn = (e) => (e.match(/```/g) || []).length % 2 == 1, nn = (e) => {
	let i = Sn(e);
	if (i !== null) return i;
	let o = e.match(N);
	if (o && !Mn(e)) {
		let i = o[2];
		if (!i || u.test(i)) return e;
		if (V(e) % 2 == 1) return `${e}\``;
	}
	return e;
}, _n = (e, i) => i >= 2 && e.substring(i - 2, i + 1) === "```" || i >= 1 && e.substring(i - 1, i + 2) === "```" || i <= e.length - 3 && e.substring(i, i + 3) === "```", $n = (e) => {
	let i = 0, o = !1;
	for (let s = 0; s < e.length - 1; s += 1) e[s] === "`" && !_n(e, s) && (o = !o), !o && e[s] === "$" && e[s + 1] === "$" && (i += 1, s += 1);
	return i;
}, Ln = (e) => {
	let i = e.indexOf("$$");
	return i !== -1 && e.indexOf("\n", i) !== -1 && !e.endsWith("\n") ? `${e}
$$` : `${e}$$`;
}, rn = (e) => $n(e) % 2 == 0 ? e : Ln(e), En = (e, i, o) => {
	if (e.substring(i + 2).includes(")")) return null;
	let s = G(e, i);
	if (s === -1 || g(e, s)) return null;
	let c = s > 0 && e[s - 1] === "!", l = c ? s - 1 : s, S = e.substring(0, l);
	if (c) return S;
	let O = e.substring(s + 1, i);
	return o === "text-only" ? `${S}${O}` : `${S}[${O}](streamdown:incomplete-link)`;
}, en = (e, i) => {
	for (let o = 0; o < i; o++) if (e[o] === "[" && !g(e, o)) {
		if (o > 0 && e[o - 1] === "!") continue;
		let i = B(e, o);
		if (i === -1) return o;
		if (i + 1 < e.length && e[i + 1] === "(") {
			let s = e.indexOf(")", i + 2);
			s !== -1 && (o = s);
		}
	}
	return i;
}, yn = (e, i, o) => {
	let s = i > 0 && e[i - 1] === "!", c = s ? i - 1 : i;
	if (!e.substring(i + 1).includes("]")) {
		let l = e.substring(0, c);
		if (s) return l;
		if (o === "text-only") {
			let o = en(e, i);
			return e.substring(0, o) + e.substring(o + 1);
		}
		return `${e}](streamdown:incomplete-link)`;
	}
	if (B(e, i) === -1) {
		let l = e.substring(0, c);
		if (s) return l;
		if (o === "text-only") {
			let o = en(e, i);
			return e.substring(0, o) + e.substring(o + 1);
		}
		return `${e}](streamdown:incomplete-link)`;
	}
	return null;
}, T = (e, i = "protocol") => {
	let o = e.lastIndexOf("](");
	if (o !== -1 && !g(e, o)) {
		let s = En(e, o, i);
		if (s !== null) return s;
	}
	for (let o = e.length - 1; o >= 0; --o) if (e[o] === "[" && !g(e, o)) {
		let s = yn(e, o, i);
		if (s !== null) return s;
	}
	return e;
}, Rn = /^-{1,2}$/, Nn = /^[\s]*-{1,2}[\s]+$/, Un = /^={1,2}$/, Wn = /^[\s]*={1,2}[\s]+$/, sn = (e) => {
	if (!e || typeof e != "string") return e;
	let i = e.lastIndexOf("\n");
	if (i === -1) return e;
	let o = e.substring(i + 1), s = e.substring(0, i), c = o.trim();
	if (Rn.test(c) && !o.match(Nn)) {
		let i = s.split("\n").at(-1);
		if (i && i.trim().length > 0) return `${e}\u200B`;
	}
	if (Un.test(c) && !o.match(Wn)) {
		let i = s.split("\n").at(-1);
		if (i && i.trim().length > 0) return `${e}\u200B`;
	}
	return e;
}, on = (e) => {
	let i = e.match(U);
	if (i) {
		let o = i[2];
		if (!o || u.test(o)) return e;
		if (e.match(A)?.length % 2 == 1) return `${e}~~`;
	} else if (e.match(w) && e.match(A)?.length % 2 == 1) return `${e}~`;
	return e;
}, P = (e) => e !== !1, a = {
	COMPARISON_OPERATORS: -10,
	HTML_TAGS: -5,
	SETEXT_HEADINGS: 0,
	LINKS: 10,
	BOLD_ITALIC: 20,
	BOLD: 30,
	ITALIC_DOUBLE_UNDERSCORE: 40,
	ITALIC_SINGLE_ASTERISK: 41,
	ITALIC_SINGLE_UNDERSCORE: 42,
	INLINE_CODE: 50,
	STRIKETHROUGH: 60,
	KATEX: 70,
	DEFAULT: 100
}, Hn = [
	{
		handler: {
			name: "comparisonOperators",
			handle: z,
			priority: a.COMPARISON_OPERATORS
		},
		optionKey: "comparisonOperators"
	},
	{
		handler: {
			name: "htmlTags",
			handle: x,
			priority: a.HTML_TAGS
		},
		optionKey: "htmlTags"
	},
	{
		handler: {
			name: "setextHeadings",
			handle: sn,
			priority: a.SETEXT_HEADINGS
		},
		optionKey: "setextHeadings"
	},
	{
		handler: {
			name: "links",
			handle: T,
			priority: a.LINKS
		},
		optionKey: "links",
		earlyReturn: (e) => e.endsWith("](streamdown:incomplete-link)")
	},
	{
		handler: {
			name: "boldItalic",
			handle: J,
			priority: a.BOLD_ITALIC
		},
		optionKey: "boldItalic"
	},
	{
		handler: {
			name: "bold",
			handle: Y,
			priority: a.BOLD
		},
		optionKey: "bold"
	},
	{
		handler: {
			name: "italicDoubleUnderscore",
			handle: j,
			priority: a.ITALIC_DOUBLE_UNDERSCORE
		},
		optionKey: "italic"
	},
	{
		handler: {
			name: "italicSingleAsterisk",
			handle: Q,
			priority: a.ITALIC_SINGLE_ASTERISK
		},
		optionKey: "italic"
	},
	{
		handler: {
			name: "italicSingleUnderscore",
			handle: q,
			priority: a.ITALIC_SINGLE_UNDERSCORE
		},
		optionKey: "italic"
	},
	{
		handler: {
			name: "inlineCode",
			handle: nn,
			priority: a.INLINE_CODE
		},
		optionKey: "inlineCode"
	},
	{
		handler: {
			name: "strikethrough",
			handle: on,
			priority: a.STRIKETHROUGH
		},
		optionKey: "strikethrough"
	},
	{
		handler: {
			name: "katex",
			handle: rn,
			priority: a.KATEX
		},
		optionKey: "katex"
	}
], Dn = (e) => {
	let i = e?.linkMode ?? "protocol";
	return Hn.filter(({ handler: i, optionKey: o }) => i.name === "links" ? P(e?.links) || P(e?.images) : P(e?.[o])).map(({ handler: e, earlyReturn: o }) => e.name === "links" ? {
		handler: I(k({}, e), { handle: (e) => T(e, i) }),
		earlyReturn: i === "protocol" ? o : void 0
	} : {
		handler: e,
		earlyReturn: o
	});
}, pr = (e, i) => {
	if (!e || typeof e != "string") return e;
	let o = e.endsWith(" ") && !e.endsWith("  ") ? e.slice(0, -1) : e, s = Dn(i), c = (i?.handlers ?? []).map((e) => ({
		handler: I(k({}, e), { priority: e.priority ?? a.DEFAULT }),
		earlyReturn: void 0
	})), l = [...s, ...c].sort((e, i) => (e.handler.priority ?? 0) - (i.handler.priority ?? 0));
	for (let { handler: e, earlyReturn: i } of l) if (o = e.handle(o), i != null && i(o)) return o;
	return o;
};
export { pr };
