import { HealthcareService } from 'fhir/r4'; // Appointment duration model (design: docs/appointments/appointment-durations-and-breaks-design.md). // Effective duration = per-service override org default the 1-hour fallback. All values are integer MINUTES on // a 15-minute grid; storage is uncapped (a hospital stay can run overnight or days). // Per-service override. FHIR HealthcareService has no native appointment-duration field, so this Akashic // extension carries it - byte-identical URL across plugin + infra. export const SERVICE_DEFAULT_DURATION_EXTENSION_URL = 'https://akashichealthrecords.com/fhir/StructureDefinition/service-default-duration'; // The fallback when neither the service nor the org sets a default. Ships at 1 hour. export const DEFAULT_APPOINTMENT_DURATION_MINUTES = 60; // 15-minute-grid quick-picks for the duration controls. Uncapped in storage - a "Custom" entry handles anything // longer than these (overnight / multi-day stays). export const DURATION_QUICK_PICK_MINUTES = [15, 30, 45, 60, 90, 120, 180, 240]; // The per-service override in minutes, if set (and positive). export const getServiceDefaultDurationMinutes = (service?: HealthcareService): number | undefined => { const value = (service?.extension ?? []).find( (e) => e.url === SERVICE_DEFAULT_DURATION_EXTENSION_URL, )?.valueInteger; return typeof value === 'number' && value > 0 ? value : undefined; }; // Effective appointment duration: per-service override org default 1-hour fallback. export const resolveAppointmentDurationMinutes = ( service: HealthcareService | undefined, orgDefaultMinutes: number | undefined, ): number => getServiceDefaultDurationMinutes(service) ?? (orgDefaultMinutes && orgDefaultMinutes > 0 ? orgDefaultMinutes : DEFAULT_APPOINTMENT_DURATION_MINUTES); // Human label for a minute count: 30 "30m", 90 "1h 30m", 240 "4h". export const formatDurationLabel = (minutes: number): string => { const hours = Math.floor(minutes / 60); const mins = minutes % 60; if (hours === 0) return `${mins}m`; if (mins === 0) return `${hours}h`; return `${hours}h ${mins}m`; };