import { Organization } from 'fhir/r4'; export const ORG_FORM_SUBMISSION_NOTIFICATIONS_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-form-submission-notifications'; export interface FormSubmissionNotificationSettings { enabled: boolean; recipients: string[]; } export const getFormSubmissionNotifications = ( organization?: Organization | null, ): FormSubmissionNotificationSettings | undefined => { const ext = organization?.extension?.find(e => e.url === ORG_FORM_SUBMISSION_NOTIFICATIONS_EXTENSION_URL); if (!ext) return undefined; const enabled = ext.extension?.find(s => s.url === 'enabled')?.valueBoolean ?? true; const recipients = (ext.extension ?? []) .filter(s => s.url === 'recipient') .map(s => s.valueString) .filter((v): v is string => !!v); return { enabled, recipients }; }; // Effective/default for display: enabled + seeded from org contact email when nothing saved yet (R4). export const getEffectiveFormSubmissionNotifications = ( organization?: Organization | null, ): FormSubmissionNotificationSettings => { const saved = getFormSubmissionNotifications(organization); if (saved) return saved; const email = organization?.telecom?.find(t => t.system === 'email' && !!t.value)?.value; return { enabled: true, recipients: email ? [email] : [] }; };