const DAYS = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']; const DAY_LABELS = { sun: 'Domingo', mon: 'Lunes', tue: 'Martes', wed: 'Miércoles', thu: 'Jueves', fri: 'Viernes', sat: 'Sábado', }; export const DAY_KEYS = DAYS; export const DAY_LABEL = DAY_LABELS; function parseHM(str) { const [h, m] = str.split(':').map(Number); if (!Number.isFinite(h) || !Number.isFinite(m)) return null; return h * 60 + m; } function parseHours(raw) { if (!raw) return null; try { return typeof raw === 'string' ? JSON.parse(raw) : raw; } catch { return null; } } function findNextOpen(hours, now) { for (let offset = 1; offset <= 7; offset++) { const day = DAYS[(now.getDay() + offset) % 7]; const range = hours[day]; if (Array.isArray(range) && range.length === 2) { const openStr = range[0]; if (offset === 1) return `mañana a las ${openStr}`; return `${DAY_LABELS[day]} a las ${openStr}`; } } return null; } export function getOpenStatus(rawHours, now = new Date()) { const hours = parseHours(rawHours); if (!hours) return null; const day = DAYS[now.getDay()]; const range = hours[day]; if (!Array.isArray(range) || range.length !== 2) { const next = findNextOpen(hours, now); return { status: 'closed', label: next ? `Cerrado · Abre ${next}` : 'Cerrado' }; } const openMins = parseHM(range[0]); const closeMins = parseHM(range[1]); if (openMins == null || closeMins == null) return null; const nowMins = now.getHours() * 60 + now.getMinutes(); if (nowMins < openMins) { return { status: 'closed', label: `Cerrado · Abre a las ${range[0]}` }; } if (nowMins >= closeMins) { const next = findNextOpen(hours, now); return { status: 'closed', label: next ? `Cerrado · Abre ${next}` : 'Cerrado' }; } return { status: 'open', label: `Abierto · Cierra a las ${range[1]}` }; } export function emptyHours() { return { sun: null, mon: null, tue: null, wed: null, thu: null, fri: null, sat: null }; }