"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefaultServerConfig = exports.getNativeHTTPServerConfig = exports.buildMCPUrl = exports.ALL_FEATURE_VALUES = exports.AVAILABLE_FEATURES = exports.DefaultMCPClientConfig = void 0;
const zod_1 = __importDefault(require("zod"));
const urls_1 = require("../../utils/urls");
exports.DefaultMCPClientConfig = zod_1.default
    .object({
    mcpServers: zod_1.default.record(zod_1.default.string(), zod_1.default.union([
        zod_1.default.object({
            command: zod_1.default.string().optional(),
            args: zod_1.default.array(zod_1.default.string()).optional(),
            env: zod_1.default.record(zod_1.default.string(), zod_1.default.string()).optional(),
        }),
        zod_1.default.object({
            url: zod_1.default.string(),
            headers: zod_1.default.record(zod_1.default.string(), zod_1.default.string()).optional(),
        }),
    ])),
})
    .passthrough();
exports.AVAILABLE_FEATURES = {
    'Data & Analytics': [
        {
            value: 'dashboards',
            label: 'Dashboards',
            hint: 'Dashboard creation and management',
        },
        {
            value: 'insights',
            label: 'Insights',
            hint: 'Analytics insights and SQL queries',
        },
        {
            value: 'experiments',
            label: 'Experiments',
            hint: 'A/B testing experiments',
        },
        {
            value: 'llm-analytics',
            label: 'LLM Analytics',
            hint: 'LLM usage and cost tracking',
        },
    ],
    'Development Tools': [
        {
            value: 'error-tracking',
            label: 'Error Tracking',
            hint: 'Error monitoring and debugging',
        },
        { value: 'flags', label: 'Feature Flags', hint: 'Feature flag management' },
    ],
    'Platform & Management': [
        {
            value: 'workspace',
            label: 'Workspace',
            hint: 'Organization and project management',
        },
        {
            value: 'docs',
            label: 'Documentation',
            hint: 'Amplitude documentation search',
        },
    ],
};
exports.ALL_FEATURE_VALUES = Object.values(exports.AVAILABLE_FEATURES)
    .flat()
    .map((feature) => feature.value);
const buildMCPUrl = (type, selectedFeatures, local, 
// Default 'us' keeps the call sites (every editor's `addServer`) and
// their tests stable for non-EU runs. EU users should ALWAYS pass
// their resolved zone — this URL gets baked into the user's editor
// config and persists past the wizard run.
zone = 'us') => {
    const host = local ? 'http://localhost:8787' : (0, urls_1.getMcpHostFromRegion)(zone);
    const baseUrl = `${host}/${type === 'sse' ? 'sse' : 'mcp'}`;
    const isAllFeaturesSelected = selectedFeatures &&
        selectedFeatures.length === exports.ALL_FEATURE_VALUES.length &&
        exports.ALL_FEATURE_VALUES.every((feature) => selectedFeatures.includes(feature));
    const params = [];
    // Add features param if not all features selected
    if (selectedFeatures &&
        selectedFeatures.length > 0 &&
        !isAllFeaturesSelected) {
        params.push(`features=${selectedFeatures.join(',')}`);
    }
    return params.length > 0 ? `${baseUrl}?${params.join('&')}` : baseUrl;
};
exports.buildMCPUrl = buildMCPUrl;
const getNativeHTTPServerConfig = (apiKey, type, selectedFeatures, local, zone = 'us') => {
    const config = {
        url: (0, exports.buildMCPUrl)(type, selectedFeatures, local, zone),
    };
    // Only add auth header if API key is provided (not OAuth mode)
    if (apiKey) {
        config.headers = {
            Authorization: `Bearer ${apiKey}`,
        };
    }
    return config;
};
exports.getNativeHTTPServerConfig = getNativeHTTPServerConfig;
const getDefaultServerConfig = (apiKey, type, selectedFeatures, local, zone = 'us') => {
    const urlWithFeatures = (0, exports.buildMCPUrl)(type, selectedFeatures, local, zone);
    // OAuth mode: no auth header, let MCP handle OAuth
    if (!apiKey) {
        return {
            command: 'npx',
            args: ['-y', 'mcp-remote@latest', urlWithFeatures],
        };
    }
    // API key mode: include auth header
    return {
        command: 'npx',
        args: [
            '-y',
            'mcp-remote@latest',
            urlWithFeatures,
            '--header',
            `Authorization:\${AMPLITUDE_AUTH_HEADER}`,
        ],
        env: {
            AMPLITUDE_AUTH_HEADER: `Bearer ${apiKey}`,
        },
    };
};
exports.getDefaultServerConfig = getDefaultServerConfig;
