/**
 * Formatting utility functions
 */

/**
 * Format phone number for display
 */
export function formatPhone(phone: string): string {
  const cleanPhone = phone.replace(/[\s-]/g, '');

  // Chinese phone: 138 1234 5678
  if (cleanPhone.length === 11) {
    return `${cleanPhone.slice(0, 3)} ${cleanPhone.slice(3, 7)} ${cleanPhone.slice(7)}`;
  }

  // HK phone: 1234 5678
  if (cleanPhone.length === 8) {
    return `${cleanPhone.slice(0, 4)} ${cleanPhone.slice(4)}`;
  }

  return phone;
}

/**
 * Mask phone number for privacy
 * 13812345678 -> 138****5678
 */
export function maskPhone(phone: string): string {
  const cleanPhone = phone.replace(/[\s-]/g, '');

  if (cleanPhone.length === 11) {
    return `${cleanPhone.slice(0, 3)}****${cleanPhone.slice(7)}`;
  }

  if (cleanPhone.length === 8) {
    return `${cleanPhone.slice(0, 2)}****${cleanPhone.slice(6)}`;
  }

  return phone;
}

/**
 * Format currency amount
 */
export function formatCurrency(
  amount: number,
  currency: string = 'HKD',
  locale: string = 'zh-HK',
): string {
  return new Intl.NumberFormat(locale, {
    style: 'currency',
    currency,
    minimumFractionDigits: 0,
    maximumFractionDigits: 2,
  }).format(amount);
}

/**
 * Format date for display
 */
export function formatDate(
  date: Date | string,
  options: Intl.DateTimeFormatOptions = {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
  },
  locale: string = 'zh-CN',
): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  return new Intl.DateTimeFormat(locale, options).format(d);
}

/**
 * Format datetime for display
 */
export function formatDateTime(
  date: Date | string,
  locale: string = 'zh-CN',
): string {
  return formatDate(date, {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  }, locale);
}

/**
 * Format relative time (e.g., "3 minutes ago")
 */
export function formatRelativeTime(date: Date | string, locale: string = 'zh-CN'): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  const now = new Date();
  const diffMs = now.getTime() - d.getTime();
  const diffSeconds = Math.floor(diffMs / 1000);
  const diffMinutes = Math.floor(diffSeconds / 60);
  const diffHours = Math.floor(diffMinutes / 60);
  const diffDays = Math.floor(diffHours / 24);

  const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });

  if (diffSeconds < 60) {
    return rtf.format(-diffSeconds, 'second');
  } else if (diffMinutes < 60) {
    return rtf.format(-diffMinutes, 'minute');
  } else if (diffHours < 24) {
    return rtf.format(-diffHours, 'hour');
  } else if (diffDays < 30) {
    return rtf.format(-diffDays, 'day');
  } else {
    return formatDate(d, { year: 'numeric', month: 'short', day: 'numeric' }, locale);
  }
}

/**
 * Truncate text with ellipsis
 */
export function truncateText(text: string, maxLength: number, suffix: string = '...'): string {
  if (text.length <= maxLength) {
    return text;
  }
  return text.slice(0, maxLength - suffix.length) + suffix;
}

/**
 * Generate initials from name
 */
export function getInitials(name: string, maxChars: number = 2): string {
  const words = name.trim().split(/\s+/);
  const initials = words.map(word => word.charAt(0).toUpperCase()).join('');
  return initials.slice(0, maxChars);
}

/**
 * Format file size
 */
export function formatFileSize(bytes: number): string {
  const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  let unitIndex = 0;
  let size = bytes;

  while (size >= 1024 && unitIndex < units.length - 1) {
    size /= 1024;
    unitIndex++;
  }

  return `${size.toFixed(1)} ${units[unitIndex]}`;
}
