/**
 * Calculate estimated reading time for an article
 * Based on average reading speed of 200-250 words per minute
 */

const WORDS_PER_MINUTE = 225; // Average reading speed
const WORDS_PER_IMAGE = 12; // Time spent looking at an image (in words equivalent)

export function calculateReadTime(content: string): number {
  if (!content) return 1;

  // Strip HTML tags
  const text = content.replace(/<[^>]*>/g, ' ');
  
  // Count words
  const words = text.trim().split(/\s+/).filter(w => w.length > 0).length;
  
  // Count images
  const imageMatches = content.match(/<img/gi);
  const imageCount = imageMatches ? imageMatches.length : 0;
  
  // Calculate total "word equivalent"
  const totalWords = words + (imageCount * WORDS_PER_IMAGE);
  
  // Calculate minutes (round up)
  const minutes = Math.ceil(totalWords / WORDS_PER_MINUTE);
  
  // Minimum 1 minute
  return Math.max(1, minutes);
}

export function formatReadTime(minutes: number): string {
  if (minutes === 1) return '1 minuta čtení';
  if (minutes < 5) return `${minutes} minuty čtení`;
  return `${minutes} minut čtení`;
}

export function calculateReadTimeFromHTML(html: string): { minutes: number; formatted: string } {
  const minutes = calculateReadTime(html);
  return {
    minutes,
    formatted: formatReadTime(minutes),
  };
}
