"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCurrentYear = exports.utcDateToLocale = exports.sameDate = exports.sameYear = exports.sameYearSameMonth = exports.generateDateByUTCValue = exports.getDateUTCValue = void 0;
exports.isValidDate = isValidDate;
exports.formatIsoDate = formatIsoDate;
exports.parseIsoDate = parseIsoDate;
exports.validateDateFormat = validateDateFormat;
/** Checks if the provided value is a valid date */
function isValidDate(value) {
    return `${new Date(value)}` !== 'Invalid Date';
}
/** Formats the date to ISO format: 'yyyy-mm-dd' */
function formatIsoDate(date) {
    const d = new Date(date);
    if (!isValidDate(d))
        return undefined;
    const yyyy = d === null || d === void 0 ? void 0 : d.getFullYear();
    const mm = (d.getMonth() + 1).toString().padStart(2, '0');
    const dd = d.getDate().toString().padStart(2, '0');
    return `${yyyy}-${mm}-${dd}`;
}
/**
 * Parses string date with the iso format of YYYY-mm-dd
 * @param input string
 * @returns Date | undefined
 */
function parseIsoDate(input) {
    if (!input)
        return;
    const ticks = Date.parse(input);
    return isNaN(ticks) ? undefined : new Date(ticks);
}
/**
 * Validation for 'YYYY-MM-DD' format
 */
function validateDateFormat(dateString) {
    const regEx = /^\d{4}-\d{2}-\d{2}$/;
    return dateString.match(regEx);
}
// Get current time's UTC year, month, date
const getDateUTCValue = (date) => ({
    utcYear: date.getUTCFullYear(),
    utcMonth: date.getUTCMonth(),
    utcDate: date.getUTCDate(),
});
exports.getDateUTCValue = getDateUTCValue;
// Generate a date by set its utc value
const generateDateByUTCValue = (year, month, day) => {
    return day ? new Date(Date.UTC(year, month, day)) : new Date(Date.UTC(year, month));
};
exports.generateDateByUTCValue = generateDateByUTCValue;
const sameYearSameMonth = (a, b) => {
    if (!a || !b)
        return false;
    return a.getUTCFullYear() === b.getUTCFullYear() && a.getUTCMonth() === b.getUTCMonth();
};
exports.sameYearSameMonth = sameYearSameMonth;
const sameYear = (a, b) => {
    if (!a || !b)
        return false;
    return a.getUTCFullYear() === b.getUTCFullYear() ? true : false;
};
exports.sameYear = sameYear;
const sameDate = (a, b) => {
    if (!a || !b)
        return false;
    return (a.getUTCFullYear() === b.getUTCFullYear() &&
        a.getUTCMonth() === b.getUTCMonth() &&
        a.getUTCDate() === b.getUTCDate());
};
exports.sameDate = sameDate;
const utcDateToLocale = (utcDate) => {
    if (!utcDate)
        return undefined;
    const localDateSameDay = new Date(utcDate.getUTCFullYear(), utcDate.getUTCMonth(), utcDate.getUTCDate());
    return localDateSameDay.toLocaleDateString();
};
exports.utcDateToLocale = utcDateToLocale;
const getCurrentYear = () => {
    const currentDate = new Date();
    return currentDate.getFullYear();
};
exports.getCurrentYear = getCurrentYear;
//# sourceMappingURL=dates.js.map