/**
 * JetBrains Plugin Scaffold
 *
 * Bridge between Code Buddy and JetBrains IDEs (IntelliJ IDEA, PyCharm,
 * WebStorm, GoLand, etc). Provides diff viewing, selection sharing,
 * diagnostic sharing, and quick launch integration.
 */
import { logger } from '../utils/logger.js';
const DEFAULT_CONFIG = {
    enableDiffViewer: true,
    enableSelectionSharing: true,
    enableDiagnosticSharing: true,
    quickLaunchShortcut: 'Ctrl+Shift+B',
    supportedIDEs: [
        'IntelliJ IDEA',
        'PyCharm',
        'WebStorm',
        'GoLand',
        'PhpStorm',
        'Rider',
        'CLion',
        'RubyMine',
        'DataGrip',
        'Android Studio',
    ],
};
export class JetBrainsBridge {
    config;
    diffs;
    sharedSelections;
    diagnostics;
    constructor(config) {
        this.config = { ...DEFAULT_CONFIG, ...config };
        this.diffs = new Map();
        this.sharedSelections = [];
        this.diagnostics = [];
        logger.debug('JetBrainsBridge initialized', { shortcut: this.config.quickLaunchShortcut });
    }
    createDiff(file, before, after) {
        if (!this.config.enableDiffViewer) {
            throw new Error('Diff viewer is disabled');
        }
        let changeType;
        if (before.length === 0 && after.length > 0) {
            changeType = 'created';
        }
        else if (before.length > 0 && after.length === 0) {
            changeType = 'deleted';
        }
        else {
            changeType = 'modified';
        }
        const diff = { file, before, after, changeType };
        this.diffs.set(file, diff);
        logger.info('Diff created', { file, changeType });
        return { ...diff };
    }
    getDiffs() {
        return Array.from(this.diffs.values()).map(d => ({ ...d }));
    }
    acceptDiff(file) {
        if (!this.diffs.has(file)) {
            return false;
        }
        this.diffs.delete(file);
        logger.info('Diff accepted', { file });
        return true;
    }
    rejectDiff(file) {
        if (!this.diffs.has(file)) {
            return false;
        }
        this.diffs.delete(file);
        logger.info('Diff rejected', { file });
        return true;
    }
    clearDiffs() {
        this.diffs.clear();
        logger.info('All diffs cleared');
    }
    shareSelection(file, text) {
        if (!this.config.enableSelectionSharing) {
            return;
        }
        this.sharedSelections.push({
            file,
            text,
            timestamp: Date.now(),
        });
        logger.debug('Selection shared', { file, length: text.length });
    }
    getSharedSelections() {
        return [...this.sharedSelections];
    }
    shareDiagnostic(file, line, message, type) {
        if (!this.config.enableDiagnosticSharing) {
            return;
        }
        this.diagnostics.push({ file, line, message, type });
        logger.debug('Diagnostic shared', { file, line, type });
    }
    getDiagnostics() {
        return [...this.diagnostics];
    }
    clearDiagnostics() {
        this.diagnostics = [];
        logger.info('Diagnostics cleared');
    }
    getQuickLaunchShortcut() {
        return this.config.quickLaunchShortcut;
    }
    setQuickLaunchShortcut(shortcut) {
        if (!shortcut || shortcut.trim().length === 0) {
            throw new Error('Shortcut cannot be empty');
        }
        this.config.quickLaunchShortcut = shortcut.trim();
        logger.info('Quick launch shortcut updated', { shortcut: this.config.quickLaunchShortcut });
    }
    getSupportedIDEs() {
        return [...this.config.supportedIDEs];
    }
    isIDESupported(ide) {
        return this.config.supportedIDEs.some(supported => supported.toLowerCase() === ide.toLowerCase());
    }
    generatePluginXml() {
        return `<idea-plugin>
  <id>com.codebuddy.jetbrains</id>
  <name>Code Buddy</name>
  <version>0.1.0</version>
  <vendor email="support@codebuddy.dev" url="https://codebuddy.dev">Code Buddy</vendor>
  <description><![CDATA[
    AI coding agent for JetBrains IDEs. Provides inline diffs, selection sharing,
    diagnostic context, and quick launch integration.
  ]]></description>
  <depends>com.intellij.modules.platform</depends>
  <extensions defaultExtensionNs="com.intellij">
    <toolWindow id="Code Buddy" anchor="right" factoryClass="com.codebuddy.ToolWindowFactory"/>
    <notificationGroup id="Code Buddy" displayType="BALLOON"/>
  </extensions>
  <actions>
    <action id="CodeBuddy.QuickLaunch" class="com.codebuddy.QuickLaunchAction"
            text="Code Buddy: Quick Launch" description="Open Code Buddy panel">
      <keyboard-shortcut keymap="\\$default" first-keystroke="${this.config.quickLaunchShortcut}"/>
    </action>
    <action id="CodeBuddy.ShareSelection" class="com.codebuddy.ShareSelectionAction"
            text="Share Selection with Code Buddy" description="Share selected code with Code Buddy">
      <add-to-group group-id="EditorPopupMenu" anchor="last"/>
    </action>
  </actions>
</idea-plugin>`;
    }
    getDiffCount() {
        return this.diffs.size;
    }
}
//# sourceMappingURL=jetbrains-plugin.js.map