// The patient's three words for a plan, over FHIR Coverage.status (active | cancelled | draft | // entered-in-error). FHIR has no "expired": a lapsed plan is active with a past period.end. export type CoverageStatusChoice = 'active' | 'cancelled' | 'expired'; export const COVERAGE_STATUS_OPTIONS: { label: string; value: CoverageStatusChoice }[] = [ { label: 'Active', value: 'active' }, { label: 'Cancelled', value: 'cancelled' }, { label: 'Expired', value: 'expired' }, ]; /** True when a FHIR date has passed, by the same rule the list uses to badge a plan Expired. */ export const endHasPassed = (periodEnd: string | undefined, now: Date = new Date()): boolean => !!periodEnd && new Date(periodEnd) < now; export const statusChoiceOf = (fhirStatus: string | undefined, periodEnd: string | undefined, now?: Date): CoverageStatusChoice => { if (fhirStatus === 'cancelled') return 'cancelled'; return endHasPassed(periodEnd, now) ? 'expired' : 'active'; }; /** Expired and Active both store `active`; the end date is what tells them apart. */ export const fhirStatusFor = (choice: CoverageStatusChoice): 'active' | 'cancelled' => choice === 'cancelled' ? 'cancelled' : 'active'; /** Why the chosen status and the Effective To date disagree, or undefined when they agree. */ export const statusDateConflict = (choice: CoverageStatusChoice, periodEnd: string | undefined, now?: Date): string | undefined => { if (choice === 'expired' && !endHasPassed(periodEnd, now)) { return 'An expired plan needs the date it ended. Enter it in Effective To.'; } if (choice === 'active' && endHasPassed(periodEnd, now)) { return 'Effective To has passed, so this plan has expired. Change the date, or set Status to Expired.'; } return undefined; };