import type { Organization } from 'fhir/r4'; // Organization-level booking policy. Mirrors the server's appointment-booking-flags.ts - the // plugin WRITES this exact URL, the server READS it - so the literal MUST match byte for byte. // Uses the StructureDefinition convention shared with the schedule booking flags. export const ORGANIZATION_REQUIRE_NEW_PATIENT_EXAM_FIRST_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-require-new-patient-exam-first'; // Organization booking mode (mirrors the server, byte-identical URL): `direct` or // `request-only`. In request-only, patient self-bookings become `proposed` requests a // receptionist confirms. Default `direct` when the extension is absent. export const ORGANIZATION_BOOKING_MODE_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-booking-mode'; // `direct`: online self-bookings confirm immediately. `review`: a self-booking holds its chosen slot // as `pending` for staff approval. `request-only`: a self-booking becomes a `proposed` request staff // schedule. The server enforces this; the plugin mirrors the extension URL. export type OrganizationBookingMode = 'direct' | 'review' | 'request-only'; // How a request-only organization wants patient self-bookings expressed. `preferred-slot`: patient // picks a preferred practitioner + a real (held) slot. `time-window`: patient gives preferred days + // times of day with no practitioner/slot, and the receptionist places it. Only meaningful under // request-only. Default `preferred-slot` when the extension is absent. export const ORGANIZATION_REQUEST_STYLE_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-request-style'; export type OrganizationRequestStyle = 'preferred-slot' | 'time-window'; // Retired: the consult is now recognised by its service's SNOMED type, not by a role list. Kept only so // saves strip the stale extension from organizations that still carry it. export const RETIRED_ORGANIZATION_NEW_PATIENT_EXAM_ROLES_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-new-patient-exam-roles'; // Org-wide default appointment duration (minutes). Every appointment inherits it unless the Visit service // overrides it (see config/appointmentDuration). Byte-identical URL across plugin + infra. Ships at 60 min. export const ORGANIZATION_DEFAULT_APPOINTMENT_DURATION_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/organization-default-appointment-duration'; // Pure reader - safe outside hook/React context (e.g. patient booking gating). Defaults: // require-new-patient-exam-first false, booking mode `direct`. export const getOrganizationPolicyFlags = ( organization?: Organization, ): { requireNewPatientExamFirst: boolean; bookingMode: OrganizationBookingMode; requestStyle: OrganizationRequestStyle; defaultAppointmentDurationMinutes: number; } => { const extensions = organization?.extension ?? []; const bookingModeCode = extensions.find( (e) => e.url === ORGANIZATION_BOOKING_MODE_EXTENSION_URL, )?.valueCode; return { requireNewPatientExamFirst: extensions.find((e) => e.url === ORGANIZATION_REQUIRE_NEW_PATIENT_EXAM_FIRST_EXTENSION_URL) ?.valueBoolean ?? false, bookingMode: bookingModeCode === 'request-only' ? 'request-only' : bookingModeCode === 'review' ? 'review' : 'direct', requestStyle: extensions.find((e) => e.url === ORGANIZATION_REQUEST_STYLE_EXTENSION_URL)?.valueCode === 'time-window' ? 'time-window' : 'preferred-slot', // Org-wide default appointment length; the 1-hour ship-default when unset. defaultAppointmentDurationMinutes: extensions.find((e) => e.url === ORGANIZATION_DEFAULT_APPOINTMENT_DURATION_EXTENSION_URL) ?.valueInteger || 60, }; };