import type {
  OpenFileResponse,
  ReadFileResult,
  SaveFilePayload,
  SaveFileResult,
  SaveAsPayload,
  ReloadFileResult,
  FileStatsResult,
  ReadDirTreeResult
} from '../../shared/types'

export interface IpcInvokeMap {
  'dialog:openFile': [void, OpenFileResponse]
  'file:read': [string, ReadFileResult]
  'file:save': [SaveFilePayload, SaveFileResult]
  'file:saveAs': [SaveAsPayload, SaveFileResult]
  'file:getCurrentPath': [void, string | null]
  'file:stats': [string, FileStatsResult]
  'file:reload': [void, ReloadFileResult]
  'tab:switched': [string | null, void]
  'window:forceClose': [void, void]
  'window:cancelClose': [void, void]
  'dir:readTree': [string, ReadDirTreeResult]
  'dir:openDialog': [void, string | null]
  'dir:watch': [string, void]
  'dir:unwatch': [void, void]
}

export type Unsubscribe = () => void

export interface ElectronAPI {
  openFile: () => Promise<OpenFileResponse>
  readFile: (filePath: string) => Promise<ReadFileResult>
  saveFile: (data: SaveFilePayload) => Promise<SaveFileResult>
  saveFileAs: (data: SaveAsPayload) => Promise<SaveFileResult>
  getCurrentPath: () => Promise<string | null>
  getFileStats: (filePath: string) => Promise<FileStatsResult>
  reloadFile: () => Promise<ReloadFileResult>
  tabSwitched: (filePath: string | null) => Promise<void>
  forceClose: () => Promise<void>
  cancelClose: () => Promise<void>
  openExternal: (url: string) => void
  readDirTree: (dirPath: string) => Promise<ReadDirTreeResult>
  openFolderDialog: () => Promise<string | null>
  watchDir: (dirPath: string) => Promise<void>
  unwatchDir: () => Promise<void>
  onFileOpenInTab: (callback: (data: { filePath: string; content: string }) => void) => Unsubscribe
  onExternalModification: (callback: (filePath: string) => void) => Unsubscribe
  onDirChanged: (callback: () => void) => Unsubscribe
  onConfirmClose: (callback: () => void) => Unsubscribe
}
