import { t as e } from "./sanitize-CDxuPfMB.js";
import { n as t, t as n } from "./utils-C_mWfcFX.js";
//#region src/display/markdown-viewer/parser.ts
var r = 0;
function i() {
	r = 0;
}
function a(e, t) {
	let n = t?.supportMermaid ?? !1, r = [], i = c(e, r, n);
	return i = d(i), i = f(i), i = p(i), i = m(i), i = y(i), i = b(i), i = x(i), i = S(i), i = C(i), i = T(i), i = E(i), i = D(i), i = i.replace(/\n/g, "<br>"), i = l(i, r), i;
}
var o = "\0CB", s = "\0MM";
function c(e, t, n) {
	return e.replace(/```(\w*)\n([\s\S]*?)```/g, (e, i, a) => {
		if (n && i === "mermaid") {
			let e = j(O(a));
			return r += 1, `${s}${`cmm-mermaid-ph-${String(r)}`}\x01${e}\x00`;
		}
		r += 1;
		let c = `chat-code-${String(r)}`, l = O(a);
		return t.push({
			language: i,
			code: l,
			id: c
		}), `${o}${String(t.length - 1)}\x00`;
	});
}
function l(e, t) {
	return e = e.replace(RegExp(`${s}([^\\x01]+)\\x01([^\\x00]+)\\x00`, "g"), (e, t, n) => `<div id="${t}" class="cmm-mermaid-placeholder" data-mermaid-content="${n}"></div>`), e = e.replace(RegExp(`${o}(\\d+)\x00`, "g"), (e, n) => {
		let r = t[Number.parseInt(n, 10)];
		return r ? u(r) : "";
	}), e;
}
function u(e) {
	let t = e.language.length > 0 ? `<span class="chat-code__lang">${A(e.language)}</span>` : "", n = k(e.code);
	return `<div class="chat-code-block" id="${e.id}" data-lang="${A(e.language)}">
<div class="chat-code__header">
${t}
<button type="button" class="chat-code__copy" data-copy-code="${e.id}" title="Copy code">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/></svg>
<span class="chat-code__copy-label">Copy</span>
</button>
</div>
<pre><code class="language-${A(e.language)}">${n}</code></pre>
</div>`;
}
function d(e) {
	return e.replace(/^(#{1,6})\s+(.+)$/gm, (e, t, n) => {
		let r = t.length;
		return `<h${String(r)} class="chat-md-heading chat-md-h${String(r)}">${n}</h${String(r)}>`;
	});
}
function f(e) {
	return e.replace(/^(?:---+|\*\*\*+|___+)\s*$/gm, "<hr class=\"chat-md-hr\">");
}
function p(e) {
	let t = e.split("\n"), n = [], r = !1;
	for (let e of t) {
		let t = /^&gt;\s?(.*)$/.exec(e);
		t ? (r ||= (n.push("<blockquote class=\"chat-md-blockquote\">"), !0), n.push(t[1] ?? "")) : (r &&= (n.push("</blockquote>"), !1), n.push(e));
	}
	return r && n.push("</blockquote>"), n.join("\n");
}
function m(e) {
	let t = e.split("\n"), n = [], r = 0;
	for (; r < t.length;) {
		let e = t[r] ?? "";
		if (h(e) && r + 1 < t.length && g(t[r + 1] ?? "")) {
			let i = [e];
			for (r++, i.push(t[r] ?? ""), r++; r < t.length && h(t[r] ?? "");) i.push(t[r] ?? ""), r++;
			n.push(_(i));
		} else n.push(e), r++;
	}
	return n.join("\n");
}
function h(e) {
	let t = e.trim();
	return t.includes("|") && !t.startsWith("|--");
}
function g(e) {
	let t = e.trim();
	return /^\|?[\s:]*-{2,}[\s:]*(\|[\s:]*-{2,}[\s:]*)*\|?\s*$/.test(t);
}
function _(e) {
	return e.length < 2 ? e.join("\n") : `<table class="chat-md-table"><thead><tr>${v(e[0] ?? "").map((e) => `<th>${e.trim()}</th>`).join("")}</tr></thead><tbody>${e.slice(2).map((e) => `<tr>${v(e).map((e) => `<td>${e.trim()}</td>`).join("")}</tr>`).join("")}</tbody></table>`;
}
function v(e) {
	let t = e.trim();
	return t.startsWith("|") && (t = t.slice(1)), t.endsWith("|") && (t = t.slice(0, -1)), t.split("|");
}
function y(e) {
	let t = e.split("\n"), n = [], r = !1, i = !1;
	for (let e of t) {
		let t = /^(\s*)[-*+]\s+(.+)$/.exec(e), a = /^(\s*)\d+\.\s+(.+)$/.exec(e);
		t ? (i &&= (n.push("</ol>"), !1), r ||= (n.push("<ul class=\"chat-md-list\">"), !0), n.push(`<li>${t[2] ?? ""}</li>`)) : a ? (r &&= (n.push("</ul>"), !1), i ||= (n.push("<ol class=\"chat-md-list\">"), !0), n.push(`<li>${a[2] ?? ""}</li>`)) : (r &&= (n.push("</ul>"), !1), i &&= (n.push("</ol>"), !1), n.push(e));
	}
	return r && n.push("</ul>"), i && n.push("</ol>"), n.join("\n");
}
function b(e) {
	return e.replace(/~~([^~]+)~~/g, "<del>$1</del>");
}
function x(e) {
	return e.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
}
function S(e) {
	return e.replace(/\*([^*]+)\*/g, "<em>$1</em>");
}
function C(e) {
	return e.replace(/`([^`]+)`/g, "<code class=\"chat-md-inline-code\">$1</code>");
}
var w = /^(?:https?:|mailto:|tel:|#|\/)/i;
function T(e) {
	return e.replace(/(?<!!)\[([^\]]+)\]\(([^)]+)\)/g, (e, t, n) => {
		let r = n.trim();
		return w.test(r) ? `<a href="${A(r)}" class="chat-md-link" target="_blank" rel="noopener noreferrer">${A(t)}</a>` : `<a class="chat-md-link">${A(t)}</a>`;
	});
}
function E(e) {
	return e.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (e, t, n) => {
		let r = n.trim();
		return /^(?:https?:|data:image\/|\/)/i.test(r) ? `<img src="${A(r)}" alt="${A(t)}" class="chat-md-image" loading="lazy" />` : `<span class="chat-md-image" role="img" aria-label="${A(t)}">[Image: ${A(t)}]</span>`;
	});
}
function D(e) {
	return e.replace(/<li>\[x\]\s*/gi, "<li class=\"chat-md-task chat-md-task--checked\"><input type=\"checkbox\" checked disabled /> ").replace(/<li>\[ \]\s*/g, "<li class=\"chat-md-task\"><input type=\"checkbox\" disabled /> ");
}
function O(e) {
	return e.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, "\"").replace(/&#39;/g, "'");
}
function k(e) {
	return e.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function A(e) {
	return e.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "&#039;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}
function j(e) {
	let t = new TextEncoder().encode(e), n = "";
	for (let e of t) n += String.fromCharCode(e);
	return btoa(n);
}
function M(e) {
	let t = atob(e), n = new Uint8Array(t.length);
	for (let e = 0; e < t.length; e++) n[e] = t.charCodeAt(e);
	return new TextDecoder().decode(n);
}
var N = {
	transformHeadings: d,
	transformHorizontalRules: f,
	transformBlockquotes: p,
	transformTables: m,
	transformLists: y,
	transformStrikethrough: b,
	transformBold: x,
	transformItalic: S,
	transformInlineCode: C,
	transformLinks: T,
	transformImages: E,
	transformTaskLists: D,
	renderCodeBlock: u,
	decodeHtmlEntities: O,
	escapeForCodeBlock: k
}, P = class {
	el;
	viewerId;
	mermaidTheme;
	supportMermaid;
	currentContent;
	mermaidInstances = [];
	constructor(r) {
		this.viewerId = t("cmm-markdown"), this.currentContent = r.content, this.supportMermaid = r.renderMermaid ?? !1, this.mermaidTheme = r.mermaidTheme ?? "auto", this.el = document.createElement("div"), this.el.id = this.viewerId, this.el.className = "cmm-markdown", r.className && this.el.classList.add(r.className);
		let i = e(this.currentContent);
		this.el.innerHTML = a(i, { supportMermaid: this.supportMermaid }), r.container !== void 0 && n(r.container).appendChild(this.el), this.supportMermaid && this.renderMermaidDiagrams();
	}
	setContent(t) {
		this.destroyMermaidInstances(), this.currentContent = t;
		let n = e(this.currentContent);
		this.el.innerHTML = a(n, { supportMermaid: this.supportMermaid }), this.supportMermaid && this.renderMermaidDiagrams();
	}
	async renderMermaidDiagrams() {
		let t = this.el.querySelectorAll(".cmm-mermaid-placeholder");
		if (t.length === 0) return;
		let { MermaidDiagram: n } = await import("./visualization/mermaid-diagram/index.js");
		for (let r of t) {
			let t = r.dataset.mermaidContent;
			if (!(t === void 0 || t === "")) try {
				let e = new n({
					container: r,
					content: M(t),
					theme: this.mermaidTheme
				});
				r.classList.remove("cmm-mermaid-placeholder"), r.classList.add("cmm-mermaid-rendered"), this.mermaidInstances.push(e);
			} catch (t) {
				r.classList.add("cmm-mermaid-error"), r.innerHTML = `<div class="cmm-mermaid-diagram__error">
          <div class="cmm-mermaid-diagram__error-title">Failed to render diagram</div>
          <div class="cmm-mermaid-diagram__error-message">${e(String(t))}</div>
        </div>`;
			}
		}
	}
	destroyMermaidInstances() {
		for (let e of this.mermaidInstances) e.destroy();
		this.mermaidInstances = [];
	}
	destroy() {
		this.destroyMermaidInstances(), this.el.remove();
	}
};
//#endregion
export { a, N as i, M as n, i as o, j as r, P as t };

//# sourceMappingURL=markdown-viewer-DrwjEcXp.js.map