/** * @file Regroupe les helpers purs de draft et de sélection du calendrier. */ import { CALENDAR_ALERT_OFFSETS, CALENDAR_MONTHLY_REPEAT_BY, WEEK_SLOT_MINUTES, formatLocalDateKey, getLocalDayIndex, minutesToTimeValue, parseLocalDateKey, timeValueToMinutes } from "./calendarUtils.js"; import { WEEK_SLOT_COUNT } from "./calendarLayout.js"; export function createDefaultDraft(date = new Date()) { const dayIndex = getLocalDayIndex(date); const dateKey = formatLocalDateKey(date); const startMinutes = Math.min(23 * 60 + 30, Math.ceil((date.getHours() * 60 + date.getMinutes()) / WEEK_SLOT_MINUTES) * WEEK_SLOT_MINUTES); return { id: "", title: "", dayIndex, endDayIndex: dayIndex, startDate: dateKey, endDate: dateKey, monthlyRepeatBy: "monthDay", repeat: "monthly", startTime: minutesToTimeValue(startMinutes), endTime: minutesToTimeValue(Math.min(23 * 60 + 59, startMinutes + WEEK_SLOT_MINUTES)), repeatable: true, alertMode: "off", alertOffsetMinutes: [], color: "violet" }; } export function getDraftSelection(draft) { const dayIndex = Number(draft.dayIndex); const startMinutes = timeValueToMinutes(draft.startTime); const endMinutes = draft.endTime ? timeValueToMinutes(draft.endTime) : startMinutes + WEEK_SLOT_MINUTES; if (dayIndex < 0 || dayIndex > 6 || startMinutes < 0 || endMinutes <= startMinutes) return null; const startSlot = Math.floor(startMinutes / WEEK_SLOT_MINUTES); const endSlot = Math.max(startSlot, Math.ceil(endMinutes / WEEK_SLOT_MINUTES) - 1); return { dayIndex, startSlot: Math.max(0, Math.min(WEEK_SLOT_COUNT - 1, startSlot)), endSlot: Math.max(0, Math.min(WEEK_SLOT_COUNT - 1, endSlot)) }; } export function getCalendarRepeatMode() { return "weekly"; } export function getDateSpanDays(startDateKey, endDateKey) { const start = parseLocalDateKey(startDateKey); const end = parseLocalDateKey(endDateKey); if (!start || !end || end < start) return 0; return Math.round((end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000)); } export function normalizeDraftDateKey(value) { const date = parseLocalDateKey(value); return date ? formatLocalDateKey(date) : ""; } export function normalizeDraftAlertOffsets(value) { const rawOffsets = Array.isArray(value) ? value : [value]; const offsets = [...new Set(rawOffsets.map(Number).filter((offset) => CALENDAR_ALERT_OFFSETS.includes(offset)))]; return offsets.sort((a, b) => b - a); } export function normalizeDraftMonthlyRepeatBy(value) { return CALENDAR_MONTHLY_REPEAT_BY.includes(value) ? value : "monthDay"; }