"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ISO_DATE_REGEX () {
        return ISO_DATE_REGEX;
    },
    get TIME_REGEX () {
        return TIME_REGEX;
    },
    get getNormalizedDate () {
        return getNormalizedDate;
    },
    get isValidISODate () {
        return isValidISODate;
    },
    get isValidTime () {
        return isValidTime;
    },
    get parseToLocalDate () {
        return parseToLocalDate;
    },
    get parseToLocalTime () {
        return parseToLocalTime;
    }
});
const _mixed = require("./mixed");
const ISO_DATE_REGEX = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
function getNormalizedDate(dateString) {
    const nativeDate = new Date(dateString);
    // NaN if dateString is not in ISO format
    if (!isNaN(new Date(`${dateString}T00:00`).getDate())) {
        // Compensate timezone offset
        return new Date(nativeDate.getTime() + nativeDate.getTimezoneOffset() * 60000);
    }
    return nativeDate;
}
function parseToLocalDate(value) {
    if ((0, _mixed.isEmpty)(value)) {
        return null;
    }
    if (typeof value === 'string' && ISO_DATE_REGEX.test(value)) {
        const [y, m, d] = value.split('-').map(Number);
        return new Date(y, m - 1, d);
    }
    return null;
}
function isValidISODate(value) {
    if (typeof value !== 'string' || !ISO_DATE_REGEX.test(value)) {
        return false;
    }
    const date = new Date(value);
    return !Number.isNaN(date.getTime()) && value === date.toISOString().slice(0, 10);
}
const TIME_REGEX = /^([01]\d|2[0-3]):([0-5]\d)(?::([0-5]\d)(?:\.(\d{1,3}))?)?$/;
function parseToLocalTime(value) {
    if ((0, _mixed.isEmpty)(value)) {
        return null;
    }
    if (typeof value !== 'string') {
        return null;
    }
    const match = TIME_REGEX.exec(value);
    if (!match) {
        return null;
    }
    const hours = Number(match[1]);
    const minutes = Number(match[2]);
    const seconds = match[3] !== undefined ? Number(match[3]) : 0;
    const msString = match[4];
    const milliseconds = msString !== undefined ? Number(msString.padEnd(3, '0').slice(0, 3)) : 0;
    return new Date(1970, 0, 1, hours, minutes, seconds, milliseconds);
}
function isValidTime(value) {
    return typeof value === 'string' && TIME_REGEX.test(value);
}
