"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.AgentsMdAgent = void 0;
const path = __importStar(require("path"));
const fs_1 = require("fs");
const AbstractAgent_1 = require("./AbstractAgent");
const FileSystemUtils_1 = require("../core/FileSystemUtils");
/**
 * Pseudo-agent that ensures the concatenated rules are written to root-level `AGENTS.md`.
 * Does not participate in MCP propagation. Idempotent: only writes (and creates a backup)
 * when content differs from existing file.
 */
class AgentsMdAgent extends AbstractAgent_1.AbstractAgent {
    getIdentifier() {
        return 'agentsmd';
    }
    getName() {
        return 'AgentsMd';
    }
    getDefaultOutputPath(projectRoot) {
        return path.join(projectRoot, 'AGENTS.md');
    }
    async applyRulerConfig(concatenatedRules, projectRoot, _rulerMcpJson, agentConfig, backup = true) {
        const output = agentConfig?.outputPath ?? this.getDefaultOutputPath(projectRoot);
        const absolutePath = path.resolve(projectRoot, output);
        await (0, FileSystemUtils_1.ensureDirExists)(path.dirname(absolutePath));
        // Add marker comment to the content to identify it as generated
        const contentWithMarker = `<!-- Generated by Ruler -->\n${concatenatedRules}`;
        // Read existing content if present and skip write if identical
        let existing = null;
        try {
            existing = await fs_1.promises.readFile(absolutePath, 'utf8');
        }
        catch {
            existing = null;
        }
        if (existing !== null && existing === contentWithMarker) {
            // No change; skip backup/write for idempotency
            return;
        }
        // Backup (only if file existed and backup is enabled) then write new content
        if (backup) {
            await (0, FileSystemUtils_1.backupFile)(absolutePath);
        }
        await (0, FileSystemUtils_1.writeGeneratedFile)(absolutePath, contentWithMarker);
    }
    getMcpServerKey() {
        // No MCP configuration for this pseudo-agent
        return '';
    }
}
exports.AgentsMdAgent = AgentsMdAgent;
