import type { Book } from '../shared/types.ts'; /** * Rows the person removed from "Cards to print". They are hidden at once; the real request waits so Undo can * bring them back. A book never printed is deleted; one already in the library is only taken off the queue. */ export type RemovalKind = 'delete' | 'dequeue'; export interface Removal { book: Book; kind: RemovalKind } export interface PendingState { items: Record } export const emptyPending: PendingState = { items: {} }; export function removeKind(book: Book): RemovalKind { return book.printedAt ? 'dequeue' : 'delete'; } export function removeLater(s: PendingState, book: Book): PendingState { return { items: { ...s.items, [book.id]: { book, kind: removeKind(book) } } }; } /** Undo (or a failed request): the book is shown again. `removal` is null when nothing was pending. */ export function restore(s: PendingState, id: number): { state: PendingState; removal: Removal | null } { const removal = s.items[id] ?? null; if (!removal) return { state: s, removal: null }; const items = { ...s.items }; delete items[id]; return { state: { items }, removal }; } export function isPending(s: PendingState, id: number): boolean { return id in s.items; } export function pendingIds(s: PendingState): number[] { return Object.keys(s.items).map(Number); } /** For the library: only books waiting to be deleted are hidden. */ export function visible(books: Book[], s: PendingState): Book[] { return books.filter(b => s.items[b.id]?.kind !== 'delete'); } /** For the print list: every removed row is hidden. */ export function queueVisible(books: Book[], s: PendingState): Book[] { return books.filter(b => !isPending(s, b.id)); }