"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runBrowserSetup = void 0;
const platform_1 = require("./platform");
const browser_status_1 = require("./browser-status");
const mcp_installer_1 = require("./mcp-installer");
const browser_settings_1 = require("./browser-settings");
const config_loader_facade_1 = require("../../config/config-loader-facade");
const defaultBrowserSetupDeps = {
    getBrowserConfig: config_loader_facade_1.getBrowserConfig,
    mutateConfig: config_loader_facade_1.mutateConfig,
    ensureBrowserMcp: mcp_installer_1.ensureBrowserMcp,
    getBrowserStatus: browser_status_1.getBrowserStatus,
};
async function runBrowserSetup(deps = defaultBrowserSetupDeps) {
    const initialConfig = deps.getBrowserConfig();
    const configUpdated = persistBrowserSetupConfig(deps, initialConfig);
    const persistedConfig = deps.getBrowserConfig();
    const effectiveConfig = (0, browser_settings_1.getEffectiveClaudeBrowserAttachConfig)(persistedConfig);
    const createdUserDataDir = (0, browser_settings_1.isManagedClaudeBrowserAttachConfig)(effectiveConfig)
        ? (0, browser_settings_1.ensureManagedBrowserUserDataDir)(effectiveConfig).createdProfileDir
        : false;
    const mcpReady = deps.ensureBrowserMcp();
    const status = await deps.getBrowserStatus();
    const notes = [];
    if (effectiveConfig.overrideActive) {
        notes.push(`Current session is using ${effectiveConfig.source}; remove that override if you want saved browser settings to take effect in future shells.`);
    }
    if (!mcpReady) {
        notes.push('CCS could not fully prepare the local browser MCP runtime.');
    }
    if (!(0, browser_settings_1.isManagedClaudeBrowserAttachConfig)(effectiveConfig)) {
        notes.push('Setup did not create the current browser user-data dir because it is not the CCS-managed default path.');
    }
    const platform = (0, platform_1.getNodePlatformKey)();
    return {
        configUpdated,
        createdUserDataDir,
        mcpReady,
        overrideActive: effectiveConfig.overrideActive,
        ready: status.claude.state === 'ready' && mcpReady,
        launchCommand: status.claude.launchCommands[platform],
        status,
        notes,
    };
}
exports.runBrowserSetup = runBrowserSetup;
function persistBrowserSetupConfig(deps, currentConfig) {
    const before = JSON.stringify(currentConfig);
    deps.mutateConfig((config) => {
        const existingBrowser = config.browser ?? currentConfig;
        const currentUserDataDir = existingBrowser.claude.user_data_dir?.trim();
        config.browser = {
            claude: {
                enabled: true,
                policy: existingBrowser.claude.policy,
                user_data_dir: currentUserDataDir || (0, browser_settings_1.getRecommendedBrowserUserDataDir)(),
                devtools_port: currentConfig.claude.devtools_port,
            },
            codex: {
                enabled: existingBrowser.codex.enabled,
                policy: existingBrowser.codex.policy,
            },
        };
    });
    return JSON.stringify(deps.getBrowserConfig()) !== before;
}
//# sourceMappingURL=browser-setup.js.map