import type { AttendanceHistoryItem } from '@gruperly/shared' /* The history endpoint cuts off at 20 sessions (HISTORY_SESSIONS_LIMIT on the backend). It is the real ceiling on what this screen can show, so "Ver todas" does not promise more than there is. */ export const HISTORY_PREVIEW = 5 export const HISTORY_LIMIT = 20 export type AttendanceSession = AttendanceHistoryItem export function formatSessionShortDate(iso: string): string { const date = new Date(iso) const weekday = date.toLocaleDateString('es-MX', { weekday: 'short' }) const day = date.toLocaleDateString('es-MX', { day: 'numeric' }) const month = date.toLocaleDateString('es-MX', { month: 'short' }) return `${weekday} ${day} ${month}` } export function attendanceStatusLabel(status: AttendanceSession['status']): string { switch (status) { case 'PRESENT': return 'Asistió' case 'ABSENT': return 'Faltó' case 'EXCUSED': return 'Faltó (avisó)' default: return 'Sin registrar' } } /* The status is read through text and icon, not color alone: the status tokens are meant as text over dark surfaces and the green on white sits just below AA, so the row carries all three signals. */ export function attendanceStatusClass(status: AttendanceSession['status']): string { switch (status) { case 'PRESENT': return 'text-success' case 'ABSENT': return 'text-danger' case 'EXCUSED': return 'text-warning' default: return 'text-foreground' } } /** "28 sep 2026". It is the usual short date: it fits on one line on mobile. */ export function formatMemberSince(iso: string): string { return new Date(iso).toLocaleDateString('es-MX', { day: 'numeric', month: 'short', year: 'numeric', }) } /** "1 clase" / "2 clases". */ export function classCountLabel(count: number): string { return count === 1 ? '1 clase' : `${count} clases` } /** What is displayed: from the most recent to the oldest, with the last * `HISTORY_PREVIEW` as the preview. The endpoint returns the opposite order * (oldest → most recent). */ export function visibleSessions( sessions: AttendanceSession[], showAll: boolean, ): AttendanceSession[] { const ordered = [...sessions].reverse() return showAll ? ordered : ordered.slice(0, HISTORY_PREVIEW) } /** Classes with a recorded attendance: attended, was absent or was absent with * notice. The "Sin registrar" ones do not count: without a record there is * neither attendance nor absence, and putting them in the denominator made * "1 de 3" read as two absences. */ function markedSessions(sessions: AttendanceSession[]): AttendanceSession[] { return sessions.filter((session) => session.status != null) } export type AttendanceSummary = { /** "1 de 1 clase", or "Sin clases registradas" if there is none with a record. */ label: string /** "2 sin registrar", or `null` if there are no classes without a record. */ unmarkedLabel: string | null } /** Summary of the rows being shown: how many classes the person recorded out of * how many have a record, and how many were left unmarked. * * The "sin registrar" text is dimmed when there is no marked class: the summary * already says there is no data and the two texts would say the same thing * twice. */ export function attendanceSummary(sessions: AttendanceSession[]): AttendanceSummary { const marked = markedSessions(sessions) if (marked.length === 0) return { label: 'Sin clases registradas', unmarkedLabel: null } const present = marked.filter((session) => session.status === 'PRESENT').length const unmarked = sessions.length - marked.length return { label: `${present} de ${classCountLabel(marked.length)}`, unmarkedLabel: unmarked > 0 ? `${unmarked} sin registrar` : null, } }