import type { Extension, HealthcareService } from 'fhir/r4'; import { fromFhirMoney, toFhirMoney } from '../utils/money'; // Per-service price. FHIR HealthcareService has no native price field, so this Akashic extension carries it - // byte-identical URL across plugin + infra. Stored as FHIR Money (decimal); read/written in integer cents. export const SERVICE_PRICE_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/service-price'; // The service's price in integer cents, if set (and non-negative). Undefined = unpriced. export const getServicePriceCents = (service?: HealthcareService | null): number | undefined => { const money = (service?.extension ?? []).find( (e) => e.url === SERVICE_PRICE_EXTENSION_URL, )?.valueMoney; if (!money || typeof money.value !== 'number') { return undefined; } const cents = fromFhirMoney(money); return cents >= 0 ? cents : undefined; }; // Strip-then-append the price extension (mirrors AddServiceModal's inline extension writes). Passing // `undefined` removes the price. // // - KEEP THE EMPTY ARRAY. It used to collapse an empty result to `extension: undefined`, which // made "I curated the extensions and there are none left" byte-identical to the bare // transformToFhirHealthcareService output ("I never considered extensions"). The server's // preserveHealthcareServiceFeatureExtensions carries the stored extensions forward on the second case, // so collapsing here meant clearing the LAST extension silently resurrected it on the next save. // An explicit `[]` is the signal that the omission was deliberate; the server normalizes it away before // writing, since HealthLake rejects empty arrays. export const applyServicePrice = ( service: HealthcareService, cents: number | undefined, ): HealthcareService => { const others: Extension[] = (service.extension ?? []).filter( (e) => e.url !== SERVICE_PRICE_EXTENSION_URL, ); const next: Extension[] = cents === undefined ? others : [...others, { url: SERVICE_PRICE_EXTENSION_URL, valueMoney: toFhirMoney(cents) }]; return { ...service, extension: next }; };