import type {Reservations} from '@/lib/types' /** One number a session took with `midas reserve`. */ export interface Held { ns: string n: number owner: string why: string | null at: number } /** Every number handed out, newest first. */ export function held(r: Reservations): Held[] { return Object.entries(r) .flatMap(([ns, x]) => Object.entries(x.by).map(([n, b]) => ({ns, n: Number(n), owner: b.owner, why: b.why, at: b.at}))) .sort((a, b) => b.at - a.at || a.ns.localeCompare(b.ns) || b.n - a.n) } /** * What the Workspace page lists per namespace: the next free number, the ones * sessions still at work hold (they may not be on `main` yet), and how many * older ones are left out (landed or given up: already on `main` or never used). */ export function byNamespace(r: Reservations, atWork: Set): {ns: string; next: number; open: Held[]; older: number}[] { const all = held(r) return Object.entries(r) .sort((a, b) => a[0].localeCompare(b[0])) .map(([ns, x]) => { const mine = all.filter((h) => h.ns === ns) const open = mine.filter((h) => atWork.has(h.owner)).sort((a, b) => b.n - a.n) return {ns, next: x.last + 1, open, older: mine.length - open.length} }) }