import { createHighlighterCore } from "shiki/core";
import darkTheme from "shiki/dist/themes/vitesse-dark.mjs";
import lightTheme from "shiki/dist/themes/vitesse-light.mjs";
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
//#region components/code-highlighter/shiki.ts
var SUPPORTED_LANG_ALIASES = {
	ts: "typescript",
	js: "javascript",
	py: "python",
	md: "markdown",
	yml: "yaml",
	shell: "bash",
	zsh: "bash",
	sh: "bash",
	vue: "vue"
};
var LANG_LOADERS = {
	javascript: () => import("shiki/dist/langs/javascript.mjs"),
	typescript: () => import("shiki/dist/langs/typescript.mjs"),
	jsx: () => import("shiki/dist/langs/jsx.mjs"),
	tsx: () => import("shiki/dist/langs/tsx.mjs"),
	json: () => import("shiki/dist/langs/json.mjs"),
	bash: () => import("shiki/dist/langs/bash.mjs"),
	markdown: () => import("shiki/dist/langs/markdown.mjs"),
	html: () => import("shiki/dist/langs/html.mjs"),
	css: () => import("shiki/dist/langs/css.mjs"),
	vue: () => import("shiki/dist/langs/vue.mjs"),
	"vue-html": () => import("shiki/dist/langs/vue-html.mjs"),
	python: () => import("shiki/dist/langs/python.mjs"),
	mermaid: () => import("shiki/dist/langs/mermaid.mjs"),
	yaml: () => import("shiki/dist/langs/yaml.mjs"),
	diff: () => import("shiki/dist/langs/diff.mjs")
};
var highlighter = null;
var highlighterPromise = null;
var loadedLangs = /* @__PURE__ */ new Set();
function normalizeLanguage(language) {
	const input = (language || "text").toLowerCase();
	return SUPPORTED_LANG_ALIASES[input] || input;
}
function escapeHtml(str) {
	return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
}
async function getHighlighter() {
	if (highlighter) return highlighter;
	if (!highlighterPromise) highlighterPromise = createHighlighterCore({
		themes: [lightTheme, darkTheme],
		langs: [],
		engine: createJavaScriptRegexEngine()
	});
	highlighter = await highlighterPromise;
	return highlighter;
}
async function loadLang(lang) {
	const loader = LANG_LOADERS[lang];
	if (!loader) return false;
	if (loadedLangs.has(lang)) return true;
	const instance = await getHighlighter();
	const mod = await loader();
	await instance.loadLanguage(mod.default);
	loadedLangs.add(lang);
	return true;
}
async function codeToHtml(code, options = {}) {
	const lang = normalizeLanguage(options.lang);
	if (!await loadLang(lang)) return `<pre><code>${escapeHtml(code)}</code></pre>`;
	return (await getHighlighter()).codeToHtml(code, {
		lang,
		theme: options.theme || "vitesse-light"
	});
}
//#endregion
export { codeToHtml };
