"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.readVSCodeSettings = readVSCodeSettings;
exports.writeVSCodeSettings = writeVSCodeSettings;
exports.transformRulerToAugmentMcp = transformRulerToAugmentMcp;
exports.mergeAugmentMcpServers = mergeAugmentMcpServers;
exports.getVSCodeSettingsPath = getVSCodeSettingsPath;
const fs_1 = require("fs");
const path = __importStar(require("path"));
/**
 * Read VSCode settings.json file
 */
async function readVSCodeSettings(settingsPath) {
    try {
        const content = await fs_1.promises.readFile(settingsPath, 'utf8');
        return JSON.parse(content);
    }
    catch (error) {
        if (error.code === 'ENOENT') {
            return {};
        }
        throw error;
    }
}
/**
 * Write VSCode settings.json file
 */
async function writeVSCodeSettings(settingsPath, settings) {
    await fs_1.promises.mkdir(path.dirname(settingsPath), { recursive: true });
    await fs_1.promises.writeFile(settingsPath, JSON.stringify(settings, null, 4));
}
/**
 * Transform ruler MCP config to Augment MCP server array format
 */
function transformRulerToAugmentMcp(rulerMcpJson) {
    const servers = [];
    if (rulerMcpJson.mcpServers && typeof rulerMcpJson.mcpServers === 'object') {
        const mcpServers = rulerMcpJson.mcpServers;
        for (const [name, serverConfig] of Object.entries(mcpServers)) {
            const augmentServer = {
                name,
                command: serverConfig.command,
            };
            if (serverConfig.args) {
                augmentServer.args = serverConfig.args;
            }
            if (serverConfig.env) {
                augmentServer.env = serverConfig.env;
            }
            servers.push(augmentServer);
        }
    }
    return servers;
}
/**
 * Merge MCP servers into VSCode settings using the specified strategy
 */
function mergeAugmentMcpServers(existingSettings, newServers, strategy) {
    const result = structuredClone(existingSettings);
    if (!result['augment.advanced']) {
        result['augment.advanced'] = {};
    }
    if (strategy === 'overwrite') {
        result['augment.advanced'].mcpServers = newServers;
    }
    else {
        const existingServers = result['augment.advanced'].mcpServers || [];
        const existingServerMap = new Map();
        for (const server of existingServers) {
            existingServerMap.set(server.name, server);
        }
        for (const newServer of newServers) {
            existingServerMap.set(newServer.name, newServer);
        }
        result['augment.advanced'].mcpServers = Array.from(existingServerMap.values());
    }
    return result;
}
/**
 * Get the VSCode settings.json path for a project (local)
 */
function getVSCodeSettingsPath(projectRoot) {
    return path.join(projectRoot, '.vscode', 'settings.json');
}
