// A staff payment whose outcome is unknown, kept per practice and invoice until the same key is confirmed or // refused, so closing the dialog, the tab or the browser never turns one payment into two. export interface PendingStaffPayment { key: string; cents: number; } const PREFIX = 'akashic.pendingStaffPayment.'; // Used only where local storage throws (a private window, blocked site data). const fallback = new Map(); const slot = (orgId: string, invoiceId: string) => `${PREFIX}${orgId}.${invoiceId}`; export const readPendingStaffPayment = (orgId: string, invoiceId: string): PendingStaffPayment | null => { const name = slot(orgId, invoiceId); try { const raw = window.localStorage.getItem(name); if (!raw) return null; const value = JSON.parse(raw) as Partial; return typeof value.key === 'string' && typeof value.cents === 'number' ? { key: value.key, cents: value.cents } : null; } catch { return fallback.get(name) ?? null; } }; export const writePendingStaffPayment = (orgId: string, invoiceId: string, pending: PendingStaffPayment | null): void => { const name = slot(orgId, invoiceId); try { if (pending) window.localStorage.setItem(name, JSON.stringify(pending)); else window.localStorage.removeItem(name); } catch { if (pending) fallback.set(name, pending); else fallback.delete(name); } };