import { t as createSubsystemLogger } from "./subsystem-_lp1q2Mi.js";
import { h as ensureChromeExtensionRelayServer } from "./chrome-7Zn7OYzP.js";
import { r as loadConfig } from "./config-VoJVjSgT.js";
import { a as resolveProfile, i as resolveBrowserConfig, t as createBrowserRouteContext } from "./server-context-CysGziRI.js";

//#region ../../src/browser/control-service.ts
let state = null;
const logService = createSubsystemLogger("browser").child("service");
function getBrowserControlState() {
	return state;
}
function createBrowserControlContext() {
	return createBrowserRouteContext({ getState: () => state });
}
async function startBrowserControlServiceFromConfig() {
	if (state) return state;
	const cfg = loadConfig();
	const resolved = resolveBrowserConfig(cfg.browser, cfg);
	if (!resolved.enabled) return null;
	state = {
		server: null,
		port: resolved.controlPort,
		resolved,
		profiles: /* @__PURE__ */ new Map()
	};
	for (const name of Object.keys(resolved.profiles)) {
		const profile = resolveProfile(resolved, name);
		if (!profile || profile.driver !== "extension") continue;
		await ensureChromeExtensionRelayServer({ cdpUrl: profile.cdpUrl }).catch((err) => {
			logService.warn(`Chrome extension relay init failed for profile "${name}": ${String(err)}`);
		});
	}
	logService.info(`Browser control service ready (profiles=${Object.keys(resolved.profiles).length})`);
	return state;
}
async function stopBrowserControlService() {
	const current = state;
	if (!current) return;
	const ctx = createBrowserRouteContext({ getState: () => state });
	try {
		for (const name of Object.keys(current.resolved.profiles)) try {
			await ctx.forProfile(name).stopRunningBrowser();
		} catch {}
	} catch (err) {
		logService.warn(`edwinpai browser stop failed: ${String(err)}`);
	}
	state = null;
	try {
		await (await import("./pw-ai-BV78euxU.js")).closePlaywrightBrowserConnection();
	} catch {}
}

//#endregion
export { stopBrowserControlService as i, getBrowserControlState as n, startBrowserControlServiceFromConfig as r, createBrowserControlContext as t };