import { r as runCli } from "./bindings-CBKV8AbA.js";
import { i as resolvePlugins } from "./apis-rMlkuPw1.js";
import Tinypool from "tinypool";
import { fileURLToPath, pathToFileURL } from "node:url";
import { extname } from "node:path";
//#region src-js/cli/worker-proxy.ts
let pool = null;
async function initExternalFormatter(numThreads) {
	pool = new Tinypool({
		filename: new URL("./cli-worker.js", import.meta.url).href,
		minThreads: numThreads,
		maxThreads: numThreads,
		runtime: "child_process",
		env: process.env
	});
	return resolvePlugins();
}
async function disposeExternalFormatter() {
	await pool?.destroy();
	pool = null;
}
async function formatFile(options, code) {
	return pool.run({
		options,
		code
	}, { name: "formatFile" }).catch((err) => {
		if (err instanceof Error) throw err;
		if (err !== null && typeof err === "object") {
			const obj = err;
			const newErr = new Error(obj.message);
			newErr.name = obj.name;
			throw newErr;
		}
		throw new Error(String(err));
	});
}
async function formatEmbeddedCode(options, code) {
	return pool.run({
		options,
		code
	}, { name: "formatEmbeddedCode" }).catch(() => null);
}
async function formatEmbeddedDoc(options, texts) {
	return pool.run({
		options,
		texts
	}, { name: "formatEmbeddedDoc" }).catch(() => null);
}
async function sortTailwindClasses(options, classes) {
	return pool.run({
		classes,
		options
	}, { name: "sortTailwindClasses" }).catch(() => null);
}
//#endregion
//#region src-js/cli/js_config/node_version.ts
const NODE_TYPESCRIPT_SUPPORT_RANGE = "^20.19.0 || >=22.12.0";
const TS_MODULE_EXTENSIONS = new Set([
	".ts",
	".mts",
	".cts"
]);
function getUnsupportedTypeScriptModuleLoadHint(err, specifier, nodeVersion = process.version) {
	if (!isTypeScriptModuleSpecifier(specifier) || !isUnknownFileExtensionError(err)) return null;
	return `TypeScript config files require Node.js ${NODE_TYPESCRIPT_SUPPORT_RANGE}.\nDetected Node.js ${nodeVersion}.\nPlease upgrade Node.js or use a JSON config file instead.`;
}
function isTypeScriptModuleSpecifier(specifier) {
	const ext = extname(normalizeModuleSpecifierPath(specifier)).toLowerCase();
	return TS_MODULE_EXTENSIONS.has(ext);
}
function normalizeModuleSpecifierPath(specifier) {
	if (!specifier.startsWith("file:")) return specifier;
	try {
		return fileURLToPath(specifier);
	} catch {
		return specifier;
	}
}
function isUnknownFileExtensionError(err) {
	if (err?.code === "ERR_UNKNOWN_FILE_EXTENSION") return true;
	const message = err?.message;
	return typeof message === "string" && /unknown(?: or unsupported)? file extension/i.test(message);
}
//#endregion
//#region src-js/cli/js_config/index.ts
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
/**
* Load a JavaScript/TypeScript config file and import it.
*
* Uses native Node.js `import()` to evaluate the config file.
* The config file should have a default export containing the oxfmt configuration object.
*/
async function importJsConfig(path) {
	const fileUrl = pathToFileURL(path);
	fileUrl.searchParams.set("cache", Date.now().toString());
	const { default: config } = await import(fileUrl.href).catch((err) => {
		const hint = getUnsupportedTypeScriptModuleLoadHint(err, path);
		if (hint && err instanceof Error) err.message += `\n\n${hint}`;
		throw err;
	});
	if (config === void 0) throw new Error("Configuration file has no default export.");
	return config;
}
/**
* Load and validate a standard oxfmt JS/TS config file.
* The default export must be a plain object containing oxfmt options.
*
* @param path - Absolute path to the JavaScript/TypeScript config file
* @returns Config object
*/
async function loadJsConfig(path) {
	const config = await importJsConfig(path);
	if (!isObject(config)) throw new Error("Configuration file must have a default export that is an object.");
	return config;
}
const VITE_OXFMT_CONFIG_FIELD = "fmt";
/**
* Load a Vite+ config file (`vite.config.ts`) and extract the `.fmt` field.
*
* @param path - Absolute path to the Vite config file
* @returns Config object from `.fmt` field, or `null` to signal "skip"
*/
async function loadVitePlusConfig(path) {
	const config = await importJsConfig(path);
	if (!isObject(config)) return null;
	const fmtConfig = config[VITE_OXFMT_CONFIG_FIELD];
	if (fmtConfig === void 0) return null;
	if (!isObject(fmtConfig)) throw new Error(`The \`${VITE_OXFMT_CONFIG_FIELD}\` field in the default export must be an object.`);
	return fmtConfig;
}
//#endregion
//#region src-js/cli.ts
(async () => {
	const args = process.argv.slice(2);
	if (!process.stdout.isTTY) process.stdout._handle?.setBlocking?.(true);
	if (!process.stdin.isTTY) process.stdin._handle?.setBlocking?.(true);
	if (args.includes("--lsp")) process.stdout.write = process.stderr.write.bind(process.stderr);
	const [mode, exitCode] = await runCli(args, process.env.VP_VERSION ? loadVitePlusConfig : loadJsConfig, initExternalFormatter, formatFile, formatEmbeddedCode, formatEmbeddedDoc, sortTailwindClasses);
	if (mode === "init") {
		await import("./init-BbKOMZ57.js").then((m) => m.runInit());
		return;
	}
	if (mode === "migrate:prettier") {
		await import("./migrate-prettier-B19hw9Dn.js").then((m) => m.runMigratePrettier());
		return;
	}
	if (mode === "migrate:biome") {
		await import("./migrate-biome-BMqs7-eg.js").then((m) => m.runMigrateBiome());
		return;
	}
	await disposeExternalFormatter();
	process.exitCode = exitCode;
	const [major, minor] = process.versions.node.split(".").map(Number);
	if (major < 25 || major === 25 && minor < 4) setTimeout(() => process.exit(), 50);
})();
//#endregion
export {};
