'use client'; import { useMemo } from 'react'; import { useI18n } from './I18nProvider'; type MonthView = { label: string; days: { label: string; date: string; blocked: boolean; isFiller: boolean }[]; }; function buildMonths(monthCount: number, blocked: Set): MonthView[] { const months: MonthView[] = []; const base = new Date(); base.setUTCDate(1); for (let i = 0; i < monthCount; i += 1) { const monthDate = new Date(base); monthDate.setUTCMonth(base.getUTCMonth() + i); const year = monthDate.getUTCFullYear(); const month = monthDate.getUTCMonth(); const label = monthDate.toLocaleDateString(undefined, { month: 'long', year: 'numeric' }); const firstDay = new Date(Date.UTC(year, month, 1)); const startWeekday = firstDay.getUTCDay(); // 0=Sun const daysInMonth = new Date(Date.UTC(year, month + 1, 0)).getUTCDate(); const days: MonthView['days'] = []; for (let f = 0; f < startWeekday; f += 1) { days.push({ label: '', date: '', blocked: false, isFiller: true }); } for (let d = 1; d <= daysInMonth; d += 1) { const date = new Date(Date.UTC(year, month, d)); const iso = date.toISOString().slice(0, 10); days.push({ label: String(d), date: iso, blocked: blocked.has(iso), isFiller: false }); } months.push({ label, days }); } return months; } export default function AvailabilityCalendar({ blockedDates, months = 2, disabled = false, }: { blockedDates: string[]; months?: number; disabled?: boolean; }) { const { t } = useI18n(); const blockedSet = useMemo(() => new Set(blockedDates), [blockedDates]); const monthViews = useMemo(() => buildMonths(months, blockedSet), [months, blockedSet]); return (
{t('availabilityTitle')} {t('availabilityLegendBooked')}
{monthViews.map((month) => (
{month.label}
{['S', 'M', 'T', 'W', 'T', 'F', 'S'].map((d) => (
{d}
))} {month.days.map((day, idx) => (
{day.label}
))}
))}
); }