/**
 * @deprecated This file is a compatibility shim retained for one minor
 * version per AC 1 of P3-IDE-01. Import from '@connexum/ai-governance/ide'
 * (CursorIdeAdapter) for the full IdeAdapter contract. This shim will be
 * deleted in the next minor release.
 *
 * ---
 *
 * Cursor IDE Adapter for GovernanceAdapter interface.
 *
 * Maps Cursor's composer/agent action format to the governance
 * framework's unified ActionContext. Cursor has limited hook support
 * compared to Claude Code: no post-action hooks, no tamper detection,
 * no approval queue integration.
 *
 * Supported Cursor tool types:
 *   - edit_file    -> mapped to 'Edit'
 *   - run_terminal -> mapped to 'Bash'
 *   - search       -> mapped to 'Grep'
 *   - read_file    -> mapped to 'Read'
 *
 * @connexum/ai-governance
 */
import { BaseGovernanceAdapter, deny, allow, } from './index.js';
// ---------------------------------------------------------------------------
// Tool type mapping: Cursor -> standard governance tool names
// ---------------------------------------------------------------------------
const CURSOR_TOOL_MAP = {
    edit_file: 'Edit',
    run_terminal: 'Bash',
    search: 'Grep',
    read_file: 'Read',
};
// ---------------------------------------------------------------------------
// Default destructive command patterns
// ---------------------------------------------------------------------------
const DEFAULT_DESTRUCTIVE_PATTERNS = [
    /\brm\s+(-[rR]f?|--recursive)\b/,
    /\bgit\s+push\s+--force\b/,
    /\bgit\s+reset\s+--hard\b/,
    /\bgit\s+clean\s+-f/,
    /\bdrop\s+(table|database)\b/i,
    /\btruncate\s+table\b/i,
    /\bnpm\s+publish\b/,
    /\bcurl\b.*\|\s*(bash|sh)\b/,
];
// ---------------------------------------------------------------------------
// CursorAdapter
// ---------------------------------------------------------------------------
export class CursorAdapter extends BaseGovernanceAdapter {
    blockedPatterns = [];
    auditLog = [];
    initialize(config) {
        super.initialize(config);
        // Merge default destructive patterns with any custom ones from config
        const customPatterns = (config.platformOptions?.blockedCommands ?? []);
        this.blockedPatterns = [
            ...DEFAULT_DESTRUCTIVE_PATTERNS,
            ...customPatterns,
        ];
        this.auditLog = [];
    }
    preAction(context) {
        this.ensureInitialized();
        // Check terminal commands against blocked patterns
        if (context.toolName === 'Bash') {
            const command = String(context.input.command ?? '');
            for (const pattern of this.blockedPatterns) {
                if (pattern.test(command)) {
                    const decision = deny(`Blocked destructive command matching pattern: ${pattern.source}`);
                    this.recordAudit('run_terminal', context, decision);
                    return decision;
                }
            }
        }
        const decision = allow('Action permitted by Cursor adapter');
        const cursorType = this.reverseLookupCursorType(context.toolName);
        if (cursorType) {
            this.recordAudit(cursorType, context, decision);
        }
        return decision;
    }
    /**
     * Post-action is a no-op for Cursor. Cursor IDE does not provide
     * post-action hooks, so this method exists only to satisfy the
     * GovernanceAdapter interface contract.
     */
    postAction(_context, _result) {
        // No-op: Cursor has no post-action hook support
    }
    getCapabilities() {
        return {
            preAction: true,
            postAction: false,
            auditLogging: true,
            circuitBreaker: true,
            approvalQueue: false,
            tamperDetection: false,
        };
    }
    // ---------------------------------------------------------------------------
    // Cursor-specific public methods
    // ---------------------------------------------------------------------------
    /**
     * Wraps a Cursor composer action with governance enforcement.
     * Converts the Cursor-native action format to an ActionContext,
     * runs preAction, and returns the decision with the mapped context.
     */
    wrapComposerAction(action) {
        const context = this.mapToActionContext(action);
        const decision = this.preAction(context);
        return { context, decision };
    }
    /**
     * Maps a Cursor composer action to the standard ActionContext format.
     */
    mapToActionContext(action) {
        const toolName = CURSOR_TOOL_MAP[action.type] ?? action.type;
        const input = {};
        switch (action.type) {
            case 'edit_file':
                if (action.filePath)
                    input.file_path = action.filePath;
                if (action.content)
                    input.new_string = action.content;
                if (action.originalContent)
                    input.old_string = action.originalContent;
                break;
            case 'run_terminal':
                if (action.command)
                    input.command = action.command;
                break;
            case 'search':
                if (action.query)
                    input.pattern = action.query;
                break;
            case 'read_file':
                if (action.filePath)
                    input.file_path = action.filePath;
                break;
        }
        if (action.metadata) {
            input._cursorMetadata = action.metadata;
        }
        const agent = {
            name: action.agentName ?? 'cursor-composer',
            role: 'cursor-agent',
            sessionId: action.composerSessionId,
        };
        return {
            toolName,
            input,
            agent,
            timestamp: new Date().toISOString(),
        };
    }
    /**
     * Returns the internal audit log for diagnostics and testing.
     */
    getAuditLog() {
        return this.auditLog;
    }
    // ---------------------------------------------------------------------------
    // Private helpers
    // ---------------------------------------------------------------------------
    recordAudit(cursorType, context, decision) {
        this.auditLog.push({
            timestamp: context.timestamp,
            cursorAction: cursorType,
            mappedToolName: context.toolName,
            decision,
            composerSessionId: context.agent.sessionId,
        });
    }
    reverseLookupCursorType(toolName) {
        for (const [cursorType, mappedName] of Object.entries(CURSOR_TOOL_MAP)) {
            if (mappedName === toolName) {
                return cursorType;
            }
        }
        return null;
    }
}
//# sourceMappingURL=cursor.js.map