import { buildToolSpecificAiConfig, } from '@cc-switch/sdk';
function normalizeTemplate(template) {
    return {
        baseUrl: String(template?.baseUrl || ''),
        apiKey: String(template?.apiKey || ''),
        model: String(template?.model || ''),
        models: Array.isArray(template?.models)
            ? template.models.map((item) => String(item || '').trim()).filter(Boolean)
            : undefined,
    };
}
function isInstalled(toolStatus, tool) {
    return Boolean(toolStatus[tool]?.installed);
}
function isClaudeCodeInstalled(toolStatus) {
    return isInstalled(toolStatus, 'claude') || isInstalled(toolStatus, 'claudecode');
}
function isRecord(value) {
    return Boolean(value && typeof value === 'object' && !Array.isArray(value));
}
function hasConfigContent(value) {
    return Object.keys(value).length > 0;
}
/**
 * 具体逻辑：将服务端已生成的 per-tool AI 配置规范化为可应用 patch，空对象视为未配置。
 */
function normalizeRequestedConfig(value) {
    if (!isRecord(value) || !hasConfigContent(value)) {
        return null;
    }
    return value;
}
/**
 * 主流程：用当前统一 AI 配置覆盖各 Agent CLI 配置，并跳过未安装 CLI。
 */
export async function applyCurrentAiConfigToInstalledTools(options) {
    const generatedConfig = options.template
        ? buildToolSpecificAiConfig(normalizeTemplate(options.template))
        : null;
    const requestedAcodeConfig = normalizeRequestedConfig(options.requestedSources?.acode);
    const requestedClaudeConfig = normalizeRequestedConfig(options.requestedSources?.claudecode);
    const requestedCodexConfig = normalizeRequestedConfig(options.requestedSources?.codex);
    const requestedOpenCodeConfig = normalizeRequestedConfig(options.requestedSources?.opencode);
    const acodeConfig = generatedConfig?.acode || requestedAcodeConfig;
    const claudeConfig = generatedConfig?.claudecode || requestedClaudeConfig;
    const codexConfig = generatedConfig?.codex || requestedCodexConfig;
    const openCodeConfig = generatedConfig?.opencode || requestedOpenCodeConfig;
    const skippedTools = Object.values(options.toolStatus)
        .filter((item) => !item.installed)
        .map((item) => item.tool);
    let mergedAcode = null;
    let mergedClaude = null;
    let mergedCodex = null;
    let mergedOpencode = null;
    if (acodeConfig && isInstalled(options.toolStatus, 'acode')) {
        mergedAcode = await options.aiConfigService.applyAcodeConfig(acodeConfig);
    }
    if (claudeConfig && isClaudeCodeInstalled(options.toolStatus)) {
        mergedClaude = await options.aiConfigService.applyClaudeCodeConfig(claudeConfig);
    }
    if (codexConfig && isInstalled(options.toolStatus, 'codex')) {
        mergedCodex = await options.aiConfigService.applyCodexConfig(codexConfig);
    }
    if (openCodeConfig && isInstalled(options.toolStatus, 'opencode')) {
        mergedOpencode = await options.aiConfigService.applyOpenCodeConfig(openCodeConfig);
    }
    return {
        generatedFromUnifiedTemplate: Boolean(generatedConfig),
        applied: {
            acode: acodeConfig && mergedAcode ? Object.keys(acodeConfig) : [],
            claudecode: claudeConfig && mergedClaude ? Object.keys(claudeConfig) : [],
            codex: codexConfig && mergedCodex ? Object.keys(codexConfig) : [],
            opencode: openCodeConfig && mergedOpencode ? Object.keys(openCodeConfig) : [],
        },
        skippedTools,
        merged: {
            acode: mergedAcode,
            claudecode: mergedClaude,
            codex: mergedCodex,
            opencode: mergedOpencode,
        },
    };
}
