// 使用 require 避免 ESM 问题
const { contextBridge, ipcRenderer } = require('electron');

// 暴露给渲染进程的 API
contextBridge.exposeInMainWorld('electronAPI', {
  // 应用信息
  getAppVersion: () => ipcRenderer.invoke('get-app-version'),
  getPlatform: () => ipcRenderer.invoke('get-platform'),

  // 窗口控制
  minimizeWindow: () => ipcRenderer.send('window-minimize'),
  maximizeWindow: () => ipcRenderer.send('window-maximize'),
  closeWindow: () => ipcRenderer.send('window-close'),
  quitApp: () => ipcRenderer.send('app-quit'), // 真正退出应用
  isMaximized: () => ipcRenderer.invoke('window-is-maximized'),
  onMaximizedChange: (callback: (isMaximized: boolean) => void) => {
    ipcRenderer.on('window-maximized', (_event: unknown, isMaximized: boolean) => callback(isMaximized));
  },

  // 文件操作
  selectFile: (options?: { filters?: { name: string; extensions: string[] }[] }) =>
    ipcRenderer.invoke('select-file', options),
  selectFolder: () => ipcRenderer.invoke('select-folder'),

  // 通知
  showNotification: (title: string, body: string) =>
    ipcRenderer.send('show-notification', { title, body }),

  // Webview Cookie 操作
  getWebviewCookies: (partition: string, url: string) =>
    ipcRenderer.invoke('get-webview-cookies', partition, url),
  clearWebviewCookies: (partition: string) =>
    ipcRenderer.invoke('clear-webview-cookies', partition),
  setWebviewCookies: (partition: string, cookies: Electron.CookiesSetDetails[]) =>
    ipcRenderer.invoke('set-webview-cookies', partition, cookies),

  // Webview 截图（用于 AI 分析）
  captureWebviewPage: (webContentsId: number) =>
    ipcRenderer.invoke('capture-webview-page', webContentsId),

  // Webview 自动操作
  webviewSendMouseClick: (webContentsId: number, x: number, y: number) =>
    ipcRenderer.invoke('webview-send-mouse-click', webContentsId, x, y),
  webviewSendTextInput: (webContentsId: number, text: string) =>
    ipcRenderer.invoke('webview-send-text-input', webContentsId, text),
  webviewGetElementPosition: (webContentsId: number, selector: string) =>
    ipcRenderer.invoke('webview-get-element-position', webContentsId, selector),
  webviewClickByText: (webContentsId: number, text: string) =>
    ipcRenderer.invoke('webview-click-by-text', webContentsId, text),

  // CDP 网络拦截
  enableNetworkIntercept: (webContentsId: number, patterns: Array<{match: string, key: string}>) =>
    ipcRenderer.invoke('enable-network-intercept', webContentsId, patterns),
  disableNetworkIntercept: (webContentsId: number) =>
    ipcRenderer.invoke('disable-network-intercept', webContentsId),
  updateNetworkPatterns: (webContentsId: number, patterns: Array<{match: string, key: string}>) =>
    ipcRenderer.invoke('update-network-patterns', webContentsId, patterns),
  onNetworkInterceptData: (callback: (data: { webContentsId: number; key: string; url: string; data: unknown }) => void) => {
    ipcRenderer.on('network-intercept-data', (_event: unknown, data: { webContentsId: number; key: string; url: string; data: unknown }) => callback(data));
  },
  removeNetworkInterceptListener: () => {
    ipcRenderer.removeAllListeners('network-intercept-data');
  },
});

// 类型声明
declare global {
  interface Window {
    electronAPI: {
      getAppVersion: () => Promise<string>;
      getPlatform: () => Promise<string>;
      minimizeWindow: () => void;
      maximizeWindow: () => void;
      closeWindow: () => void;
      quitApp: () => void;
      isMaximized: () => Promise<boolean>;
      onMaximizedChange: (callback: (isMaximized: boolean) => void) => void;
      selectFile: (options?: { filters?: { name: string; extensions: string[] }[] }) => Promise<string | null>;
      selectFolder: () => Promise<string | null>;
      showNotification: (title: string, body: string) => void;
      getWebviewCookies: (partition: string, url: string) => Promise<Electron.Cookie[]>;
      clearWebviewCookies: (partition: string) => Promise<boolean>;
      setWebviewCookies: (partition: string, cookies: Electron.CookiesSetDetails[]) => Promise<boolean>;
      captureWebviewPage: (webContentsId: number) => Promise<string | null>;
      webviewSendMouseClick: (webContentsId: number, x: number, y: number) => Promise<boolean>;
      webviewSendTextInput: (webContentsId: number, text: string) => Promise<boolean>;
      webviewGetElementPosition: (webContentsId: number, selector: string) => Promise<{ x: number; y: number; width: number; height: number } | null>;
      webviewClickByText: (webContentsId: number, text: string) => Promise<boolean>;
      // CDP 网络拦截
      enableNetworkIntercept: (webContentsId: number, patterns: Array<{match: string, key: string}>) => Promise<boolean>;
      disableNetworkIntercept: (webContentsId: number) => Promise<boolean>;
      updateNetworkPatterns: (webContentsId: number, patterns: Array<{match: string, key: string}>) => Promise<boolean>;
      onNetworkInterceptData: (callback: (data: { webContentsId: number; key: string; url: string; data: unknown }) => void) => void;
      removeNetworkInterceptListener: () => void;
    };
  }
}
