"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.setupMcp = setupMcp;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const jsonc_1 = require("./jsonc");
function setupMcp(baseDir, options) {
    const mcpServerPath = path.resolve(baseDir, 'bin', 'mcp-server.js');
    const result = {
        mcpServerFound: fs.existsSync(mcpServerPath),
        mcpServerPath,
        configs: [],
    };
    if (!result.mcpServerFound)
        return result;
    const targets = [
        { path: path.join(os.homedir(), '.claude.json'), name: 'Claude Code' },
        {
            path: process.platform === 'win32'
                ? path.join(process.env.APPDATA || '', 'Claude', 'claude_desktop_config.json')
                : process.platform === 'linux'
                    ? path.join(os.homedir(), '.config', 'Claude', 'claude_desktop_config.json')
                    : path.join(os.homedir(), 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json'),
            name: 'Claude Desktop',
        },
    ];
    for (const target of targets) {
        if (target.name === 'Claude Desktop' && !fs.existsSync(path.dirname(target.path))) {
            result.configs.push({ ...target, status: 'skipped' });
            continue;
        }
        let data = {};
        const fileExists = fs.existsSync(target.path);
        if (fileExists) {
            try {
                data = (0, jsonc_1.parseJsonc)(fs.readFileSync(target.path, 'utf-8'));
            }
            catch {
                if (options?.force) {
                    const backupDir = path.join(os.homedir(), '.splitgame', 'backups');
                    if (!fs.existsSync(backupDir))
                        fs.mkdirSync(backupDir, { recursive: true });
                    const backupName = path.basename(target.path) + '.backup.' + Date.now();
                    fs.copyFileSync(target.path, path.join(backupDir, backupName));
                    data = {};
                }
                else {
                    result.configs.push({ ...target, status: 'parse-error' });
                    continue;
                }
            }
        }
        if (!data.mcpServers)
            data.mcpServers = {};
        if (data.mcpServers['splitgame'] && !options?.force) {
            result.configs.push({ ...target, status: 'exists' });
            continue;
        }
        try {
            const dir = path.dirname(target.path);
            if (!fs.existsSync(dir))
                fs.mkdirSync(dir, { recursive: true });
            const tmpPath = target.path + '.tmp';
            data.mcpServers['splitgame'] = { command: 'node', args: [mcpServerPath] };
            fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2) + '\n', 'utf-8');
            fs.renameSync(tmpPath, target.path);
            result.configs.push({ ...target, status: 'configured' });
        }
        catch {
            result.configs.push({ ...target, status: 'skipped' });
        }
    }
    return result;
}
//# sourceMappingURL=mcp-setup.js.map