import { format } from 'date-fns';

type DateFormatOptions = {
  [key: number]: string;
};

const dateFormatOptions: DateFormatOptions = {
  0: 'dd/MM/yyyy', // 04/12/2024
  1: 'dd/MM', // 04/12
  2: 'dd/MM/yyyy HH:mm ', // 04/12/2024 10:00
  3: 'yyyy-MM-dd HH:mm', // 2024-12-04 10:00
  4: 'MM-dd-yyyy HH:mm', // 12-04-2024 10:00
  5: 'EEEE, MMMM do yyyy HH:mm', // Wednesday, December 4th 2024 10:00
};

export const formatDate = (
  date: Date | null | undefined,
  formatOption?: number,
): string => {
  if (date == null) {
    return '-';
  }

  const formatString = dateFormatOptions[formatOption ?? 0];
  if (!formatString) {
    throw new Error(`Invalid format option: ${formatOption}`);
  }

  return format(date, formatString);
};
