import { contextBridge, ipcRenderer, type IpcRendererEvent } from 'electron' import type { Api } from '@shared/api' function on(channel: string, cb: (...args: T) => void): () => void { const listener = (_e: IpcRendererEvent, ...args: unknown[]): void => cb(...(args as T)) ipcRenderer.on(channel, listener) return () => ipcRenderer.removeListener(channel, listener) } const api: Api = { getState: () => ipcRenderer.invoke('getState'), testCredentials: (creds) => ipcRenderer.invoke('testCredentials', creds), saveSetup: (creds, settings) => ipcRenderer.invoke('saveSetup', creds, settings), updateSettings: (settings) => ipcRenderer.invoke('updateSettings', settings), setTheme: (theme) => ipcRenderer.invoke('setTheme', theme), signOut: () => ipcRenderer.invoke('signOut'), connectGoogleVoice: () => ipcRenderer.invoke('connectGoogleVoice'), connectGoogleVoiceCookies: (text) => ipcRenderer.invoke('connectGoogleVoiceCookies', text), disconnectGoogleVoice: () => ipcRenderer.invoke('disconnectGoogleVoice'), rabbitStatus: () => ipcRenderer.invoke('rabbitStatus'), rabbitInstall: (mode) => ipcRenderer.invoke('rabbitInstall', mode), rabbitSync: () => ipcRenderer.invoke('rabbitSync'), onRabbitProgress: (cb) => on('rabbitProgress', cb), listContacts: () => ipcRenderer.invoke('listContacts'), listAddressBooks: () => ipcRenderer.invoke('listAddressBooks'), addContact: (c) => ipcRenderer.invoke('addContact', c), listConversations: () => ipcRenderer.invoke('listConversations'), getMessages: (did, contact) => ipcRenderer.invoke('getMessages', did, contact), markRead: (did, contact) => ipcRenderer.invoke('markRead', did, contact), markUnread: (did, contact) => ipcRenderer.invoke('markUnread', did, contact), setArchived: (did, contact, archived) => ipcRenderer.invoke('setArchived', did, contact, archived), setName: (did, contact, name) => ipcRenderer.invoke('setName', did, contact, name), getDrafts: () => ipcRenderer.invoke('getDrafts'), setDraft: (contact, text) => ipcRenderer.invoke('setDraft', contact, text), search: (query, scope) => ipcRenderer.invoke('search', query, scope), send: (did, contact, text, attachments) => ipcRenderer.invoke('send', did, contact, text, attachments), deleteFailed: (id) => ipcRenderer.invoke('deleteFailed', id), retryFailed: (id) => ipcRenderer.invoke('retryFailed', id), saveMedia: (id, index) => ipcRenderer.invoke('saveMedia', id, index), exportChat: (did, contact, opts) => ipcRenderer.invoke('exportChat', did, contact, opts), readMedia: (id, index) => ipcRenderer.invoke('readMedia', id, index), copyImage: (png) => ipcRenderer.invoke('copyImage', png), syncStatus: () => ipcRenderer.invoke('syncStatus'), pollNow: () => ipcRenderer.invoke('pollNow'), openDataFolder: () => ipcRenderer.invoke('openDataFolder'), snapshotNow: () => ipcRenderer.invoke('snapshotNow'), chooseDataDir: () => ipcRenderer.invoke('chooseDataDir'), onMessages: (cb) => on('messages', cb), onConversationsChanged: (cb) => on('conversationsChanged', cb), onSyncStatus: (cb) => on('syncStatus', cb), onContactsChanged: (cb) => on('contactsChanged', cb), onStateChanged: (cb) => on('stateChanged', cb), onOpenConversation: (cb) => on('openConversation', cb) } contextBridge.exposeInMainWorld('api', api)