// ------------------------------------------------------------------------
//  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 <date>. <detail>"
//    On peak -1d : "peaks tonight - <detail>"
//    After peak  : "is past its peak (<date>) but still possibly visible - <detail>"
//
//  Single-day events (start === end) keep their static message unchanged.
// ------------------------------------------------------------------------

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);
  if (diffDays < -1) {
    return ev.title + ' is active and building — peak on ' + peakFmt + '. ' + baseCapd;
  } else if (diffDays <= 1) {
    return ev.title + ' peaks tonight — ' + base;
  } else {
    return ev.title + ' is past its peak (' + peakFmt + ') but still possibly visible — ' + base;
  }
}
