import type { Coding, Observation } from 'fhir/r4'; export const SHARE_ORIGIN_SYSTEM = 'https://akashic.health/fhir/CodeSystem/share-origin'; /** The server-set origins a records release may include; any other origin is held. */ export type ReleasableOrigin = 'native-org' | 'native-patient-report'; // Read only from the server's own label; the client never sets or keeps one. export function releasableOriginOf(observation: Observation | undefined | null): ReleasableOrigin | undefined { const origins = (observation?.meta?.security ?? []).filter((coding: Coding) => coding.system === SHARE_ORIGIN_SYSTEM); if (origins.length !== 1) return undefined; const code = origins[0].code; return code === 'native-org' || code === 'native-patient-report' ? code : undefined; } /** A new entry on a generic form says no source, so a release leaves it out. */ export const GENERIC_ENTRY_HELD_NOTE = 'An observation entered on this form is recorded without where it came from, so a records release leaves it out. To record a vital sign taken here, use Record vitals.'; /** An ordinary edit carries no new source evidence, so the server records the reading as unknown. */ export const editDropsSourceNote = (origin: ReleasableOrigin): string => `This reading is released as ${origin === 'native-org' ? "this practice's own measurement" : "the patient's own report"}. Saving a change on this form records it without that source, so a records release will leave it out.`;