import { ApiError, fetchRosters, quickCheckin } from "../api"; import type { QuickCheckinBookingPayload, QuickCheckinWalkInPayload, RosterAttendee, RosterSession, } from "../types"; import { formatMexicanPhone, getCleanDigitsPhone } from "../utils"; import { showToast } from "./Toast"; /** * Format workshop session capacity label according to spec AC-1: * "Ocupación: {occupied_count} / {max_capacity} ({attended_count} presentes)" */ export function formatRosterCapacity( occupiedCount: number, maxCapacity: number, attendedCount: number, ): string { return `Ocupación: ${occupiedCount} / ${maxCapacity} (${attendedCount} presentes)`; } /** * Render attendance status pill according to section 2.2: * REGISTRADO (Slate/Neutral) vs ASISTIÓ (Emerald/Green) */ export function renderAttendanceStatusBadge( attended: boolean, bookingStatus?: string, ): string { if (attended || bookingStatus === "COMPLETED") { return 'ASISTIÓ'; } return 'REGISTRADO'; } /** * Render payment status pill according to section 2.2: * PAGADO (Emerald/Green) vs PENDIENTE (Amber/Orange) */ export function renderPaymentStatusPill(paymentStatus: string): string { if (paymentStatus === "ACKNOWLEDGED") { return 'PAGADO'; } return 'PENDIENTE'; } /** * Build 1-tap WhatsApp click-to-chat URL */ export function buildWhatsAppUrl(phone: string): string { const clean = getCleanDigitsPhone(phone); return `https://wa.me/${clean}`; } /** * Format ISO datetime to 24-hour time in Mexico City timezone (e.g. 18:00) */ export function formatRosterSessionTime(isoStr: string): string { try { const dt = new Date(isoStr); if (Number.isNaN(dt.getTime())) return isoStr; return new Intl.DateTimeFormat("es-MX", { timeZone: "America/Mexico_City", hour: "2-digit", minute: "2-digit", hour12: false, }).format(dt); } catch { return isoStr; } } /** * Helper to get today's date formatted as YYYY-MM-DD in Mexico City timezone */ export function getTodayDateString(): string { try { const formatter = new Intl.DateTimeFormat("en-CA", { timeZone: "America/Mexico_City", year: "numeric", month: "2-digit", day: "2-digit", }); return formatter.format(new Date()); } catch { const d = new Date(); const y = d.getFullYear(); const m = String(d.getMonth() + 1).padStart(2, "0"); const day = String(d.getDate()).padStart(2, "0"); return `${y}-${m}-${day}`; } } export type ConflictType = | "ALREADY_CHECKED_IN" | "CAPACITY_EXCEEDED" | "UNKNOWN"; /** * Disambiguate HTTP 409 Conflict based on response detail */ export function disambiguate409Error(detail?: string): ConflictType { if (!detail) return "UNKNOWN"; const lower = detail.toLowerCase(); if (lower.includes("already checked in")) { return "ALREADY_CHECKED_IN"; } if (lower.includes("cupo máximo") || lower.includes("cupo maximo")) { return "CAPACITY_EXCEEDED"; } return "UNKNOWN"; } /** * Main component renderer for the Group Workshop Roster view (#rosters) */ export function renderWorkshopRoster( container: HTMLElement, onStateChange?: () => void, ): void { let selectedDate = getTodayDateString(); let cachedSessions: RosterSession[] = []; container.innerHTML = `
Pase de lista digital, cobro en puerta y registro de asistentes sin cita.
Fecha seleccionada: ${dateStr}. Navega a otra fecha para consultar talleres.
Aún no hay asistentes registrados para esta sesión.