/**
 * Renderer-side IPC entry: import { dialog, fs, clipboard, shell, app, windowApi, mixone } from '#mixone/ipc'
 *
 * This file is generated by mixone create. All methods forward to window.mixone.* bridge (injected by preload).
 * Type contracts are inlined here (strictly matching mixone-ipc.d.ts) to avoid circular .ts/.d.ts imports.
 * mixone-ipc.d.ts serves as external contract documentation (consumers may reference it, but runtime proxy goes through here).
 */
import type { MixoneWindowApi } from './window-api'

// === Type contracts (strictly matching ./mixone-ipc.d.ts) ===
export interface FileFilter {
  name: string
  extensions: string[]
}
export interface OpenFileOptions {
  title?: string
  defaultPath?: string
  properties?: Array<'openFile' | 'openDirectory' | 'multiSelections' | 'showHiddenFiles'>
  filters?: FileFilter[]
}
export interface OpenFileResult {
  filePaths: string[]
  canceled: boolean
  bookmarks?: string[]
}
export interface SaveFileOptions {
  title?: string
  defaultPath?: string
  filters?: FileFilter[]
}
export interface SaveFileResult {
  filePath: string | null
  canceled: boolean
}
export interface MessageBoxOptions {
  type?: 'none' | 'info' | 'error' | 'question' | 'warning'
  title?: string
  message: string
  detail?: string
  buttons?: string[]
  defaultId?: number
  cancelId?: number
}
export interface MessageBoxResult {
  response: number
  checkboxResponse: boolean
  canceled: boolean
}
export interface MixoneDialogApi {
  openFile: (options?: OpenFileOptions) => Promise<OpenFileResult>
  saveFile: (options?: SaveFileOptions) => Promise<SaveFileResult>
  messageBox: (options: MessageBoxOptions) => Promise<MessageBoxResult>
}
export interface ReadTextOptions { encoding?: BufferEncoding }
export interface ReadTextResult { content: string; size: number }
export interface WriteTextOptions { encoding?: BufferEncoding }
export interface FsStats { size: number; isFile: boolean; isDirectory: boolean; mtime: number }
export interface DirEntry { name: string; isFile: boolean; isDirectory: boolean; isSymbolicLink: boolean }
export interface ReadDirResult { entries: DirEntry[]; error?: string }
export interface MixoneFsApi {
  readText: (filePath: string, options?: ReadTextOptions) => Promise<ReadTextResult>
  writeText: (filePath: string, content: string, options?: WriteTextOptions) => Promise<{ bytesWritten: number }>
  exists: (filePath: string) => Promise<boolean>
  stat: (filePath: string) => Promise<FsStats>
  unlinkSafe: (filePath: string) => Promise<{ success: boolean }>
  readDir: (dirPath: string) => Promise<ReadDirResult>
  mkdir: (dirPath: string, options?: { recursive?: boolean }) => Promise<{ success: boolean; error?: string }>
  rename: (oldPath: string, newPath: string) => Promise<{ success: boolean; error?: string }>
  copyFile: (src: string, dest: string) => Promise<{ success: boolean; error?: string }>
  appendText: (filePath: string, content: string, options?: WriteTextOptions) => Promise<{ success: boolean; error?: string }>
  readJson: (filePath: string) => Promise<{ data: any; error: string | null }>
  writeJson: (filePath: string, data: any, options?: { space?: number }) => Promise<{ success: boolean; error?: string }>
}
export interface ClipboardImageInfo { empty: boolean; size: { width: number; height: number } | null }
export interface MixoneClipboardApi {
  readText: () => Promise<string>
  writeText: (text: string) => Promise<{ success: true }>
  readHtml: () => Promise<string>
  writeHtml: (html: string) => Promise<{ success: true }>
  readImage: () => Promise<ClipboardImageInfo>
  clear: () => Promise<{ success: true }>
}
export interface MixoneShellApi {
  openExternal: (url: string, options?: { activate?: boolean }) => Promise<{ success: true }>
  showItemInFolder: (fullPath: string) => Promise<{ success: true }>
  openPath: (fullPath: string) => Promise<string>
  beep: () => Promise<{ success: true }>
}
export interface AppInfoResult {
  name: string
  version: string
  electron: string
  node: string
  chrome: string
  platform: NodeJS.Platform
  cwd: string
}
export interface MixoneAppApi {
  info: () => Promise<AppInfoResult>
  relaunch: () => Promise<{ success: true }>
  quit: () => Promise<{ success: true }>
}
export interface MixoneIpcApi {
  platform: NodeJS.Platform
  version: string
  node: string
  chrome: string
  isDesktop: true
  window: MixoneWindowApi
  dialog: MixoneDialogApi
  fs: MixoneFsApi
  clipboard: MixoneClipboardApi
  shell: MixoneShellApi
  app: MixoneAppApi
}

declare global {
  interface Window {
    {{bridgeGlobal}}: MixoneIpcApi
  }
}

const bridge = (typeof window !== 'undefined' ? window.{{bridgeGlobal}} : undefined) as MixoneIpcApi | undefined

if (!bridge) {
  console.error('[mixone/ipc] bridge not injected: window.{{bridgeGlobal}} does not exist, please ensure preload is loaded')
}

export const windowApi: MixoneWindowApi = bridge!.window
export const dialog: MixoneDialogApi = bridge!.dialog
export const fs: MixoneFsApi = bridge!.fs
export const clipboard: MixoneClipboardApi = bridge!.clipboard
export const shell: MixoneShellApi = bridge!.shell
export const app: MixoneAppApi = bridge!.app

export const mixone = bridge!
export default mixone