/**
 * Type definitions and utility functions for the file viewer plugin system.
 *
 * Defines the FileViewerPlugin interface that each viewer must implement (id, name,
 * canHandle predicate, priority, and a React component), the FileViewerComponentProps
 * passed to viewer components (file path, content, save callback, scroll/highlight
 * options, review context), and helper functions for file extension matching.
 */
export interface FileViewerPlugin {
  /** Unique identifier for this viewer */
  id: string
  /** Display name shown in the viewer selector */
  name: string
  /** Icon to display in the viewer selector */
  icon?: React.ReactNode
  /** Check if this viewer can handle the given file path */
  canHandle: (filePath: string) => boolean
  /** Priority when multiple viewers can handle a file (higher = more preferred as default) */
  priority: number
  /** The component to render the file content */
  component: React.ComponentType<FileViewerComponentProps>
}

export interface EditorActions {
  showOutline: () => void
  showFind: () => void
}

export interface FileViewerComponentProps {
  /** The file path to display */
  filePath: string
  /** The file content (already loaded) */
  content: string
  /** The project/session directory (for resolving tsconfig paths, etc.) */
  directory?: string
  /** Callback when content is modified and should be saved */
  onSave?: (content: string) => Promise<boolean>
  /** Callback when content is modified (for dirty tracking). Passes current content for external save button. */
  onDirtyChange?: (isDirty: boolean, currentContent?: string) => void
  /** Line number to scroll to */
  scrollToLine?: number
  /** Text to highlight in the file */
  searchHighlight?: string
  /** Bumps each time the user explicitly navigates to this path. Viewers that hold internal navigation state (e.g. WebviewViewer) use it to detect re-clicks and force a reload back to filePath. */
  navigationToken?: number
  /** Review context - present when viewing files in a review session */
  reviewContext?: {
    sessionDirectory: string
    commentsFilePath: string
  }
  /** Callback when editor provides actions (outline, etc.) */
  onEditorReady?: (actions: EditorActions | null) => void
  /** Callback when the editor wants to navigate to a different file (e.g. go-to-definition) */
  onOpenFile?: (filePath: string, line?: number) => void
}

/** Get file extension from path */
export function getFileExtension(filePath: string): string {
  return filePath.split('.').pop()?.toLowerCase() || ''
}

/** Check if file matches any of the given extensions */
export function matchesExtensions(filePath: string, extensions: string[]): boolean {
  const ext = getFileExtension(filePath)
  return extensions.includes(ext)
}
