"use strict";
/**
 * Browser Launch Setup — Executor-level browser initialization
 *
 * Extracted from executor/index.ts (Phase 04).
 * Handles:
 * 1. Browser launch flag resolution and override parsing
 * 2. Browser attach config + exposure resolution + blocked-override warning
 * 3. Optional browser attach runtime resolution (devtools WebSocket)
 * 4. Browser MCP ensure + sync-to-config-dir
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveBrowserRuntime = exports.resolveBrowserLaunchFlags = void 0;
const ui_1 = require("../../utils/ui");
const browser_1 = require("../../utils/browser");
const config_loader_facade_1 = require("../../config/config-loader-facade");
/**
 * Phase 1 — resolve browser CLI flags and attach config.
 * Call this immediately after resolveExecutorProxy so that
 * argsWithoutBrowserFlags is available for downstream parsing.
 *
 * @returns partial setup result (no async work yet)
 */
function resolveBrowserLaunchFlags(argsWithoutProxy) {
    let browserLaunchOverride;
    let argsWithoutBrowserFlags = argsWithoutProxy;
    try {
        const browserLaunchFlags = (0, browser_1.resolveBrowserLaunchFlagResolution)(argsWithoutProxy);
        browserLaunchOverride = browserLaunchFlags.override;
        argsWithoutBrowserFlags = browserLaunchFlags.argsWithoutFlags;
    }
    catch (error) {
        console.error((0, ui_1.warn)(error.message));
        process.exit(1);
        return { browserLaunchOverride: undefined, argsWithoutBrowserFlags };
    }
    const browserConfig = (0, config_loader_facade_1.getBrowserConfig)();
    const browserAttachConfig = (0, browser_1.getEffectiveClaudeBrowserAttachConfig)(browserConfig);
    const claudeBrowserExposure = (0, browser_1.resolveBrowserExposure)({
        enabled: browserAttachConfig.enabled,
        policy: browserConfig.claude.policy,
    }, browserLaunchOverride);
    const blockedBrowserOverrideWarning = (0, browser_1.getBlockedBrowserOverrideWarning)('Claude Browser Attach', claudeBrowserExposure);
    if (blockedBrowserOverrideWarning) {
        console.error((0, ui_1.warn)(blockedBrowserOverrideWarning));
    }
    return { browserLaunchOverride, argsWithoutBrowserFlags };
}
exports.resolveBrowserLaunchFlags = resolveBrowserLaunchFlags;
/**
 * Phase 2 — resolve async browser attach runtime and MCP setup.
 * Must be called AFTER phase-1 and AFTER ensureWebSearchMcpOrThrow().
 */
async function resolveBrowserRuntime(browserLaunchOverride, inheritedClaudeConfigDir) {
    const browserConfig = (0, config_loader_facade_1.getBrowserConfig)();
    const browserAttachConfig = (0, browser_1.getEffectiveClaudeBrowserAttachConfig)(browserConfig);
    const claudeBrowserExposure = (0, browser_1.resolveBrowserExposure)({
        enabled: browserAttachConfig.enabled,
        policy: browserConfig.claude.policy,
    }, browserLaunchOverride);
    const browserAttachRuntime = browserAttachConfig.enabled && claudeBrowserExposure.exposeForLaunch
        ? await (0, browser_1.resolveOptionalBrowserAttachRuntime)(browserAttachConfig)
        : undefined;
    const browserRuntimeEnv = browserAttachRuntime?.runtimeEnv;
    if (browserAttachRuntime?.warning) {
        process.stderr.write(`${(0, ui_1.warn)(browserAttachRuntime.warning)}\n`);
    }
    if (browserRuntimeEnv) {
        (0, browser_1.ensureBrowserMcpOrThrow)();
    }
    // Sync browser MCP config into inherited Claude instance if browser is active
    if (browserRuntimeEnv && inheritedClaudeConfigDir) {
        if (!(0, browser_1.syncBrowserMcpToConfigDir)(inheritedClaudeConfigDir)) {
            throw new Error('Browser MCP is enabled, but CCS could not sync the browser MCP config into the inherited Claude instance.');
        }
    }
    return { browserRuntimeEnv };
}
exports.resolveBrowserRuntime = resolveBrowserRuntime;
//# sourceMappingURL=browser-launch-setup.js.map