/** * Cheque identity, lifetime, and reserved-goods helpers. * Matching is exact on itemId + full instance JSON so future metadata fields * automatically participate in which vault stacks a cheque can reserve. * * Note: filled cheque item id is duplicated here to avoid a circular import with * `definitions/items.js` (`ItemId.filledCheque === 7`). */ /** @see ItemId.filledCheque in definitions/items.js */ export const FILLED_CHEQUE_ITEM_ID = 7; /** Written cheques lapse after 30 calendar days from `createdAt`. */ export const CHEQUE_LIFETIME_MS = 30 * 24 * 60 * 60 * 1000; /** Stable identity for cheque reservation / vault selection (ignores qty & origins). */ export function stackIdentityKey (stack) { return JSON.stringify({ itemId: stack?.itemId ?? null, instance: stack?.instance ?? null }); } /** True when two stacks are the same item with identical instance metadata. */ export function stacksIdentical (left, right) { if (!left || !right) { return false; } return stackIdentityKey(left) === stackIdentityKey(right); } export function countIdenticalInSlots (slots, reference) { if (!reference?.itemId) { return 0; } let total = 0; for (const slot of slots ?? []) { if (!stacksIdentical(slot, reference)) { continue; } total += Math.max(0, Math.floor(Number(slot.quantity) || 0)); } return total; } export function isFilledChequeStack (stack) { return (stack?.itemId | 0) === FILLED_CHEQUE_ITEM_ID && stack?.instance?.kind === "filled-cheque"; } export function chequeCreatedAtMs (stack) { if (stack?.instance?.createdAt == null || stack.instance.createdAt === "") { return null; } const created = Number(stack.instance.createdAt); return Number.isFinite(created) ? created : null; } export function chequeExpiresAtMs (stack) { const created = chequeCreatedAtMs(stack); return created == null ? 0 : created + CHEQUE_LIFETIME_MS; } export function chequeIsExpired (stack, now = Date.now()) { if (!isFilledChequeStack(stack)) { return false; } const created = chequeCreatedAtMs(stack); if (created == null) { return false; } return now >= created + CHEQUE_LIFETIME_MS; } /** Whole days remaining until expiry (ceil). 0 if already expired; null if unknown. */ export function chequeDaysRemaining (stack, now = Date.now()) { if (!isFilledChequeStack(stack)) { return null; } const created = chequeCreatedAtMs(stack); if (created == null) { return null; } const ms = created + CHEQUE_LIFETIME_MS - now; if (ms <= 0) { return 0; } return Math.ceil(ms / (24 * 60 * 60 * 1000)); } export function chequeExpiryTooltipLine (stack, now = Date.now()) { const days = chequeDaysRemaining(stack, now); if (days == null) { return ""; } if (days <= 0) { return "Expired."; } if (days === 1) { return "Expires in 1 day."; } return `Expires in ${days} days.`; } /** Stack shape for the goods reserved on a filled cheque (icons / names). */ export function chequeReservedGoodsStack (chequeStack) { const instance = chequeStack?.instance; if (!instance || instance.kind !== "filled-cheque") { return null; } return { itemId: instance.itemId | 0, quantity: Math.max(1, instance.quantity | 0 || 1), instance: instance.reservedInstance ?? null }; }