type StoragePort = Pick; const KEY = 'akashic_savedClinicianCheckout_v1'; const digest = (value: unknown): value is string => typeof value === 'string' && /^[a-f0-9]{64}$/.test(value); // Hashes only; retain uncertain purchases across reload without keeping role details or checkout secrets. export function claimSavedClinicianCheckout(storage: StoragePort, scope: string, initialize: boolean): void { const refused = () => new Error('The checkout result is not confirmed here. Check billing before another purchase.'); if (!digest(scope)) throw refused(); const raw = storage.getItem(KEY); let held: string[]; if (raw === null) { if (!initialize) throw refused(); held = []; } else { let value: unknown; try { value = JSON.parse(raw); } catch { throw refused(); } if (!value || typeof value !== 'object' || Array.isArray(value)) throw refused(); const record = value as Record; if (record.v !== 1 || Object.keys(record).some(key => !['v', 'held'].includes(key)) || !Array.isArray(record.held) || record.held.length > 20 || !record.held.every(digest) || new Set(record.held).size !== record.held.length) throw refused(); held = record.held; } if (held.includes(scope) || held.length >= 20) throw refused(); const next = JSON.stringify({ v: 1, held: [...held, scope] }); storage.setItem(KEY, next); if (storage.getItem(KEY) !== next) throw refused(); }