import { marked } from 'marked';

// Configured once: no GFM tables (not needed), breaks = true for newlines
marked.setOptions({ breaks: true, gfm: true });

/** Render markdown-lite to HTML. Input is from trusted authenticated users. */
export function renderMd(md: string): string {
  return marked.parse(md) as string;
}

/** Format a UTC ISO date string for display in Europe/Copenhagen. */
export function fmtDate(iso: string, opts: Intl.DateTimeFormatOptions = {
  day: 'numeric', month: 'long', year: 'numeric',
}): string {
  return new Intl.DateTimeFormat('da-DK', { timeZone: 'Europe/Copenhagen', ...opts }).format(new Date(iso));
}

export function fmtDateTime(iso: string): string {
  return fmtDate(iso, {
    day: 'numeric', month: 'long', year: 'numeric',
    hour: '2-digit', minute: '2-digit',
  });
}

/** True if the ISO timestamp is within `minutes` minutes of now. */
export function withinMinutes(iso: string, minutes: number): boolean {
  return (Date.now() - new Date(iso).getTime()) < minutes * 60_000;
}
