import type { CarePlan } from 'fhir/r4'; /** The parts of a care plan its edit form writes; a field it leaves out was cleared there. */ const FORM_FIELDS = [ 'status', 'intent', 'text', 'category', 'encounter', 'basedOn', 'replaces', 'partOf', 'title', 'description', 'created', 'note', 'contributor', 'careTeam', 'addresses', 'goal', 'activity', 'period', ] as const; /** * A form's save written onto the plan it opened: the form's own fields as edited (cleared ones removed), and every * other field - author, identifier, instantiation, supporting information, extensions - kept as stored. */ export const onTheOpenedPlan = (opened: CarePlan | undefined, edited: CarePlan): CarePlan => { if (!opened) return edited; const kept: CarePlan = { ...opened }; FORM_FIELDS.forEach((field) => { delete kept[field]; }); return { ...kept, ...edited, meta: { ...opened.meta, ...edited.meta }, ...(edited.author ? {} : opened.author ? { author: opened.author } : {}), ...(edited.subject ? {} : opened.subject ? { subject: opened.subject } : {}), }; };