"use strict";
/**
 * Default dispatch flow — no profile configured, use Claude's own defaults.
 *
 * Extracted from src/ccs.ts main() else branch (profileInfo.type === 'default').
 * Skip WebSearch hook — native Claude has server-side WebSearch.
 * Skip Image Analyzer hook — native Claude has native vision support.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.runDefaultFlow = void 0;
const ui_1 = require("../../utils/ui");
const browser_1 = require("../../utils/browser");
const shell_executor_1 = require("../../utils/shell-executor");
const targets_1 = require("../../targets");
const environment_builder_1 = require("../environment-builder");
async function runDefaultFlow(ctx) {
    const { profileInfo, resolvedTarget, claudeCli, targetAdapter, targetBinaryInfo, targetRemainingArgs, nativeClaudeRemainingArgs, runtimeReasoningOverride, codexRuntimeConfigOverrides, claudeBrowserExposure, claudeAttachConfig, resolveProfileContinuityInheritance, } = ctx;
    const envVars = {
        CCS_PROFILE_TYPE: 'default',
        CCS_WEBSEARCH_SKIP: '1',
        CCS_IMAGE_ANALYSIS_SKIP: '1',
    };
    const browserAttachRuntime = resolvedTarget === 'claude' &&
        claudeBrowserExposure?.exposeForLaunch &&
        claudeAttachConfig?.enabled
        ? await (0, browser_1.resolveOptionalBrowserAttachRuntime)(claudeAttachConfig)
        : undefined;
    const browserRuntimeEnv = browserAttachRuntime?.runtimeEnv;
    if (browserAttachRuntime?.warning) {
        process.stderr.write(`${(0, ui_1.warn)(browserAttachRuntime.warning)}\n`);
    }
    if (resolvedTarget === 'claude') {
        if (browserRuntimeEnv) {
            (0, browser_1.ensureBrowserMcpOrThrow)();
            Object.assign(envVars, browserRuntimeEnv);
        }
        const defaultContinuityInheritance = await resolveProfileContinuityInheritance({
            profileName: profileInfo.name,
            profileType: profileInfo.type,
            target: resolvedTarget,
        });
        if (defaultContinuityInheritance.sourceAccount && process.env.CCS_DEBUG) {
            console.error((0, ui_1.info)(`Continuity inheritance active: profile "${profileInfo.name}" -> account "${defaultContinuityInheritance.sourceAccount}"`));
        }
        if (defaultContinuityInheritance.claudeConfigDir) {
            envVars.CLAUDE_CONFIG_DIR = defaultContinuityInheritance.claudeConfigDir;
            if (browserRuntimeEnv &&
                !(0, browser_1.syncBrowserMcpToConfigDir)(defaultContinuityInheritance.claudeConfigDir)) {
                throw new Error('Browser MCP is enabled, but CCS could not sync the browser MCP config into the inherited Claude instance.');
            }
        }
    }
    // Dispatch through target adapter for non-claude targets
    if (resolvedTarget !== 'claude') {
        const adapter = targetAdapter;
        if (!adapter) {
            console.error((0, ui_1.fail)(`Target adapter not found for "${resolvedTarget}"`));
            process.exit(1);
        }
        if (!adapter.supportsProfileType('default')) {
            console.error((0, ui_1.fail)(`${adapter.displayName} does not support default profile mode`));
            process.exit(1);
        }
        const creds = {
            profile: 'default',
            baseUrl: process.env['ANTHROPIC_BASE_URL'] || '',
            apiKey: process.env['ANTHROPIC_AUTH_TOKEN'] || '',
            model: process.env['ANTHROPIC_MODEL'],
            provider: (0, targets_1.resolveDroidProvider)({
                provider: process.env['CCS_DROID_PROVIDER'] || process.env['DROID_PROVIDER'],
                baseUrl: process.env['ANTHROPIC_BASE_URL'],
                model: process.env['ANTHROPIC_MODEL'],
            }),
            reasoningOverride: runtimeReasoningOverride,
            runtimeConfigOverrides: codexRuntimeConfigOverrides,
            browserRuntimeEnv,
        };
        if (resolvedTarget === 'droid' && (!creds.baseUrl || !creds.apiKey)) {
            console.error((0, ui_1.fail)(`${adapter.displayName} default mode requires ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN`));
            console.error((0, ui_1.info)('Use a settings-based profile instead: ccs glm --target droid'));
            process.exit(1);
        }
        await adapter.prepareCredentials(creds);
        const targetArgs = adapter.buildArgs('default', targetRemainingArgs, {
            creds,
            profileType: 'default',
            binaryInfo: targetBinaryInfo || undefined,
        });
        const targetEnv = adapter.buildEnv(creds, 'default');
        adapter.exec(targetArgs, targetEnv, { binaryInfo: targetBinaryInfo || undefined });
        return;
    }
    const launchArgs = (0, environment_builder_1.resolveNativeClaudeLaunchArgs)(browserRuntimeEnv
        ? (0, browser_1.appendBrowserToolArgs)(nativeClaudeRemainingArgs)
        : nativeClaudeRemainingArgs, 'default', envVars.CLAUDE_CONFIG_DIR);
    (0, shell_executor_1.execClaude)(claudeCli, launchArgs, envVars);
}
exports.runDefaultFlow = runDefaultFlow;
//# sourceMappingURL=default-flow.js.map