/**
 * CC-Switch SDK 包装层
 *
 * 负责创建 SDK 实例并注入 adapters。
 * adapters 实现在 src/adapters/cc-switch/ 中。
 */
import path from 'node:path';
import { AiConfigService, CcSwitchSdk } from '@cc-switch/sdk';
import { createMcpAdapters, createSkillAdapters } from '../adapters/cc-switch/index.js';
import { getClaudeConfigFile, getClaudeMcpConfigFile, getClaudeSkillsDir, getCodexConfigFile, getCodexSkillsDir, getRuntimeHomeDir, getOpencodeConfigFile, getOpencodeSkillsDir, } from '../config.js';
import { getAgentInitRoot } from './instance-state.js';
import { InstanceService } from './instance-service.js';
export function getCcSwitchAppPaths() {
    const runtimeHome = path.resolve(getRuntimeHomeDir());
    return {
        runtimeHome,
        claudeConfigFile: path.resolve(getClaudeConfigFile(runtimeHome)),
        claudeMcpConfigFile: path.resolve(getClaudeMcpConfigFile(runtimeHome)),
        claudeSkillsDir: path.resolve(getClaudeSkillsDir(runtimeHome)),
        codexConfigFile: path.resolve(getCodexConfigFile(runtimeHome)),
        codexSkillsDir: path.resolve(getCodexSkillsDir(runtimeHome)),
        opencodeConfigFile: path.resolve(getOpencodeConfigFile(runtimeHome)),
        opencodeSkillsDir: path.resolve(getOpencodeSkillsDir(runtimeHome)),
    };
}
export function getCcSwitchSdk(agentId) {
    const initRoot = getAgentInitRoot(agentId);
    const sdkRoot = path.join(initRoot, 'cc-switch-sdk');
    const appPaths = getCcSwitchAppPaths();
    const paths = {
        stateFile: path.join(sdkRoot, 'state.json'),
        backupDir: path.join(sdkRoot, 'backups'),
        claudeConfigFile: appPaths.claudeConfigFile,
        claudeSkillsDir: appPaths.claudeSkillsDir,
        claudecodeConfigFile: appPaths.claudeMcpConfigFile,
        claudecodeSkillsDir: appPaths.claudeSkillsDir, // 使用 claude 路径
        codexConfigFile: appPaths.codexConfigFile,
        codexSkillsDir: appPaths.codexSkillsDir,
        opencodeConfigFile: appPaths.opencodeConfigFile,
        opencodeSkillsDir: appPaths.opencodeSkillsDir,
        ssotSkillsDir: path.join(sdkRoot, 'skills'),
    };
    // 创建 adapters 实例并注入
    const mcpAdapters = createMcpAdapters(paths);
    const skillAdapters = createSkillAdapters(paths);
    return new CcSwitchSdk({
        rootDir: sdkRoot,
        paths,
        adapters: {
            mcp: mcpAdapters,
            skill: skillAdapters,
        },
    });
}
export async function loadCcSwitchSdk(agentId) {
    const sdk = getCcSwitchSdk(agentId);
    await sdk.load();
    return sdk;
}
export async function discoverCcSwitchConfiguredItems(sdk) {
    const [mcpServers, skills] = await Promise.all([
        sdk.importMcpFromApps(),
        sdk.importSkillsFromApps(),
    ]);
    return { mcpServers, skills };
}
export function getAiConfigService() {
    const appPaths = getCcSwitchAppPaths();
    return new AiConfigService({
        claudecodeConfigFile: appPaths.claudeConfigFile,
        codexConfigFile: appPaths.codexConfigFile,
        opencodeConfigFile: appPaths.opencodeConfigFile,
    });
}
export function getInstanceService(agentId, workspacePath) {
    const sdk = getCcSwitchSdk(agentId);
    const initRoot = getAgentInitRoot(agentId);
    return new InstanceService(sdk, initRoot, workspacePath);
}
export async function runCcSwitchAdapter(action, agentId, payload, sdk) {
    const activeSdk = sdk || (await loadCcSwitchSdk(agentId));
    const appPaths = getCcSwitchAppPaths();
    return {
        ok: true,
        adapter: 'cc-switch-sdk',
        action,
        agentId: String(agentId),
        payload,
        paths: appPaths,
        snapshot: {
            skills: activeSdk.getAllSkills().length,
            mcpServers: activeSdk.getAllMcpServers().length,
        },
    };
}
export function toSdkAppsFromEnabledTools(enabledTools) {
    const tools = Array.isArray(enabledTools) ? enabledTools : [];
    const includes = (name) => tools.includes(name);
    return {
        claude: false,
        claudecode: includes('claudecode') || includes('claude'),
        codex: includes('codex'),
        gemini: includes('gemini'),
        opencode: includes('opencode'),
        openclaw: includes('openclaw'),
    };
}
export function toSdkApps(apps) {
    if (!apps) {
        return {
            claude: false,
            claudecode: false,
            codex: false,
            gemini: false,
            opencode: false,
            openclaw: false,
        };
    }
    return {
        claude: false,
        claudecode: Boolean(apps.claudecode) || Boolean(apps.claude),
        codex: Boolean(apps.codex),
        gemini: Boolean(apps.gemini),
        opencode: Boolean(apps.opencode),
        openclaw: Boolean(apps.openclaw),
    };
}
