import type { Communication } from 'fhir/r4'; // The server's version number, or null for a copy it has not confirmed (an optimistic line). const versionOf = (message: Communication): number | null => { const version = Number(message.meta?.versionId); return Number.isInteger(version) && version > 0 ? version : null; }; /** * Which copy of one message to show. The incoming copy wins, unless the held copy is a newer confirmed version: the * thread search trails a write, so a re-read can answer with the edition before a read receipt it already delivered. */ export const newerEdition = (held: Communication | undefined, incoming: Communication): Communication => { if (!held) return incoming; const heldVersion = versionOf(held); const incomingVersion = versionOf(incoming); return heldVersion !== null && incomingVersion !== null && heldVersion > incomingVersion ? held : incoming; };