import { performance } from "node:perf_hooks";
import process from "node:process";
import { ViteNodeRunner } from "vite-node/client";
import { consola } from "consola";
import { viteNodeFetch, viteNodeOptions } from "#vite-node";
const runner = createRunner();
let render;
var vite_node_entry_default = async (ssrContext) => {
	process.server = true;
	import.meta.server = true;
	const invalidates = await viteNodeFetch.getInvalidates();
	const updates = runner.moduleCache.invalidateDepTree(invalidates);
	const start = performance.now();
	render = updates.has(viteNodeOptions.entryPath) || !render ? (await runner.executeFile(viteNodeOptions.entryPath)).default : render;
	if (updates.size) {
		const time = Math.round((performance.now() - start) * 1e3) / 1e3;
		consola.success(`Vite server hmr ${updates.size} files`, time ? `in ${time}ms` : "");
	}
	return await render(ssrContext);
};
function createRunner() {
	return new ViteNodeRunner({
		root: viteNodeOptions.root,
		base: viteNodeOptions.base,
		async resolveId(id, importer) {
			return await viteNodeFetch.resolveId(id, importer);
		},
		async fetchModule(id) {
			id = id.replace(/\/\//g, "/");
			return await viteNodeFetch.fetchModule(id).catch((err) => {
				const errorData = err?.data?.data;
				if (!errorData) throw err;
				let _err;
				try {
					const { message, stack } = formatViteError(errorData, id);
					_err = {
						statusText: "Vite Error",
						message,
						stack
					};
				} catch (formatError) {
					consola.warn("Internal nuxt error while formatting vite-node error. Please report this!", formatError);
					const message = `[vite-node] [TransformError] ${errorData?.message || "-"}`;
					consola.error(message, errorData);
					throw {
						statusText: "Vite Error",
						message,
						stack: `${message}\nat ${id}\n` + (errorData?.stack || "")
					};
				}
				throw _err;
			});
		}
	});
}
function formatViteError(errorData, id) {
	const errorCode = errorData.name || errorData.reasonCode || errorData.code;
	const frame = errorData.frame || errorData.source || errorData.pluginCode;
	const getLocId = (locObj = {}) => locObj.file || locObj.id || locObj.url || id || "";
	const getLocPos = (locObj = {}) => locObj.line ? `${locObj.line}:${locObj.column || 0}` : "";
	const locId = getLocId(errorData.loc) || getLocId(errorData.location) || getLocId(errorData.input) || getLocId(errorData);
	const locPos = getLocPos(errorData.loc) || getLocPos(errorData.location) || getLocPos(errorData.input) || getLocPos(errorData);
	const loc = locId.replace(process.cwd(), ".") + (locPos ? `:${locPos}` : "");
	const message = [
		"[vite-node]",
		errorData.plugin && `[plugin:${errorData.plugin}]`,
		errorCode && `[${errorCode}]`,
		loc,
		errorData.reason && `: ${errorData.reason}`,
		frame && `<br><pre>${frame.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")}</pre><br>`
	].filter(Boolean).join(" ");
	return {
		message,
		stack: [
			message,
			`at ${loc}`,
			errorData.stack
		].filter(Boolean).join("\n")
	};
}
export { vite_node_entry_default as default };
