import type { Book } from '../shared/types.ts'; /** The single state a book is shown in. */ export type RowState = | { kind: 'looking'; note: string | null } | { kind: 'ready' } | { kind: 'attention'; reason: string }; export function rowState(book: Book): RowState { if (book.status === 'pending') return { kind: 'looking', note: book.attentionReason }; if (book.status === 'resolved' && book.callNumber) return { kind: 'ready' }; if (book.status === 'resolved') return { kind: 'attention', reason: 'No call number was found.' }; return { kind: 'attention', reason: book.attentionReason ?? 'This book needs a look.' }; } /** Queued books: the ones needing attention first, then newest first. */ export function queueOrder(books: Book[]): Book[] { const rank = (b: Book) => (rowState(b).kind === 'attention' ? 0 : 1); return books .filter(b => b.inQueue) .sort((a, b) => { if (rank(a) !== rank(b)) return rank(a) - rank(b); if (a.createdAt !== b.createdAt) return a.createdAt < b.createdAt ? 1 : -1; return b.id - a.id; }); } export interface PrintStatus { ready: number; looking: number; label: string; reason: string | null } /** What the print button says, and why it is disabled when it is. */ export function printStatus(queue: Book[]): PrintStatus { let ready = 0, looking = 0; for (const b of queue) { const k = rowState(b).kind; if (k === 'ready') ready++; else if (k === 'looking') looking++; } const label = ready === 0 ? 'Print cards' : `Print ${ready} ${ready === 1 ? 'card' : 'cards'}`; const reason = looking ? `${looking} still looking up` : ready === 0 ? 'Nothing to print yet' : null; return { ready, looking, label, reason }; } /** "12 s", "1 min 05 s" */ export function elapsedText(ms: number): string { const s = Math.max(0, Math.floor(ms / 1000)); return s < 60 ? `${s} s` : `${Math.floor(s / 60)} min ${String(s % 60).padStart(2, '0')} s`; }