import { BaseBlockHandler, type IBlockEventHandlers } from '../block.base.js'; import type { IBlock } from '../../wysiwyg.types.js'; import { cssManager } from '@design.estate/dees-element'; /** * MarkdownBlockHandler - Handles markdown content with preview/edit toggle * * Features: * - Live markdown preview * - Edit/preview mode toggle * - Syntax highlighting in edit mode * - Common markdown shortcuts * - Auto-save on mode switch */ export class MarkdownBlockHandler extends BaseBlockHandler { type = 'markdown'; render(block: IBlock, isSelected: boolean): string { const isEditMode = block.metadata?.isEditMode ?? true; const content = block.content || ''; return `
$1$1');
    
    // Links
    html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '$1');
    
    // Lists
    html = html.replace(/^\* (.+)$/gm, ''); html = '
' + html + '
'; // Clean up empty paragraphs html = html.replace(/<\/p>/g, ''); html = html.replace(/
( ( ()/g, '$1');
    html = html.replace(/(<\/ul>)<\/p>/g, '$1');
    html = html.replace(/
)/g, '$1');
    html = html.replace(/(<\/pre>)<\/p>/g, '$1');
    
    return html;
  }
  
  private escapeHtml(text: string): string {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
  }
  
  getContent(element: HTMLElement): string {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (editor) {
      return editor.value;
    }
    
    // If in preview mode, return the stored content
    const container = element.querySelector('.markdown-block-container');
    const blockId = container?.getAttribute('data-block-id');
    // In real implementation, would need access to block data
    return '';
  }
  
  setContent(element: HTMLElement, content: string): void {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (editor) {
      editor.value = content;
      this.autoResize(editor);
    }
  }
  
  getCursorPosition(element: HTMLElement): number | null {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    return editor ? editor.selectionStart : null;
  }
  
  setCursorToStart(element: HTMLElement): void {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (editor) {
      editor.selectionStart = editor.selectionEnd = 0;
      editor.focus();
    } else {
      this.focus(element);
    }
  }
  
  setCursorToEnd(element: HTMLElement): void {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (editor) {
      const length = editor.value.length;
      editor.selectionStart = editor.selectionEnd = length;
      editor.focus();
    } else {
      this.focus(element);
    }
  }
  
  focus(element: HTMLElement): void {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (editor) {
      editor.focus();
    } else {
      const preview = element.querySelector('.markdown-preview') as HTMLElement;
      preview?.focus();
    }
  }
  
  focusWithCursor(element: HTMLElement, position: 'start' | 'end' | number = 'end'): void {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (editor) {
      if (position === 'start') {
        this.setCursorToStart(element);
      } else if (position === 'end') {
        this.setCursorToEnd(element);
      } else if (typeof position === 'number') {
        editor.selectionStart = editor.selectionEnd = position;
        editor.focus();
      }
    } else {
      this.focus(element);
    }
  }
  
  getSplitContent(element: HTMLElement): { before: string; after: string } | null {
    const editor = element.querySelector('.markdown-editor') as HTMLTextAreaElement;
    if (!editor) return null;
    
    const cursorPos = editor.selectionStart;
    return {
      before: editor.value.substring(0, cursorPos),
      after: editor.value.substring(cursorPos)
    };
  }
  
  getStyles(): string {
    return `
      /* Markdown Block Container */
      .markdown-block-container {
        position: relative;
        margin: 12px 0;
        border: 1px solid ${cssManager.bdTheme('#e5e7eb', '#374151')};
        border-radius: 6px;
        overflow: hidden;
        transition: all 0.15s ease;
        background: ${cssManager.bdTheme('#ffffff', '#111827')};
      }
      
      .markdown-block-container.selected {
        border-color: ${cssManager.bdTheme('#9ca3af', '#6b7280')};
      }
      
      /* Header */
      .markdown-header {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 8px 12px;
        border-bottom: 1px solid ${cssManager.bdTheme('#e5e7eb', '#374151')};
        background: ${cssManager.bdTheme('#f9fafb', '#0a0a0a')};
      }
      
      .markdown-icon {
        font-size: 14px;
        font-weight: 600;
        opacity: 0.8;
      }
      
      .markdown-title {
        flex: 1;
        font-size: 13px;
        font-weight: 500;
        color: ${cssManager.bdTheme('#374151', '#e5e7eb')};
      }
      
      .markdown-toggle-mode {
        padding: 4px 8px;
        background: transparent;
        border: 1px solid ${cssManager.bdTheme('#e5e7eb', '#374151')};
        border-radius: 4px;
        font-size: 14px;
        cursor: pointer;
        transition: all 0.15s ease;
      }
      
      .markdown-toggle-mode:hover {
        background: ${cssManager.bdTheme('#f3f4f6', '#1f2937')};
        border-color: ${cssManager.bdTheme('#d1d5db', '#4b5563')};
      }
      
      /* Content */
      .markdown-content {
        position: relative;
        min-height: 120px;
      }
      
      /* Editor */
      .markdown-editor {
        width: 100%;
        min-height: 120px;
        padding: 12px;
        background: transparent;
        border: none;
        outline: none;
        resize: none;
        font-family: 'Monaco', 'Consolas', 'Courier New', monospace;
        font-size: 13px;
        line-height: 1.6;
        color: ${cssManager.bdTheme('#1f2937', '#f3f4f6')};
        overflow: hidden;
      }
      
      .markdown-editor::placeholder {
        color: ${cssManager.bdTheme('#9ca3af', '#6b7280')};
      }
      
      /* Preview */
      .markdown-preview {
        padding: 12px;
        min-height: 96px;
        outline: none;
        font-size: 14px;
        line-height: 1.6;
        color: ${cssManager.bdTheme('#1f2937', '#f3f4f6')};
      }
      
      .preview-empty {
        color: ${cssManager.bdTheme('#9ca3af', '#6b7280')};
        font-style: italic;
      }
      
      /* Markdown preview styles */
      .markdown-preview h1 {
        font-size: 24px;
        font-weight: 600;
        margin: 16px 0 8px 0;
        color: ${cssManager.bdTheme('#111827', '#f9fafb')};
      }
      
      .markdown-preview h2 {
        font-size: 20px;
        font-weight: 600;
        margin: 14px 0 6px 0;
        color: ${cssManager.bdTheme('#111827', '#f9fafb')};
      }
      
      .markdown-preview h3 {
        font-size: 18px;
        font-weight: 600;
        margin: 12px 0 4px 0;
        color: ${cssManager.bdTheme('#111827', '#f9fafb')};
      }
      
      .markdown-preview p {
        margin: 8px 0;
      }
      
      .markdown-preview ul,
      .markdown-preview ol {
        margin: 8px 0;
        padding-left: 24px;
      }
      
      .markdown-preview li {
        margin: 4px 0;
      }
      
      .markdown-preview code {
        background: ${cssManager.bdTheme('#f3f4f6', '#1f2937')};
        padding: 2px 4px;
        border-radius: 3px;
        font-family: 'Monaco', 'Consolas', 'Courier New', monospace;
        font-size: 0.9em;
      }
      
      .markdown-preview pre {
        background: ${cssManager.bdTheme('#f3f4f6', '#1f2937')};
        padding: 12px;
        border-radius: 4px;
        overflow-x: auto;
        margin: 8px 0;
      }
      
      .markdown-preview pre code {
        background: transparent;
        padding: 0;
      }
      
      .markdown-preview strong {
        font-weight: 600;
        color: ${cssManager.bdTheme('#111827', '#f9fafb')};
      }
      
      .markdown-preview em {
        font-style: italic;
      }
      
      .markdown-preview a {
        color: ${cssManager.bdTheme('#3b82f6', '#60a5fa')};
        text-decoration: none;
      }
      
      .markdown-preview a:hover {
        text-decoration: underline;
      }
      
      .markdown-preview blockquote {
        border-left: 3px solid ${cssManager.bdTheme('#e5e7eb', '#374151')};
        padding-left: 12px;
        margin: 8px 0;
        color: ${cssManager.bdTheme('#6b7280', '#9ca3af')};
      }
    `;
  }
}