/**
 * Converts a Unix timestamp to a localized date string
 * @param timestamp - Unix timestamp in seconds
 * @returns Localized date string
 */
export function unixTimestampToLocalizedDate(timestamp: number): string {
  const date = new Date(timestamp * 1000)
  return date.toLocaleString(navigator.language)
}

/**
 * Creates a relative time formatter
 * @returns Functions for formatting relative time
 */
export function useRelativeTime() {
  const rtf = new Intl.RelativeTimeFormat('en', {
    localeMatcher: 'best fit',
    numeric: 'always',
    style: 'long',
  })

  /**
   * Formats seconds ago into a human-readable relative time string
   * @param seconds - Number of seconds ago (positive = past)
   * @returns Formatted relative time string
   */
  const formatTimeAgo = (seconds: number): string => {
    if (seconds >= 3600) {
      return rtf.format(-Math.floor(seconds / 3600), 'hours')
    } else if (seconds >= 60) {
      return rtf.format(-Math.floor(seconds / 60), 'minutes')
    } else {
      return rtf.format(-seconds, 'seconds')
    }
  }

  return {
    formatTimeAgo,
  }
}
