export default function formatDateTime(date: Date | string | number): string {
  const properDate = new Date(date);

  const defaultOptions = {
    locale: 'en-US',
    weekday: 'short',
    year: 'numeric',
    month: 'long',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: true,
  };

  let formattingOptions = defaultOptions;
  const savedOptions = localStorage.getItem('dateTimeFormatOptions');

  if (savedOptions)
    try {
      formattingOptions = { ...defaultOptions, ...JSON.parse(savedOptions) };
    } catch (e) {
      console.error('Failed to parse date format options from localStorage', e);
    }

  return properDate.toLocaleString(formattingOptions.locale, {
    weekday: formattingOptions.weekday === '' ? undefined : formattingOptions.weekday,
    year: formattingOptions.year === '' ? undefined : formattingOptions.year,
    month: formattingOptions.month === '' ? undefined : formattingOptions.month,
    day: formattingOptions.day === '' ? undefined : formattingOptions.day,
    hour: formattingOptions.hour === '' ? undefined : formattingOptions.hour,
    minute: formattingOptions.minute === '' ? undefined : formattingOptions.minute,
    second: formattingOptions.second === '' ? undefined : formattingOptions.second,
    hour12: formattingOptions.hour12,
  } as unknown as Intl.DateTimeFormatOptions);
}
