// ------------------------------------------------------------------------ // dynamic-message.js - Rewrites a cosmic event's message based on where // today sits vs. the peak. // // Before peak : "is active and building - peak on . (~NN% chance...)" // On peak -1d : "peaks tonight - (~NN% chance...)" // After peak : "is past its peak () but still possibly visible - (~NN% chance...)" // // Single-day events (start === end) keep their static message unchanged. // `ev.chance` (see ../events/activity-profile.js), when present, is // appended as an estimated viewing-chance figure. // ------------------------------------------------------------------------ export function dynamicCosmicMessage(ev, dateStr) { if (!ev.peak || ev.start === ev.end) return ev.message; var today = new Date(dateStr + 'T00:00Z'); var peak = new Date(ev.peak + 'T00:00Z'); var diffDays = Math.round((today - peak) / 86400000); var peakFmt = peak.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', timeZone: 'UTC' }); var base = ev.message .replace(/^.*?(?:peaks tonight|peak tonight|is active tonight|is active|peaks)\s*—\s*/i, '') .trim(); var baseCapd = base.charAt(0).toUpperCase() + base.slice(1); var chanceSuffix = typeof ev.chance === 'number' ? ' (~' + ev.chance + '% chance of a good show tonight)' : ''; if (diffDays < -1) { return ev.title + ' is active and building — peak on ' + peakFmt + '. ' + baseCapd + chanceSuffix; } else if (diffDays <= 1) { return ev.title + ' peaks tonight — ' + base + chanceSuffix; } else { return ev.title + ' is past its peak (' + peakFmt + ') but still possibly visible — ' + base + chanceSuffix; } }