import { ipcMain } from 'electron' import { IPC } from '../../../shared/ipc-channels' import type { UndoOperation } from '../../../shared/undo' import { UndoStackService } from '../../services/undo-stack.service' import type { BookRegistryService } from '../../services/book-registry' import { wrap } from '../result' export function registerUndoHandlers(registry: BookRegistryService): void { ipcMain.handle( IPC.UNDO_PUSH, ( _e, { bookId, chapterId, operation }: { bookId: string; chapterId: string; operation: UndoOperation } ) => wrap(() => new UndoStackService(registry.getDb(bookId)).push(chapterId, operation)) ) ipcMain.handle( IPC.UNDO_LIST, (_e, { bookId, chapterId }: { bookId: string; chapterId: string }) => wrap(() => new UndoStackService(registry.getDb(bookId)).list(chapterId)) ) ipcMain.handle( IPC.UNDO_APPLY, (_e, { bookId, entryId }: { bookId: string; entryId: number }) => wrap(() => { const op = new UndoStackService(registry.getDb(bookId)).apply(entryId) if (!op) throw new Error('Undo entry not found') return op }) ) }