"use strict";
/**
 * Image analysis environment preparation for settings-based profiles.
 *
 * Split from settings-flow.ts to keep that file under the 300 LOC budget.
 * Resolves runtime status, connection, and env overrides for image analysis MCP.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveSettingsImageAnalysisEnv = void 0;
const ui_1 = require("../../utils/ui");
const hooks_1 = require("../../utils/hooks");
const cliproxy_1 = require("../../cliproxy");
const port_manager_1 = require("../../cliproxy/config/port-manager");
/**
 * Resolve the complete image analysis env vars for a settings-based profile launch.
 * Handles native-read fallback and proxy-stopped recovery.
 */
async function resolveSettingsImageAnalysisEnv(ctx) {
    const { profileInfo, resolvedTarget, settings, cliproxyBridge, imageAnalysisMcpReady, imageAnalysisFallbackHookReady, remainingArgs, targetRemainingArgs, } = ctx;
    const imageAnalysisStatus = await (0, hooks_1.resolveImageAnalysisRuntimeStatus)({
        profileName: profileInfo.name,
        profileType: profileInfo.type,
        settings,
        cliproxyBridge,
        sharedHookInstalled: imageAnalysisFallbackHookReady,
    });
    const runtimeConnection = (0, hooks_1.resolveImageAnalysisRuntimeConnection)();
    let imageAnalysisEnv = (0, hooks_1.getImageAnalysisHookEnv)({
        profileName: profileInfo.name,
        profileType: profileInfo.type,
        settings,
        cliproxyBridge,
    });
    imageAnalysisEnv = (0, hooks_1.applyImageAnalysisRuntimeOverrides)(imageAnalysisEnv, {
        backendId: imageAnalysisStatus.backendId,
        model: imageAnalysisStatus.model,
        runtimePath: imageAnalysisStatus.runtimePath,
        baseUrl: runtimeConnection.baseUrl,
        apiKey: runtimeConnection.apiKey,
        allowSelfSigned: runtimeConnection.allowSelfSigned,
    });
    imageAnalysisEnv = {
        ...imageAnalysisEnv,
        CCS_IMAGE_ANALYSIS_SKIP_HOOK: resolvedTarget === 'claude' && imageAnalysisMcpReady ? '1' : '0',
    };
    const imageAnalysisProvider = imageAnalysisEnv['CCS_CURRENT_PROVIDER'];
    if (resolvedTarget === 'claude' &&
        imageAnalysisEnv['CCS_IMAGE_ANALYSIS_SKIP'] !== '1' &&
        imageAnalysisProvider) {
        const verboseProxyLaunch = remainingArgs.includes('--verbose') ||
            remainingArgs.includes('-v') ||
            targetRemainingArgs.includes('--verbose') ||
            targetRemainingArgs.includes('-v');
        if (imageAnalysisStatus.effectiveRuntimeMode === 'native-read') {
            console.error((0, ui_1.info)(`${imageAnalysisStatus.effectiveRuntimeReason || `Image analysis via ${imageAnalysisProvider} is unavailable.`} This session will use native Read.`));
            imageAnalysisEnv = {
                ...imageAnalysisEnv,
                CCS_CURRENT_PROVIDER: '',
                CCS_IMAGE_ANALYSIS_SKIP: '1',
                CCS_IMAGE_ANALYSIS_RUNTIME_PATH: '',
                CCS_IMAGE_ANALYSIS_RUNTIME_BASE_URL: '',
                CCS_IMAGE_ANALYSIS_RUNTIME_API_KEY: '',
                CCS_IMAGE_ANALYSIS_RUNTIME_ALLOW_SELF_SIGNED: '0',
            };
        }
        else if (imageAnalysisStatus.proxyReadiness === 'stopped') {
            const ensureServiceResult = await (0, cliproxy_1.ensureCliproxyService)((0, port_manager_1.resolveLifecyclePort)(), verboseProxyLaunch);
            if (!ensureServiceResult.started) {
                console.error((0, ui_1.warn)(`Image analysis via ${imageAnalysisProvider} is unavailable because CCS could not start the local CLIProxy service. This session will use native Read.`));
                imageAnalysisEnv = {
                    ...imageAnalysisEnv,
                    CCS_CURRENT_PROVIDER: '',
                    CCS_IMAGE_ANALYSIS_SKIP: '1',
                };
            }
        }
    }
    return imageAnalysisEnv;
}
exports.resolveSettingsImageAnalysisEnv = resolveSettingsImageAnalysisEnv;
//# sourceMappingURL=settings-image-analysis-prep.js.map