"use strict";
/**
 * IDE Integration Module
 * Exports formatters and utilities for IDE-specific config files
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.IDE_FILE_PATHS = exports.OCULUM_SECTION_END = exports.OCULUM_SECTION_START = exports.formatClaudeCodeSection = exports.formatWindsurfRules = exports.formatCursorRules = void 0;
exports.detectIDEConfigs = detectIDEConfigs;
exports.writeIDEFile = writeIDEFile;
exports.updateClaudeMdSection = updateClaudeMdSection;
exports.clearIDEFiles = clearIDEFiles;
exports.getIDEFilePath = getIDEFilePath;
const fs_1 = require("fs");
const path_1 = require("path");
// Export formatters
var cursor_1 = require("./cursor");
Object.defineProperty(exports, "formatCursorRules", { enumerable: true, get: function () { return cursor_1.formatCursorRules; } });
var windsurf_1 = require("./windsurf");
Object.defineProperty(exports, "formatWindsurfRules", { enumerable: true, get: function () { return windsurf_1.formatWindsurfRules; } });
var claude_code_1 = require("./claude-code");
Object.defineProperty(exports, "formatClaudeCodeSection", { enumerable: true, get: function () { return claude_code_1.formatClaudeCodeSection; } });
Object.defineProperty(exports, "OCULUM_SECTION_START", { enumerable: true, get: function () { return claude_code_1.OCULUM_SECTION_START; } });
Object.defineProperty(exports, "OCULUM_SECTION_END", { enumerable: true, get: function () { return claude_code_1.OCULUM_SECTION_END; } });
/** Default file paths for each IDE */
exports.IDE_FILE_PATHS = {
    cursor: '.cursor/rules/security.mdc',
    windsurf: '.windsurfrules',
    'claude-code': 'CLAUDE.md',
};
/**
 * Detect which IDE configurations exist in the project
 *
 * @param projectPath - Path to the project root
 * @returns Array of detected IDE types
 */
function detectIDEConfigs(projectPath) {
    const detected = [];
    // Check for Cursor
    if ((0, fs_1.existsSync)((0, path_1.join)(projectPath, '.cursor'))) {
        detected.push('cursor');
    }
    // Check for Windsurf rules
    if ((0, fs_1.existsSync)((0, path_1.join)(projectPath, '.windsurfrules'))) {
        detected.push('windsurf');
    }
    // Check for Claude Code
    if ((0, fs_1.existsSync)((0, path_1.join)(projectPath, 'CLAUDE.md'))) {
        detected.push('claude-code');
    }
    return detected;
}
/**
 * Write an IDE-specific file
 *
 * @param projectPath - Path to the project root
 * @param relativePath - Relative path for the file
 * @param content - Content to write
 * @returns Result object
 */
function writeIDEFile(projectPath, relativePath, content) {
    const fullPath = (0, path_1.join)(projectPath, relativePath);
    try {
        // Ensure directory exists
        const dir = (0, path_1.dirname)(fullPath);
        if (!(0, fs_1.existsSync)(dir)) {
            (0, fs_1.mkdirSync)(dir, { recursive: true });
        }
        // Write file
        (0, fs_1.writeFileSync)(fullPath, content);
        return { success: true, path: fullPath };
    }
    catch (err) {
        return {
            success: false,
            path: fullPath,
            error: `Failed to write ${relativePath}: ${err instanceof Error ? err.message : 'Unknown error'}`,
        };
    }
}
/**
 * Update the Oculum section in CLAUDE.md
 * Replaces content between OCULUM_SECURITY_START and OCULUM_SECURITY_END markers
 * If markers don't exist, appends the section at the end
 *
 * @param projectPath - Path to the project root
 * @param section - The section content (including markers)
 * @returns Result object
 */
function updateClaudeMdSection(projectPath, section) {
    const filePath = (0, path_1.join)(projectPath, 'CLAUDE.md');
    try {
        let content;
        if ((0, fs_1.existsSync)(filePath)) {
            content = (0, fs_1.readFileSync)(filePath, 'utf-8');
            // Check if markers exist
            const startMarker = '<!-- OCULUM_SECURITY_START -->';
            const endMarker = '<!-- OCULUM_SECURITY_END -->';
            const startIndex = content.indexOf(startMarker);
            const endIndex = content.indexOf(endMarker);
            if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) {
                // Replace existing section
                content =
                    content.substring(0, startIndex) +
                        section +
                        content.substring(endIndex + endMarker.length);
            }
            else {
                // Append section at end
                content = content.trimEnd() + '\n\n' + section;
            }
        }
        else {
            // Create new file with section
            content = `# CLAUDE.md\n\n${section}`;
        }
        (0, fs_1.writeFileSync)(filePath, content);
        return { success: true, path: filePath };
    }
    catch (err) {
        return {
            success: false,
            path: filePath,
            error: `Failed to update CLAUDE.md: ${err instanceof Error ? err.message : 'Unknown error'}`,
        };
    }
}
/**
 * Clear IDE rule files
 *
 * @param projectPath - Path to the project root
 * @param types - Optional array of IDE types to clear. If not provided, clears all.
 * @returns Result object with list of cleared files
 */
function clearIDEFiles(projectPath, types) {
    const toCheck = types || ['cursor', 'windsurf', 'claude-code'];
    const clearedFiles = [];
    const errors = [];
    for (const ide of toCheck) {
        const relativePath = exports.IDE_FILE_PATHS[ide];
        const fullPath = (0, path_1.join)(projectPath, relativePath);
        try {
            if (ide === 'claude-code') {
                // For CLAUDE.md, remove only the Oculum section, not the whole file
                if ((0, fs_1.existsSync)(fullPath)) {
                    const content = (0, fs_1.readFileSync)(fullPath, 'utf-8');
                    const startMarker = '<!-- OCULUM_SECURITY_START -->';
                    const endMarker = '<!-- OCULUM_SECURITY_END -->';
                    const startIndex = content.indexOf(startMarker);
                    const endIndex = content.indexOf(endMarker);
                    if (startIndex !== -1 && endIndex !== -1) {
                        // Remove section (including potential surrounding newlines)
                        let newContent = content.substring(0, startIndex) + content.substring(endIndex + endMarker.length);
                        // Clean up extra newlines
                        newContent = newContent.replace(/\n{3,}/g, '\n\n').trimEnd() + '\n';
                        (0, fs_1.writeFileSync)(fullPath, newContent);
                        clearedFiles.push(relativePath);
                    }
                }
            }
            else {
                // For other IDEs, delete the file
                if ((0, fs_1.existsSync)(fullPath)) {
                    (0, fs_1.unlinkSync)(fullPath);
                    clearedFiles.push(relativePath);
                }
            }
        }
        catch (err) {
            errors.push(`Failed to clear ${relativePath}: ${err instanceof Error ? err.message : 'Unknown error'}`);
        }
    }
    return {
        success: errors.length === 0,
        clearedFiles,
        errors,
    };
}
/**
 * Get the default file path for an IDE type
 */
function getIDEFilePath(ide) {
    return exports.IDE_FILE_PATHS[ide];
}
//# sourceMappingURL=index.js.map