// Inline helper module for timezone- and locale-aware date/time labels.
// Included verbatim by templates; exposes `window.TimeFmt`.
//
// Heute/Gestern is decided in UTC (same as server-side grouping) to keep
// client and server in lockstep. Locale date format follows the browser.
(() => {
  const utcDayISO = (d) => d.toISOString().slice(0, 10);
  const todayISO = () => utcDayISO(new Date());
  const yesterdayISO = () => {
    const y = new Date();
    y.setUTCDate(y.getUTCDate() - 1);
    return utcDayISO(y);
  };
  const dateLabelFromISO = (iso) => {
    if (iso === todayISO()) return 'Heute';
    if (iso === yesterdayISO()) return 'Gestern';
    const parts = iso.split('-');
    if (parts.length !== 3) return iso;
    return new Date(Date.UTC(+parts[0], +parts[1] - 1, +parts[2]))
      .toLocaleDateString();
  };
  const dateLabel = (d) => dateLabelFromISO(utcDayISO(d));
  const timeLabel = (d) => d.toLocaleTimeString(undefined, {
    hour: '2-digit', minute: '2-digit', hour12: false,
  });
  window.TimeFmt = { dateLabelFromISO, dateLabel, timeLabel };
})();
