import type { IBlock } from '../wysiwyg.types.js';
import type { IBlockEventHandlers } from '../wysiwyg.interfaces.js';

// Re-export types from the interfaces
export type { IBlockEventHandlers } from '../wysiwyg.interfaces.js';

export interface IBlockContext {
  shadowRoot: ShadowRoot;
  component: any; // Reference to the wysiwyg-block component
}

export interface IBlockHandler {
  type: string;
  render(block: IBlock, isSelected: boolean): string;
  setup(element: HTMLElement, block: IBlock, handlers: IBlockEventHandlers): void;
  getStyles(): string;
  getPlaceholder?(): string;
  
  // Optional methods for editable blocks - now with context
  getContent?(element: HTMLElement, context?: IBlockContext): string;
  setContent?(element: HTMLElement, content: string, context?: IBlockContext): void;
  getCursorPosition?(element: HTMLElement, context?: IBlockContext): number | null;
  setCursorToStart?(element: HTMLElement, context?: IBlockContext): void;
  setCursorToEnd?(element: HTMLElement, context?: IBlockContext): void;
  focus?(element: HTMLElement, context?: IBlockContext): void;
  focusWithCursor?(element: HTMLElement, position: 'start' | 'end' | number, context?: IBlockContext): void;
  getSplitContent?(element: HTMLElement, context?: IBlockContext): { before: string; after: string } | null;
}


export abstract class BaseBlockHandler implements IBlockHandler {
  abstract type: string;
  abstract render(block: IBlock, isSelected: boolean): string;
  
  // Default implementation for common setup
  setup(element: HTMLElement, block: IBlock, handlers: IBlockEventHandlers): void {
    // Common setup logic
  }
  
  // Common styles can be defined here
  getStyles(): string {
    return '';
  }
}