import { addSeconds, format, isValid, parse, parseISO, } from 'date-fns';
import { format as formatTz } from 'date-fns-tz';
const millis = () => new Date().getTime();
const formatDateTime = (isoDateStr, outputFormat) => {
    if (!isoDateStr)
        return '';
    try {
        const parsedDate = parseISO(isoDateStr);
        return formatTz(parsedDate, outputFormat);
    }
    catch (error) {
        console.error('Invalid date or format:', error);
        return '';
    }
};
const formatDate = (date, fmt) => {
    if (!date)
        return '';
    return formatTz(date, fmt);
};
const parseISOString = (s) => parseISO(s);
const isValidDate = (date) => isValid(date);
const parseDate = (dateStr, formatStr, referenceDate) => parse(dateStr, formatStr, referenceDate ?? new Date());
const addSecondsToDate = (date, seconds) => addSeconds(date, seconds);
const parseTime = (time) => {
    if (time == null || time <= 0)
        return '00:00:00.000';
    return new Date(time * 1000).toISOString().slice(11, 23);
};
function extractDateFromMongoId(id, time) {
    const DATE_PATTERN = 'yyyy-MM-dd';
    const TIME_PATTERN = 'HH:mm:ss';
    const hexTimestamp = id.substring(0, 8);
    const timestampInMilliseconds = parseInt(hexTimestamp, 16) * 1000;
    const dtf = time ? `${DATE_PATTERN} ${TIME_PATTERN}` : DATE_PATTERN;
    const date = addSeconds(new Date(0), timestampInMilliseconds / 1000);
    const formattedDate = formatTz(date, dtf);
    return formattedDate;
}
const DateUtils = {
    millis,
    formatDate,
    format,
    parseISOString,
    isValidDate,
    parseDate,
    addSecondsToDate,
    parseTime,
    extractDateFromMongoId,
    formatDateTime,
};
export default DateUtils;
