"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNativeMcpPath = getNativeMcpPath;
exports.readNativeMcp = readNativeMcp;
exports.writeNativeMcp = writeNativeMcp;
const path = __importStar(require("path"));
const fs_1 = require("fs");
/** Determine the native MCP config path for a given agent. */
async function getNativeMcpPath(adapterName, projectRoot) {
    const candidates = [];
    switch (adapterName) {
        case 'GitHub Copilot':
            candidates.push(path.join(projectRoot, '.vscode', 'mcp.json'));
            break;
        case 'Visual Studio':
            candidates.push(path.join(projectRoot, '.mcp.json'));
            candidates.push(path.join(projectRoot, '.vs', 'mcp.json'));
            break;
        case 'Cursor':
            candidates.push(path.join(projectRoot, '.cursor', 'mcp.json'));
            break;
        case 'Windsurf':
            candidates.push(path.join(projectRoot, '.windsurf', 'mcp_config.json'));
            break;
        case 'Claude Code':
            candidates.push(path.join(projectRoot, '.mcp.json'));
            break;
        case 'OpenAI Codex CLI':
            candidates.push(path.join(projectRoot, '.codex', 'config.toml'));
            break;
        case 'Aider':
            candidates.push(path.join(projectRoot, '.mcp.json'));
            break;
        case 'Open Hands':
            // For Open Hands, we target the main config file, not a separate mcp.json
            candidates.push(path.join(projectRoot, 'config.toml'));
            break;
        case 'Gemini CLI':
            candidates.push(path.join(projectRoot, '.gemini', 'settings.json'));
            break;
        case 'Junie':
            candidates.push(path.join(projectRoot, '.junie', 'mcp', 'mcp.json'));
            break;
        case 'Qwen Code':
            candidates.push(path.join(projectRoot, '.qwen', 'settings.json'));
            break;
        case 'Kilo Code':
            candidates.push(path.join(projectRoot, '.kilocode', 'mcp.json'));
            break;
        case 'Kiro':
            candidates.push(path.join(projectRoot, '.kiro', 'settings', 'mcp.json'));
            break;
        case 'OpenCode':
            candidates.push(path.join(projectRoot, 'opencode.json'));
            break;
        case 'Firebase Studio':
            candidates.push(path.join(projectRoot, '.idx', 'mcp.json'));
            break;
        case 'Factory Droid':
            candidates.push(path.join(projectRoot, '.factory', 'mcp.json'));
            break;
        case 'Zed':
            // Only consider project-local Zed settings (avoid writing to user home directory)
            candidates.push(path.join(projectRoot, '.zed', 'settings.json'));
            break;
        default:
            return null;
    }
    for (const p of candidates) {
        try {
            await fs_1.promises.access(p);
            return p;
        }
        catch {
            // continue
        }
    }
    // default to first candidate if none exist
    return candidates.length > 0 ? candidates[0] : null;
}
/** Read native MCP config from disk, or return empty object if missing/invalid. */
async function readNativeMcp(filePath) {
    try {
        const text = await fs_1.promises.readFile(filePath, 'utf8');
        return JSON.parse(text);
    }
    catch {
        return {};
    }
}
/** Write native MCP config to disk, creating parent directories as needed. */
async function writeNativeMcp(filePath, data) {
    await fs_1.promises.mkdir(path.dirname(filePath), { recursive: true });
    const text = JSON.stringify(data, null, 2) + '\n';
    await fs_1.promises.writeFile(filePath, text, 'utf8');
}
