/* ============================================================
   VYNDR — NEWS WIRE formatting (Wave 2B, Offseason Hub).

   Small pure helpers for the real-ESPN news wire. Timestamps are rendered in
   mono (the brand rule: all data is mono); an unparseable/absent published
   time yields '' (absent beats a fabricated "just now"). Type chips map the
   raw ESPN feed type to a short human label without inventing categories.

   CommonJS so the .tsx surface imports it (allowJs) AND Jest exercises it.
   ============================================================ */

/**
 * timeAgo(published, now) — compact relative time for a mono timestamp.
 * Invalid / missing input → '' (never a fabricated stamp). `published` is an
 * ISO string or epoch ms; `now` defaults to Date.now().
 */
function timeAgo(published, now = Date.now()) {
  if (published == null || published === '') return '';
  const t = typeof published === 'number' ? published : Date.parse(String(published));
  if (!Number.isFinite(t)) return '';
  const diff = Number(now) - t;
  if (!Number.isFinite(diff)) return '';
  if (diff < 0) return 'now';
  const min = Math.floor(diff / 60000);
  if (min < 1) return 'now';
  if (min < 60) return `${min}m ago`;
  const hr = Math.floor(min / 60);
  if (hr < 24) return `${hr}h ago`;
  const day = Math.floor(hr / 24);
  return `${day}d ago`;
}

// Raw ESPN feed types → short display labels. Unknown types fall back to a
// title-cased version of the raw string (never dropped, never invented).
const TYPE_LABEL = {
  Recap: 'RECAP',
  HeadlineNews: 'NEWS',
  Story: 'STORY',
  Preview: 'PREVIEW',
  Notebook: 'NOTEBOOK',
  Media: 'MEDIA',
};

/** typeLabel(type) — short uppercase chip label, or '' when absent. */
function typeLabel(type) {
  if (!type) return '';
  const raw = String(type);
  if (TYPE_LABEL[raw]) return TYPE_LABEL[raw];
  // split camelCase / snake and uppercase
  return raw.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/[_-]+/g, ' ').trim().toUpperCase();
}

module.exports = { timeAgo, typeLabel, TYPE_LABEL };
