import { useI18n } from 'vue-i18n';

export default function formatDate(dateVal) {
    const { t } = useI18n();
    const today = new Date();
    if (dateVal == null) return '';

    const date = new Date(dateVal);
    const dateZeroTime = new Date(dateVal);
    dateZeroTime.setHours(0, 0, 0, 0);
    const diff = Math.trunc(
        (today.getTime() - dateZeroTime.getTime()) / (1000 * 3600 * 24)
    );
    let format;
    if (diff === 0) format = t('globals.today');
    else if (diff === 1) format = t('globals.yesterday');
    else if (diff > 1 && diff < 7) {
        const options = { weekday: 'short' };
        format = date.toLocaleDateString(t('globals.dateFormat'), options);
    } else if (today.getFullYear() === date.getFullYear()) {
        const options = { day: 'numeric', month: 'short' };
        format = date.toLocaleDateString(t('globals.dateFormat'), options);
    } else {
        const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
        format = date.toLocaleDateString(t('globals.dateFormat'), options);
    }

    // Formatear la hora en HH:mm
    const hours = date.getHours().toString().padStart(2, '0');
    const minutes = date.getMinutes().toString().padStart(2, '0');
    return `${format} ${hours}:${minutes}`;
}
