import type { Bundle, Communication } from 'fhir/r4'; /** * The server adds a just-sent message to the FIRST page of a thread read until the search indexes it, and * leaves later pages as searched, so a paged read can carry one message twice. The later copy is the * searched one and wins, in its own position. */ export const oneCopyPerThreadMessage = (bundle: Bundle): Bundle => { const entries = bundle.entry ?? []; const lastIndex = new Map(); entries.forEach((entry, index) => { const id = entry.resource?.id; if (id) lastIndex.set(id, index); }); const kept = entries.filter((entry, index) => { const id = entry.resource?.id; return !id || lastIndex.get(id) === index; }); return kept.length === entries.length ? bundle : { ...bundle, entry: kept }; };