import type { AbsenceRequest, AbsenceStatus, AbsenceType, HalfDay } from '~/services/dto/absence' export type BadgeVariant = 'neutral' | 'info' | 'success' | 'warning' | 'danger' const STATUS_VARIANTS: Record = { pending: 'warning', approved: 'success', rejected: 'danger', cancelled: 'neutral', } const STATUS_ICONS: Record = { pending: 'mdi:clock-outline', approved: 'mdi:check-circle-outline', rejected: 'mdi:close-circle-outline', cancelled: 'mdi:cancel', } // Colours used for the calendar bars, keyed by absence type. const TYPE_COLORS: Record = { cp: '#4A90D9', mariage_pacs: '#E91E63', conge_parental: '#9C27B0', deces: '#607D8B', maladie: '#C62828', } export function useAbsenceHelpers() { const { t } = useI18n() function statusLabel(status: AbsenceStatus): string { return t(`absences.status.${status}`) } function statusVariant(status: AbsenceStatus): BadgeVariant { return STATUS_VARIANTS[status] ?? 'neutral' } function statusIcon(status: AbsenceStatus): string { return STATUS_ICONS[status] ?? 'mdi:help-circle-outline' } function typeLabel(type: AbsenceType): string { return t(`absences.types.${type}`) } function typeColor(type: AbsenceType): string { return TYPE_COLORS[type] ?? '#9CA3AF' } function halfDayLabel(half: HalfDay): string { return t(`absences.halfDay.${half}`) } function formatDate(iso: string | null): string { if (!iso) return '' const d = new Date(iso) if (isNaN(d.getTime())) return '' const day = String(d.getDate()).padStart(2, '0') const month = String(d.getMonth() + 1).padStart(2, '0') return `${day}/${month}/${d.getFullYear()}` } /** Human-readable period with half-day annotations. */ function formatRange(req: Pick): string { const start = formatDate(req.startDate) const end = formatDate(req.endDate) const startSuffix = req.startHalfDay ? ` (${halfDayLabel(req.startHalfDay)})` : '' const endSuffix = req.endHalfDay ? ` (${halfDayLabel(req.endHalfDay)})` : '' if (start === end) { return `${start}${startSuffix}` } return `${start}${startSuffix} → ${end}${endSuffix}` } function formatDays(days: number): string { const rounded = Math.round(days * 2) / 2 const unit = rounded > 1 ? t('absences.daysPlural') : t('absences.daySingular') return `${rounded} ${unit}` } return { statusLabel, statusVariant, statusIcon, typeLabel, typeColor, halfDayLabel, formatDate, formatRange, formatDays, } }