/**
 * Formatting utility functions for amounts and currencies
 */

/**
 * Format a number with thousand separators
 * @param value - The number to format
 * @param locale - The locale to use for formatting (default: 'en-US')
 * @returns Formatted string with thousand separators
 */
export function formatNumber(value: number, locale: string = 'en-US'): string {
  return new Intl.NumberFormat(locale).format(value)
}

/**
 * Format satoshi amounts with thousand separators
 * @param sats - The satoshi amount
 * @param locale - The locale to use for formatting (default: 'en-US')
 * @returns Formatted string with "sats" suffix
 */
export function formatSats(sats: number, locale: string = 'en-US'): string {
  return `${formatNumber(sats, locale)} sats`
}

/**
 * Format millisatoshi amounts with thousand separators
 * @param msats - The millisatoshi amount
 * @param locale - The locale to use for formatting (default: 'en-US')
 * @returns Formatted string with "sats" suffix
 */
export function formatMsats(msats: number, locale: string = 'en-US'): string {
  const sats = Math.floor(msats / 1000)
  return formatSats(sats, locale)
}

/**
 * Format currency amounts with thousand separators
 * @param amount - The amount to format
 * @param currency - The currency code (e.g., 'USD', 'EUR')
 * @param locale - The locale to use for formatting (default: 'en-US')
 * @returns Formatted currency string
 */
export function formatCurrency(amount: number, currency: string, locale: string = 'en-US'): string {
  try {
    return new Intl.NumberFormat(locale, {
      style: 'currency',
      currency: currency.toUpperCase()
    }).format(amount)
  } catch (error) {
    // Fallback to basic formatting if currency is not supported
    return `${formatNumber(amount, locale)} ${currency}`
  }
}

/**
 * Format event ticket price with appropriate formatting
 * @param price - The price amount
 * @param currency - The currency code
 * @param locale - The locale to use for formatting (default: 'en-US')
 * @returns Formatted price string
 */
export function formatEventPrice(price: number, currency: string, locale: string = 'en-US'): string {
  if (currency.toLowerCase() === 'sats' || currency.toLowerCase() === 'sat') {
    return formatSats(price, locale)
  }
  return formatCurrency(price, currency, locale)
}

/**
 * Format wallet balance with appropriate formatting
 * @param balanceMsat - The balance in millisatoshi
 * @param locale - The locale to use for formatting (default: 'en-US')
 * @returns Formatted balance string
 */
export function formatWalletBalance(balanceMsat: number, locale: string = 'en-US'): string {
  if (balanceMsat === 0) {
    return '0 sats'
  }
  return formatMsats(balanceMsat, locale)
}

