/**
 * Aides d'affichage des articles du Mag : temps de lecture estimé
 * et extrait texte à partir du HTML de l'article.
 */

export function readingTimeMinutes(htmlBody: string | null | undefined): number {
  const words = (htmlBody ?? '')
    .replace(/<[^>]*>/g, ' ')
    .split(/\s+/)
    .filter(Boolean).length;
  return Math.max(1, Math.round(words / 200));
}

export function articleExcerpt(htmlBody: string | null | undefined, maxLength = 180): string {
  const text = (htmlBody ?? '')
    .replace(/<[^>]*>/g, ' ')
    .replace(/&nbsp;/g, ' ')
    .replace(/\s+/g, ' ')
    .trim();
  if (text.length <= maxLength) return text;
  return `${text.slice(0, maxLength).replace(/\s+\S*$/, '')}…`;
}
