import type { DashboardPeriod } from "../shared/types"; /** * Compute a date range (dateFrom / dateTo) based on the selected period. * Shared between useDashboard, useReports, DashboardPage and ReportsPage. */ export function computeDateRange( period: DashboardPeriod, customDateFrom?: string, customDateTo?: string, ): { dateFrom?: string; dateTo?: string } { if (period === "all") return {}; if (period === "custom" && customDateFrom && customDateTo) { return { dateFrom: customDateFrom, dateTo: customDateTo }; } const now = new Date(); const year = now.getFullYear(); const month = now.getMonth(); const day = now.getDate(); const dateTo = `${year}-${String(month + 1).padStart(2, "0")}-${String(day).padStart(2, "0")}`; let from: Date; switch (period) { case "month": from = new Date(year, month, 1); break; case "3months": from = new Date(year, month - 2, 1); break; case "6months": from = new Date(year, month - 5, 1); break; case "year": from = new Date(year, 0, 1); break; case "12months": from = new Date(year, month - 11, 1); break; default: from = new Date(year, month, 1); break; } const dateFrom = `${from.getFullYear()}-${String(from.getMonth() + 1).padStart(2, "0")}-${String(from.getDate()).padStart(2, "0")}`; return { dateFrom, dateTo }; } /** * Build an array of month options for the budget month dropdown. * Returns the last 24 months with localized labels. */ export function buildMonthOptions(language: string): Array<{ key: string; value: string; label: string }> { const now = new Date(); const currentMonth = now.getMonth(); const currentYear = now.getFullYear(); return Array.from({ length: 24 }, (_, i) => { const d = new Date(currentYear, currentMonth - i, 1); const y = d.getFullYear(); const m = d.getMonth() + 1; const label = new Intl.DateTimeFormat(language, { month: "long", year: "numeric" }).format(d); return { key: `${y}-${m}`, value: `${y}-${m}`, label: label.charAt(0).toUpperCase() + label.slice(1) }; }); }