"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.MistralVibeAgent = void 0;
const path = __importStar(require("path"));
const fs_1 = require("fs");
const toml_1 = require("@iarna/toml");
const AgentsMdAgent_1 = require("./AgentsMdAgent");
const FileSystemUtils_1 = require("../core/FileSystemUtils");
const constants_1 = require("../constants");
/**
 * Mistral Vibe CLI agent adapter.
 * Propagates rules to AGENTS.md and MCP servers to .vibe/config.toml.
 */
class MistralVibeAgent {
    constructor() {
        this.agentsMdAgent = new AgentsMdAgent_1.AgentsMdAgent();
    }
    getIdentifier() {
        return 'mistral';
    }
    getName() {
        return 'Mistral';
    }
    async applyRulerConfig(concatenatedRules, projectRoot, rulerMcpJson, agentConfig, backup = true) {
        // First perform idempotent AGENTS.md write via composed AgentsMdAgent
        await this.agentsMdAgent.applyRulerConfig(concatenatedRules, projectRoot, null, {
            outputPath: agentConfig?.outputPath ||
                agentConfig?.outputPathInstructions ||
                undefined,
        }, backup);
        // Handle MCP configuration
        const defaults = this.getDefaultOutputPath(projectRoot);
        const mcpEnabled = agentConfig?.mcp?.enabled ?? true;
        if (mcpEnabled && rulerMcpJson) {
            // Apply MCP server filtering and transformation
            const { filterMcpConfigForAgent } = await Promise.resolve().then(() => __importStar(require('../mcp/capabilities')));
            const filteredMcpConfig = filterMcpConfigForAgent(rulerMcpJson, this);
            if (!filteredMcpConfig) {
                return; // No compatible servers found
            }
            const filteredRulerMcpJson = filteredMcpConfig;
            // Determine the config file path
            const configPath = agentConfig?.outputPathConfig ?? defaults.config;
            // Ensure the parent directory exists
            await fs_1.promises.mkdir(path.dirname(configPath), { recursive: true });
            // Get the merge strategy
            const strategy = agentConfig?.mcp?.strategy ?? 'merge';
            // Transform ruler MCP servers to Vibe format
            const rulerServers = filteredRulerMcpJson.mcpServers || {};
            const vibeServers = [];
            for (const [serverName, serverConfig] of Object.entries(rulerServers)) {
                const vibeServer = {
                    name: serverName,
                    transport: this.determineTransport(serverConfig),
                };
                // Handle stdio servers
                if (serverConfig.command) {
                    vibeServer.command = serverConfig.command;
                    if (serverConfig.args) {
                        vibeServer.args = serverConfig.args;
                    }
                }
                // Handle remote servers
                if (serverConfig.url) {
                    vibeServer.url = serverConfig.url;
                }
                // Handle headers
                if (serverConfig.headers) {
                    vibeServer.headers = serverConfig.headers;
                }
                // Handle env
                if (serverConfig.env) {
                    vibeServer.env = serverConfig.env;
                }
                vibeServers.push(vibeServer);
            }
            // Read existing TOML config if it exists
            let existingConfig = {};
            try {
                const existingContent = await fs_1.promises.readFile(configPath, 'utf8');
                existingConfig = (0, toml_1.parse)(existingContent);
            }
            catch {
                // File doesn't exist or can't be parsed, use empty config
            }
            // Create the updated config
            const updatedConfig = { ...existingConfig };
            if (strategy === 'overwrite') {
                // For overwrite strategy, replace the entire mcp_servers array
                updatedConfig.mcp_servers = vibeServers;
            }
            else {
                // For merge strategy, merge by server name
                const existingServers = updatedConfig.mcp_servers || [];
                // Keep existing servers that aren't being overwritten by ruler
                const mergedServers = existingServers.filter((s) => !rulerServers[s.name]);
                // Add all ruler servers
                mergedServers.push(...vibeServers);
                updatedConfig.mcp_servers = mergedServers;
            }
            // Convert to TOML and write
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            const tomlContent = (0, toml_1.stringify)(updatedConfig);
            await (0, FileSystemUtils_1.writeGeneratedFile)(configPath, tomlContent);
        }
    }
    /**
     * Determines the transport type based on server configuration.
     */
    determineTransport(server) {
        if (server.command) {
            return 'stdio';
        }
        if (server.url) {
            // Default to http for remote servers
            // Could potentially detect streamable-http based on URL patterns if needed
            return 'http';
        }
        return 'stdio';
    }
    getDefaultOutputPath(projectRoot) {
        return {
            instructions: path.join(projectRoot, constants_1.DEFAULT_RULES_FILENAME),
            config: path.join(projectRoot, '.vibe', 'config.toml'),
        };
    }
    supportsMcpStdio() {
        return true;
    }
    supportsMcpRemote() {
        return true; // Mistral Vibe supports http and streamable-http transports
    }
    supportsNativeSkills() {
        // Mistral Vibe supports native skills in .vibe/skills/
        return true;
    }
}
exports.MistralVibeAgent = MistralVibeAgent;
