const rtf = new Intl.RelativeTimeFormat('de-DE', { numeric: 'auto' })

const timeFormat = Intl.DateTimeFormat('de-DE', {
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false,
})

const dateTimeFormat = Intl.DateTimeFormat('de-DE', {
  year: 'numeric',
  month: '2-digit',
  day: '2-digit',
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  hour12: false,
})

export const getRelativeOrAbsoluteDate = (date: Date): string => {
  const now = new Date()
  const diffInMs = date.getTime() - now.getTime()
  const diffInDays = Math.floor(diffInMs / (1000 * 60 * 60 * 24))

  if (diffInDays >= -7 && diffInDays <= 2) {
    return `${rtf.format(diffInDays, 'day')}, ${timeFormat.format(date)}`
  }

  return dateTimeFormat.format(date)
};