import { createRequire } from "node:module";
import { existsSync } from "node:fs";
import { stat } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
//#region src/browser/providers/playwright-doctor.ts
/** Root directory of the `playwright-core` package (Electron extraResources or node_modules). */
function resolvePlaywrightCoreRoot() {
	const envRoot = process.env.XOPC_PLAYWRIGHT_CORE_ROOT?.trim();
	if (envRoot) {
		if (!existsSync(join(envRoot, "package.json"))) throw new Error(`playwright-core package.json not found under XOPC_PLAYWRIGHT_CORE_ROOT (${envRoot})`);
		return envRoot;
	}
	return dirname(createRequire(fileURLToPath(import.meta.url)).resolve("playwright-core/package.json"));
}
/** Absolute path to bundled `playwright-core/cli.js` (same revision as runtime). */
function resolvePlaywrightCoreCliPath() {
	return join(resolvePlaywrightCoreRoot(), "cli.js");
}
/** Load playwright-core (Electron extraResources or node_modules). */
async function loadPlaywrightCoreModule() {
	const envRoot = process.env.XOPC_PLAYWRIGHT_CORE_ROOT?.trim();
	if (envRoot) {
		const entry = join(envRoot, "index.js");
		return createRequire(import.meta.url)(entry);
	}
	return import("playwright-core");
}
/** Check whether playwright-core's default Chromium revision is on disk. */
async function playwrightChromiumDoctor() {
	try {
		const pw = await loadPlaywrightCoreModule();
		const chromium = pw.chromium ?? pw.default?.chromium;
		if (!chromium?.executablePath) return {
			installed: false,
			reason: "playwright-core missing"
		};
		let executablePath;
		try {
			executablePath = chromium.executablePath();
		} catch (err) {
			return {
				installed: false,
				reason: err instanceof Error ? err.message : String(err)
			};
		}
		try {
			return {
				installed: (await stat(executablePath)).isFile(),
				executablePath
			};
		} catch {
			return {
				installed: false,
				executablePath,
				reason: "Chromium executable not found on disk"
			};
		}
	} catch (e) {
		return {
			installed: false,
			reason: e instanceof Error ? e.message : String(e)
		};
	}
}
//#endregion
export { loadPlaywrightCoreModule, playwrightChromiumDoctor, resolvePlaywrightCoreCliPath, resolvePlaywrightCoreRoot };

//# sourceMappingURL=playwright-doctor.js.map