"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.OpenCodeAgent = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
class OpenCodeAgent {
    getIdentifier() {
        return 'opencode';
    }
    getName() {
        return 'OpenCode';
    }
    getDefaultOutputPath(projectRoot) {
        return {
            instructions: path.join(projectRoot, 'AGENTS.md'),
            mcp: path.join(projectRoot, 'opencode.json'),
        };
    }
    async applyRulerConfig(concatenatedRules, projectRoot, rulerMcpJson, agentConfig) {
        const outputPaths = this.getDefaultOutputPath(projectRoot);
        const instructionsPath = path.resolve(projectRoot, agentConfig?.outputPathInstructions ?? outputPaths['instructions']);
        const mcpPath = path.resolve(projectRoot, agentConfig?.outputPathConfig ?? outputPaths['mcp']);
        await fs.writeFile(instructionsPath, concatenatedRules);
        // Create OpenCode config with schema and MCP configuration
        let finalMcpConfig = {
            $schema: 'https://opencode.ai/config.json',
            mcp: {},
        };
        try {
            const existingMcpConfig = JSON.parse(await fs.readFile(mcpPath, 'utf-8'));
            if (existingMcpConfig && typeof existingMcpConfig === 'object') {
                finalMcpConfig = {
                    $schema: 'https://opencode.ai/config.json',
                    ...existingMcpConfig,
                    mcp: {
                        ...(existingMcpConfig.mcp || {}),
                        ...(rulerMcpJson?.mcpServers ?? {}),
                    },
                };
            }
            else if (rulerMcpJson) {
                finalMcpConfig = {
                    $schema: 'https://opencode.ai/config.json',
                    mcp: (rulerMcpJson?.mcpServers ?? {}),
                };
            }
        }
        catch {
            if (rulerMcpJson) {
                finalMcpConfig = {
                    $schema: 'https://opencode.ai/config.json',
                    mcp: (rulerMcpJson?.mcpServers ?? {}),
                };
            }
        }
        // Always write the config file, even if MCP is empty
        await fs.writeFile(mcpPath, JSON.stringify(finalMcpConfig, null, 2));
    }
    supportsMcpStdio() {
        return true;
    }
    supportsMcpRemote() {
        return true;
    }
    supportsMcpTimeout() {
        return true;
    }
    supportsNativeSkills() {
        return true;
    }
}
exports.OpenCodeAgent = OpenCodeAgent;
