import {useMemo} from 'react';
import {useTranslation} from 'react-i18next';
import {
  getCurrentLocale,
  getDifferenceInDays,
  getFormattedDate,
  getTimeFromUlid,
  isToday,
  isYesterday,
} from '@shared/utils';

export const useHumanizedDate = (ulid?: string) => {
  const {t} = useTranslation();

  const humanizedDate = useMemo(() => {
    if (!ulid) {
      return '';
    }

    const date = new Date(getTimeFromUlid(ulid));

    if (isToday(date)) {
      return getFormattedDate(date, 'HH:mm');
    } else if (isYesterday(date)) {
      return t('general.yesterday');
    } else if (getDifferenceInDays(date) < 7) {
      return getFormattedDate(date, 'EEEE', {
        locale: getCurrentLocale(),
      });
    } else {
      return getFormattedDate(date, 'dd/MM/yy');
    }
  }, [ulid, t]);

  return humanizedDate;
};
