/**
 * ACode MCP Adapter
 *
 * 主流程：读取内置 ACode 的 JSON 配置 -> 将 mcpServers 映射导入 SDK ->
 * 按 SDK 状态增删 ACode 配置中的 MCP server。ACode 内置在 runtime-bridge 中，
 * 因此不需要外部 CLI 安装步骤。
 */
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 };
    }
}
async function writeJsonFile(filePath, content) {
    const fs = await import('node:fs/promises');
    const path = await import('node:path');
    await fs.mkdir(path.dirname(filePath), { recursive: true });
    await fs.writeFile(filePath, JSON.stringify(content, null, 2), 'utf-8');
}
function createId(prefix) {
    const timestamp = Date.now().toString(36);
    const random = Math.random().toString(36).slice(2, 8);
    return `${prefix}_${timestamp}_${random}`;
}
export class AcodeMcpAdapter {
    constructor(configPath) {
        this.configPath = configPath;
        this.appId = 'acode';
    }
    async importServers() {
        const config = await readJsonFile(this.configPath, {});
        const entries = Object.entries(config.mcpServers ?? {});
        return entries.map(([name, server]) => ({
            id: createId('mcp'),
            name,
            server,
            apps: {
                acode: true,
                claude: false,
                claudecode: false,
                codex: false,
                gemini: false,
                opencode: false,
                openclaw: false,
            },
        }));
    }
    async syncServer(server) {
        const config = await readJsonFile(this.configPath, {});
        config.mcpServers = {
            ...(config.mcpServers ?? {}),
            [server.name]: server.server,
        };
        await writeJsonFile(this.configPath, config);
    }
    async removeServer(server) {
        const config = await readJsonFile(this.configPath, {});
        if (!config.mcpServers?.[server.name]) {
            return;
        }
        delete config.mcpServers[server.name];
        await writeJsonFile(this.configPath, config);
    }
}
