/**
 * Claude MCP Adapter
 *
 * 实现 IMcpAdapter 接口，负责 Claude Desktop 的 MCP 配置读写
 */
/**
 * JSON 文件读取工具
 */
async function readJsonFile(filePath, fallback) {
    try {
        const raw = await import('node:fs/promises').then(fs => fs.readFile(filePath, "utf-8"));
        const parsed = JSON.parse(raw);
        if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
            return parsed;
        }
        return { ...fallback };
    }
    catch {
        return { ...fallback };
    }
}
/**
 * JSON 文件写入工具
 */
async function writeJsonFile(filePath, content) {
    const fs = await import('node:fs/promises');
    const path = await import('node:path');
    const parent = path.dirname(filePath);
    await fs.mkdir(parent, { recursive: true });
    await fs.writeFile(filePath, JSON.stringify(content, null, 2), "utf-8");
}
/**
 * 生成唯一 ID
 */
function createId(prefix) {
    const timestamp = Date.now().toString(36);
    const random = Math.random().toString(36).slice(2, 8);
    return `${prefix}_${timestamp}_${random}`;
}
/**
 * 将 Claude config 格式转换为 MCP spec
 */
function toMcpSpecFromClaude(input) {
    return input;
}
/**
 * 将 MCP spec 转换为 Claude config 格式
 */
function toClaudeServer(input) {
    return input;
}
export class ClaudeMcpAdapter {
    constructor(configPath) {
        this.configPath = configPath;
        this.appId = "claude";
    }
    async importServers() {
        const config = await readJsonFile(this.configPath, {});
        const entries = Object.entries(config.mcpServers ?? {});
        return entries.map(([name, server]) => ({
            id: createId("mcp"),
            name,
            server: toMcpSpecFromClaude(server),
            apps: {
                acode: false,
                claude: true,
                claudecode: false,
                codex: false,
                gemini: false,
                opencode: false,
                openclaw: false,
            },
        }));
    }
    async syncServer(server) {
        const config = await readJsonFile(this.configPath, {});
        config.mcpServers = config.mcpServers ?? {};
        config.mcpServers[server.name] = toClaudeServer(server.server);
        await writeJsonFile(this.configPath, config);
    }
    async removeServer(server) {
        const config = await readJsonFile(this.configPath, {});
        if (!config.mcpServers) {
            return;
        }
        delete config.mcpServers[server.name];
        await writeJsonFile(this.configPath, config);
    }
}
