import { contextBridge,ipcRenderer } from 'electron' import type { ReceiptAPI,ExportProgress,ConfirmationRequest,ProcessingTask } from '../shared/types' async function call(method:string,...args:unknown[]):Promise{ const result=await ipcRenderer.invoke('receipt:'+method,...args) if(!result.ok)throw Error(result.error) return result.value as T } const api:ReceiptAPI={ bootstrap:()=>call('bootstrap'),saveSettings:s=>call('saveSettings',s),chooseFolder:k=>call('chooseFolder',k), importExcel:()=>call('importExcel'),saveExcelTemplate:()=>call('saveExcelTemplate'),listSeals:()=>call('listSeals'), preview:r=>call('preview',r),exportReceipts:r=>call('exportReceipts',r),cancelExport:()=>call('cancelExport'), listBatches:query=>call('listBatches',query),listBatchPage:query=>call('listBatchPage',query),deleteBatch:id=>call('deleteBatch',id),openFolder:p=>call('openFolder',p), answerConfirmation:(id,confirmed)=>call('answerConfirmation',{id,confirmed}), onProcessing:callback=>{const listener=(_event:Electron.IpcRendererEvent,task:ProcessingTask)=>callback(task);ipcRenderer.on('receipt:processing',listener);return()=>ipcRenderer.removeListener('receipt:processing',listener)}, onConfirmation:callback=>{const listener=(_event:Electron.IpcRendererEvent,data:ConfirmationRequest)=>callback(data);ipcRenderer.on('receipt:confirmation',listener);return()=>ipcRenderer.removeListener('receipt:confirmation',listener)}, onProgress:callback=>{const listener=(_event:Electron.IpcRendererEvent,data:ExportProgress)=>callback(data);ipcRenderer.on('receipt:progress',listener);return()=>ipcRenderer.removeListener('receipt:progress',listener)} } contextBridge.exposeInMainWorld('receipts',api)