"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.DefaultMCPClient = exports.MCPClient = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const jsonc = __importStar(require("jsonc-parser"));
const zod_1 = require("zod");
const defaults_1 = require("./defaults");
const MCPConfigSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()));
class MCPClient {
    name;
}
exports.MCPClient = MCPClient;
class DefaultMCPClient extends MCPClient {
    name = 'Default';
    constructor() {
        super();
    }
    getServerPropertyName() {
        return 'mcpServers';
    }
    getServerConfig(apiKey, type, selectedFeatures, local, zone = 'us') {
        return (0, defaults_1.getDefaultServerConfig)(apiKey, type, selectedFeatures, local, zone);
    }
    async isServerInstalled(local) {
        try {
            const configPath = await this.getConfigPath();
            if (!fs.existsSync(configPath)) {
                return false;
            }
            const configContent = await fs.promises.readFile(configPath, 'utf8');
            const parsed = MCPConfigSchema.safeParse(jsonc.parse(configContent));
            const config = parsed.success ? parsed.data : {};
            const serverPropertyName = this.getServerPropertyName();
            const serverName = local ? 'amplitude-local' : 'amplitude';
            return (serverPropertyName in config && serverName in config[serverPropertyName]);
        }
        catch {
            return false;
        }
    }
    async addServer(apiKey, selectedFeatures, local, zone = 'us') {
        return this._addServerType(apiKey, 'sse', selectedFeatures, local, zone);
    }
    async _addServerType(apiKey, type, selectedFeatures, local, zone = 'us') {
        try {
            const configPath = await this.getConfigPath();
            const configDir = path.dirname(configPath);
            await fs.promises.mkdir(configDir, { recursive: true });
            const serverPropertyName = this.getServerPropertyName();
            let configContent = '';
            let existingConfig = {};
            if (fs.existsSync(configPath)) {
                configContent = await fs.promises.readFile(configPath, 'utf8');
                const parsed = MCPConfigSchema.safeParse(jsonc.parse(configContent));
                existingConfig = parsed.success ? parsed.data : {};
            }
            const newServerConfig = this.getServerConfig(apiKey, type, selectedFeatures, local, zone);
            if (!existingConfig[serverPropertyName]) {
                existingConfig[serverPropertyName] = {};
            }
            const serverName = local ? 'amplitude-local' : 'amplitude';
            existingConfig[serverPropertyName][serverName] = newServerConfig;
            const edits = jsonc.modify(configContent, [serverPropertyName, serverName], newServerConfig, {
                formattingOptions: {
                    tabSize: 2,
                    insertSpaces: true,
                },
            });
            const modifiedContent = jsonc.applyEdits(configContent, edits);
            await fs.promises.writeFile(configPath, modifiedContent, 'utf8');
            return { success: true };
        }
        catch {
            return { success: false };
        }
    }
    async removeServer(local) {
        try {
            const configPath = await this.getConfigPath();
            if (!fs.existsSync(configPath)) {
                return { success: false };
            }
            const configContent = await fs.promises.readFile(configPath, 'utf8');
            const parsed = MCPConfigSchema.safeParse(jsonc.parse(configContent));
            const config = parsed.success ? parsed.data : {};
            const serverPropertyName = this.getServerPropertyName();
            const serverName = local ? 'amplitude-local' : 'amplitude';
            if (serverPropertyName in config &&
                serverName in config[serverPropertyName]) {
                const edits = jsonc.modify(configContent, [serverPropertyName, serverName], undefined, {
                    formattingOptions: {
                        tabSize: 2,
                        insertSpaces: true,
                    },
                });
                const modifiedContent = jsonc.applyEdits(configContent, edits);
                await fs.promises.writeFile(configPath, modifiedContent, 'utf8');
                return { success: true };
            }
        }
        catch {
            //
        }
        return { success: false };
    }
}
exports.DefaultMCPClient = DefaultMCPClient;
