import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { getDateDaysDiff } from './dateDiffer';
import { Tooltip } from '../tooltip/Tooltip.component';
export const DATE_FORMATER = Intl.DateTimeFormat('fr-CA', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour12: false,
});
export const TIME_SECOND_FORMATER = Intl.DateTimeFormat('en-GB', {
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
});
export const TIME_FORMATER = Intl.DateTimeFormat('en-GB', {
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
});
const isItFutureOrIsItPast = (timeDiff, stringToBeFormatted) => {
    if (timeDiff > 0) {
        return `${stringToBeFormatted} ago`;
    }
    else {
        return `in ${stringToBeFormatted}`;
    }
};
export const FormattedDateTime = ({ format, value, }) => {
    switch (format) {
        case 'date':
            return _jsx(_Fragment, { children: DATE_FORMATER.format(value) });
        case 'date-time':
            return (_jsx(_Fragment, { children: DATE_FORMATER.format(value) + ' ' + TIME_FORMATER.format(value) }));
        case 'date-time-second':
            return (_jsx(_Fragment, { children: DATE_FORMATER.format(value) +
                    ' ' +
                    TIME_SECOND_FORMATER.format(value) }));
        case 'time':
            return _jsx(_Fragment, { children: TIME_FORMATER.format(value) });
        case 'time-second':
            return _jsx(_Fragment, { children: TIME_SECOND_FORMATER.format(value) });
        case 'relative':
            const now = new Date();
            const monthDiff = getDateDaysDiff(value, now, 'months');
            const dayDiff = getDateDaysDiff(value, now, 'days');
            if (monthDiff !== 0 && Math.abs(dayDiff) > 90) {
                return (_jsx(Tooltip, { overlay: _jsx(FormattedDateTime, { format: "date-time-second", value: value }), children: isItFutureOrIsItPast(monthDiff, `${Math.abs(monthDiff)} month${Math.abs(monthDiff) > 1 ? 's' : ''}`) }));
            }
            if (dayDiff !== 0) {
                return (_jsx(Tooltip, { overlay: _jsx(FormattedDateTime, { format: "date-time-second", value: value }), children: isItFutureOrIsItPast(dayDiff, `${Math.abs(dayDiff)} day${Math.abs(dayDiff) > 1 ? 's' : ''}`) }));
            }
            const hourDiff = getDateDaysDiff(value, now, 'hours');
            if (hourDiff !== 0) {
                return (_jsx(Tooltip, { overlay: _jsx(FormattedDateTime, { format: "date-time-second", value: value }), children: isItFutureOrIsItPast(hourDiff, `${Math.abs(hourDiff)} hour${Math.abs(hourDiff) > 1 ? 's' : ''}`) }));
            }
            const minuteDiff = getDateDaysDiff(value, now, 'minutes');
            if (minuteDiff !== 0) {
                return (_jsx(Tooltip, { overlay: _jsx(FormattedDateTime, { format: "date-time-second", value: value }), children: isItFutureOrIsItPast(minuteDiff, `${Math.abs(minuteDiff)} minute${Math.abs(minuteDiff) > 1 ? 's' : ''}`) }));
            }
            return (_jsx(Tooltip, { overlay: _jsx(FormattedDateTime, { format: "date-time-second", value: value }), children: "few seconds ago" }));
        //TO FINISH
        default:
            return _jsx(_Fragment, {});
    }
};
