"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.CrushAgent = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
class CrushAgent {
    getIdentifier() {
        return 'crush';
    }
    getName() {
        return 'Crush';
    }
    getDefaultOutputPath(projectRoot) {
        return {
            instructions: path.join(projectRoot, 'CRUSH.md'),
            mcp: path.join(projectRoot, '.crush.json'),
        };
    }
    /**
     * Transform MCP server types for Crush compatibility.
     * Crush expects "http" for HTTP servers and "sse" for SSE servers, not "remote".
     */
    transformMcpServersForCrush(mcpServers) {
        const transformedServers = {};
        for (const [name, serverDef] of Object.entries(mcpServers)) {
            if (serverDef && typeof serverDef === 'object') {
                const server = serverDef;
                const transformedServer = { ...server };
                // Transform type: "remote" to appropriate Crush types
                if (server.type === 'remote' &&
                    server.url &&
                    typeof server.url === 'string') {
                    const url = server.url;
                    // Check if URL suggests SSE (contains /sse path segment)
                    if (/\/sse(\/|$)/i.test(url)) {
                        transformedServer.type = 'sse';
                    }
                    else {
                        transformedServer.type = 'http';
                    }
                }
                transformedServers[name] = transformedServer;
            }
            else {
                transformedServers[name] = serverDef;
            }
        }
        return transformedServers;
    }
    async applyRulerConfig(concatenatedRules, projectRoot, rulerMcpJson, agentConfig) {
        const outputPaths = this.getDefaultOutputPath(projectRoot);
        const instructionsPath = agentConfig?.outputPathInstructions ?? outputPaths['instructions'];
        const mcpPath = agentConfig?.outputPathConfig ?? outputPaths['mcp'];
        await fs.writeFile(instructionsPath, concatenatedRules);
        // Always transform from mcpServers ({ mcpServers: ... }) to { mcp: ... } for Crush
        let finalMcpConfig = { mcp: {} };
        try {
            const existingMcpConfig = JSON.parse(await fs.readFile(mcpPath, 'utf-8'));
            if (existingMcpConfig && typeof existingMcpConfig === 'object') {
                const transformedServers = this.transformMcpServersForCrush((rulerMcpJson?.mcpServers ?? {}));
                finalMcpConfig = {
                    ...existingMcpConfig,
                    mcp: {
                        ...(existingMcpConfig.mcp || {}),
                        ...transformedServers,
                    },
                };
            }
            else if (rulerMcpJson) {
                const transformedServers = this.transformMcpServersForCrush((rulerMcpJson?.mcpServers ?? {}));
                finalMcpConfig = {
                    mcp: transformedServers,
                };
            }
        }
        catch {
            if (rulerMcpJson) {
                const transformedServers = this.transformMcpServersForCrush((rulerMcpJson?.mcpServers ?? {}));
                finalMcpConfig = {
                    mcp: transformedServers,
                };
            }
        }
        if (Object.keys(finalMcpConfig.mcp).length > 0) {
            await fs.writeFile(mcpPath, JSON.stringify(finalMcpConfig, null, 2));
        }
    }
    supportsMcpStdio() {
        return true;
    }
    supportsMcpRemote() {
        return true;
    }
}
exports.CrushAgent = CrushAgent;
