"use client"; import { useState, useCallback } from "react"; import { useTranslation } from "react-i18next"; import { ChevronRight, ChevronLeft } from "lucide-react"; const MONTH_KEYS = [ "month.january", "month.february", "month.march", "month.april", "month.may", "month.june", "month.july", "month.august", "month.september", "month.october", "month.november", "month.december", ]; const DAY_KEYS = [ "dayAbbr.sun", "dayAbbr.mon", "dayAbbr.tue", "dayAbbr.wed", "dayAbbr.thu", "dayAbbr.fri", "dayAbbr.sat", ]; export default function DateSelectionCalendar({ mode, availableDates, selectedStart, selectedEnd, onSelectStart, onSelectEnd, }) { const { t } = useTranslation(); const [calendarMonth, setCalendarMonth] = useState(() => new Date().getMonth()); const [calendarYear, setCalendarYear] = useState(() => new Date().getFullYear()); const [rangeError, setRangeError] = useState(null); const isPastDate = useCallback((dateStr) => { const today = new Date(); today.setHours(0, 0, 0, 0); return new Date(dateStr) < today; }, []); const isDateAvailable = useCallback( (dateStr) => (availableDates ? availableDates.has(dateStr) : true), [availableDates] ); const isMonthFullyAvailable = useCallback( (year, monthIdx) => { const daysInMonth = new Date(year, monthIdx + 1, 0).getDate(); for (let day = 1; day <= daysInMonth; day++) { const dateStr = `${year}-${String(monthIdx + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`; if (isPastDate(dateStr) || !isDateAvailable(dateStr)) return false; } return true; }, [isPastDate, isDateAvailable] ); const isPastMonth = useCallback((year, monthIdx) => { const now = new Date(); return ( year < now.getFullYear() || (year === now.getFullYear() && monthIdx < now.getMonth()) ); }, []); const handleDayClick = (dateStr) => { setRangeError(null); if (!selectedStart || selectedEnd) { onSelectStart(dateStr); onSelectEnd(null); return; } if (new Date(dateStr) <= new Date(selectedStart)) { onSelectStart(dateStr); onSelectEnd(null); return; } const start = new Date(selectedStart); const end = new Date(dateStr); for (let d = new Date(start); d <= end; d.setDate(d.getDate() + 1)) { const ds = d.toISOString().split("T")[0]; if (!isDateAvailable(ds)) { setRangeError(t("unavailableInRange", { defaultValue: "توجد أيام غير متاحة ضمن الفترة المحددة" })); return; } } onSelectEnd(dateStr); }; const handleMonthClick = (year, monthIdx) => { setRangeError(null); const monthStr = `${year}-${String(monthIdx + 1).padStart(2, "0")}`; const firstDay = `${monthStr}-01`; if (!selectedStart || selectedEnd || monthStr <= (selectedStart || "").substring(0, 7)) { onSelectStart(firstDay); onSelectEnd(null); } else { const lastDay = new Date(year, monthIdx + 1, 0).getDate(); onSelectEnd(`${monthStr}-${String(lastDay).padStart(2, "0")}`); } }; const navigateMonth = (delta) => { let month = calendarMonth + delta; let year = calendarYear; if (month < 0) { month = 11; year--; } if (month > 11) { month = 0; year++; } setCalendarMonth(month); setCalendarYear(year); }; const isEntryStep = !selectedStart || !!selectedEnd; return (