import { isWorkerContext } from "../utils/isWorkerContext.js";
import { getThemes } from "../utils/getThemes.js";
import { bundledThemes } from "shiki";
import { normalizeTheme } from "shiki/core";

//#region src/highlighter/themes.ts
const ResolvedThemes = /* @__PURE__ */ new Map();
const ResolvingThemes = /* @__PURE__ */ new Map();
const RegisteredCustomThemes = /* @__PURE__ */ new Map();
const attachedThemes = /* @__PURE__ */ new Set();
function getResolvedThemes(themeNames) {
	const resolvedThemes = [];
	for (const themeName of themeNames) {
		const theme = ResolvedThemes.get(themeName);
		if (theme == null) throw new Error(`getAllResolvedThemes: ${themeName} is unresolved, you must resolve all necessary themes before calling this function`);
		resolvedThemes.push(theme);
	}
	return resolvedThemes;
}
function getResolvedOrResolveTheme(themeName) {
	return ResolvedThemes.get(themeName) ?? resolveTheme(themeName);
}
function hasResolvedThemes(themeNames) {
	for (const themeName of themeNames) if (!ResolvedThemes.has(themeName)) return false;
	return true;
}
async function resolveTheme(themeName) {
	if (isWorkerContext()) throw new Error(`resolveTheme("${themeName}") cannot be called from a worker context. Themes must be pre-resolved on the main thread and passed to the worker via the resolvedLanguages parameter.`);
	const resolver = ResolvingThemes.get(themeName);
	if (resolver != null) return resolver;
	try {
		const loader = RegisteredCustomThemes.get(themeName) ?? bundledThemes[themeName];
		if (loader == null) throw new Error(`resolveTheme: No valid loader for ${themeName}`);
		const resolver$1 = loader().then((result) => {
			return normalizeAndCacheResolvedTheme(themeName, "default" in result ? result.default : result);
		});
		ResolvingThemes.set(themeName, resolver$1);
		const theme = await resolver$1;
		if (theme.name !== themeName) throw new Error(`resolvedTheme: themeName: ${themeName} does not match theme.name: ${theme.name}`);
		ResolvedThemes.set(theme.name, theme);
		return theme;
	} finally {
		ResolvingThemes.delete(themeName);
	}
}
async function resolveThemes(themes) {
	const resolvedThemes = [];
	const themesToResolve = [];
	for (const themeName of themes) {
		const themeData = getResolvedOrResolveTheme(themeName) ?? resolveTheme(themeName);
		if ("then" in themeData) themesToResolve.push(themeData);
		else resolvedThemes.push(themeData);
	}
	if (themesToResolve.length > 0) await Promise.all(themesToResolve).then((resolved) => {
		for (const theme of resolved) if (theme != null) resolvedThemes.push(theme);
	});
	return resolvedThemes;
}
function attachResolvedThemes(themes, highlighter) {
	themes = Array.isArray(themes) ? themes : [themes];
	for (let themeRef of themes) {
		let resolvedTheme;
		if (typeof themeRef === "string") {
			resolvedTheme = ResolvedThemes.get(themeRef);
			if (resolvedTheme == null) throw new Error(`loadResolvedThemes: ${themeRef} is not resolved, you must resolve it before calling loadResolvedThemes`);
		} else {
			resolvedTheme = themeRef;
			themeRef = themeRef.name;
			if (!ResolvedThemes.has(themeRef)) ResolvedThemes.set(themeRef, resolvedTheme);
		}
		if (attachedThemes.has(themeRef)) continue;
		attachedThemes.add(themeRef);
		highlighter.loadThemeSync(resolvedTheme);
	}
}
function areThemesAttached(themes) {
	for (const theme of getThemes(themes)) if (!attachedThemes.has(theme)) return false;
	return true;
}
function cleanUpResolvedThemes() {
	ResolvedThemes.clear();
	attachedThemes.clear();
}
function registerCustomTheme(themeName, loader) {
	if (RegisteredCustomThemes.has(themeName)) {
		console.error("SharedHighlight.registerCustomTheme: theme name already registered", themeName);
		return;
	}
	RegisteredCustomThemes.set(themeName, loader);
}
function normalizeAndCacheResolvedTheme(themeName, themeData) {
	const resolvedTheme = ResolvedThemes.get(themeName);
	if (resolvedTheme != null) return resolvedTheme;
	themeData = normalizeTheme(themeData);
	ResolvedThemes.set(themeName, themeData);
	return themeData;
}
registerCustomTheme("pierre-dark", () => {
	return import("../themes/pierre-dark.js");
});
registerCustomTheme("pierre-light", () => {
	return import("../themes/pierre-light.js");
});

//#endregion
export { areThemesAttached, attachResolvedThemes, cleanUpResolvedThemes, getResolvedOrResolveTheme, getResolvedThemes, hasResolvedThemes, registerCustomTheme, resolveTheme, resolveThemes };
//# sourceMappingURL=themes.js.map