"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseUtcDate = exports.toUtcDate = exports.tryParseDate = exports.formatDateByFieldType = exports.mapTempoDateFormatWithFieldType = void 0;
const tempo_1 = require("@formkit/tempo");
const index_1 = require("../enums/index");
/**
 * From a Date FieldType, return it's equivalent TempoJS format,
 * refer to TempoJS docs for the format tokens being used: https://tempo.formkit.com/#format
 * @param fieldType
 * @param withZeroPadding - should we include zero padding in format (e.g.: 03:04:54)
 */
function mapTempoDateFormatWithFieldType(fieldType, withZeroPadding = false) {
    let map;
    switch (fieldType) {
        case index_1.FieldType.dateTime:
        case index_1.FieldType.dateTimeIso:
            map = 'YYYY-MM-DD HH:mm:ss';
            break;
        case index_1.FieldType.dateTimeIsoAmPm:
            map = 'YYYY-MM-DD hh:mm:ss a';
            break;
        case index_1.FieldType.dateTimeIsoAM_PM:
            map = 'YYYY-MM-DD hh:mm:ss A';
            break;
        case index_1.FieldType.dateTimeShortIso:
            map = 'YYYY-MM-DD HH:mm';
            break;
        // all Euro Formats (date/month/year)
        case index_1.FieldType.dateEuro:
            map = 'DD/MM/YYYY';
            break;
        case index_1.FieldType.dateEuroShort:
            map = withZeroPadding
                ? 'DD/MM/YY'
                : 'D/M/YY';
            break;
        case index_1.FieldType.dateTimeEuro:
            map = 'DD/MM/YYYY HH:mm:ss';
            break;
        case index_1.FieldType.dateTimeShortEuro:
            map = withZeroPadding
                ? 'DD/MM/YYYY HH:mm'
                : 'D/M/YYYY H:m';
            break;
        case index_1.FieldType.dateTimeEuroAmPm:
            map = 'DD/MM/YYYY hh:mm:ss a';
            break;
        case index_1.FieldType.dateTimeEuroAM_PM:
            map = 'DD/MM/YYYY hh:mm:ss A';
            break;
        case index_1.FieldType.dateTimeEuroShort:
            map = withZeroPadding
                ? 'DD/MM/YY HH:mm:ss'
                : 'D/M/YY H:m:s';
            break;
        case index_1.FieldType.dateTimeEuroShortAmPm:
            map = withZeroPadding
                ? 'DD/MM/YY hh:mm:ss a'
                : 'D/M/YY h:m:s a';
            break;
        case index_1.FieldType.dateTimeEuroShortAM_PM:
            map = withZeroPadding
                ? 'DD/MM/YY hh:mm:ss A'
                : 'D/M/YY h:m:s A';
            break;
        // all US Formats (month/date/year)
        case index_1.FieldType.dateUs:
            map = 'MM/DD/YYYY';
            break;
        case index_1.FieldType.dateUsShort:
            map = withZeroPadding
                ? 'MM/DD/YY'
                : 'M/D/YY';
            break;
        case index_1.FieldType.dateTimeUs:
            map = 'MM/DD/YYYY HH:mm:ss';
            break;
        case index_1.FieldType.dateTimeUsAmPm:
            map = 'MM/DD/YYYY hh:mm:ss a';
            break;
        case index_1.FieldType.dateTimeUsAM_PM:
            map = 'MM/DD/YYYY hh:mm:ss A';
            break;
        case index_1.FieldType.dateTimeUsShort:
            map = withZeroPadding
                ? 'MM/DD/YY HH:mm:ss'
                : 'M/D/YY H:m:s';
            break;
        case index_1.FieldType.dateTimeUsShortAmPm:
            map = withZeroPadding
                ? 'MM/DD/YY hh:mm:ss a'
                : 'M/D/YY h:m:s a';
            break;
        case index_1.FieldType.dateTimeUsShortAM_PM:
            map = withZeroPadding
                ? 'MM/DD/YY hh:mm:ss A'
                : 'M/D/YY h:m:s A';
            break;
        case index_1.FieldType.dateTimeShortUs:
            map = withZeroPadding
                ? 'MM/DD/YYYY HH:mm'
                : 'M/D/YYYY H:m';
            break;
        case index_1.FieldType.dateUtc:
            map = 'ISO8601';
            break;
        case index_1.FieldType.date:
        case index_1.FieldType.dateIso:
        default:
            map = 'YYYY-MM-DD';
            break;
    }
    return map;
}
exports.mapTempoDateFormatWithFieldType = mapTempoDateFormatWithFieldType;
/**
 * Format a date using Tempo and a defined input/output field types
 * @param {string|Date} inputDate
 * @param {FieldType} inputFieldType
 * @param {FieldType} outputFieldType
 * @returns
 */
function formatDateByFieldType(inputDate, inputFieldType, outputFieldType) {
    const inputFormat = inputFieldType ? mapTempoDateFormatWithFieldType(inputFieldType) : undefined;
    const outputFormat = mapTempoDateFormatWithFieldType(outputFieldType);
    const date = inputDate instanceof Date ? inputDate : tryParseDate(inputDate, inputFormat);
    if (date && inputDate !== undefined) {
        if (outputFieldType === index_1.FieldType.dateUtc) {
            return date.toISOString();
        }
        return (0, tempo_1.format)(date, outputFormat, 'en-US');
    }
    return '';
}
exports.formatDateByFieldType = formatDateByFieldType;
/**
 * Try to parse date with Tempo or return `false` (instead of throwing) if Date is invalid.
 * When using strict mode, it will detect if the date is invalid when outside of the calendar (e.g. "2011-11-31").
 * However in non-strict mode, it will roll the date backward if out of calendar (e.g. "2011-11-31" would return "2011-11-30").
   * @param {string|Date} [inputDate] - input date (or null)
 * @param {string} [inputFormat] - optional input format to use when parsing
 * @param {Boolean} [strict] - are we using strict mode?
 * @returns
 */
function tryParseDate(inputDate, inputFormat, strict = false) {
    try {
        if (!inputDate) {
            return false;
        }
        return inputDate instanceof Date ? inputDate : (0, tempo_1.parse)({
            date: inputDate,
            format: inputFormat,
            dateOverflow: strict ? 'throw' : 'backward',
            locale: 'en-US'
        });
    }
    catch (_e) {
        return false;
    }
}
exports.tryParseDate = tryParseDate;
/**
 * Parse a Date as a UTC date (without local TZ offset)
 * @param inputDate
 * @returns
 */
function toUtcDate(inputDate) {
    // to parse as UTC in Tempo, we need to remove the offset (which is a simple inversed offset to cancel itself)
    return (0, tempo_1.removeOffset)(inputDate, (0, tempo_1.offset)(inputDate, 'utc'));
}
exports.toUtcDate = toUtcDate;
;
/**
 * Parse a date passed as a string (Date only, without time) and return a TZ Date (without milliseconds)
 * @param inputDateString
 * @returns TZ UTC date formatted
 */
function parseUtcDate(inputDateString) {
    let outputFormattedDate = '';
    if (typeof inputDateString === 'string' && /^[0-9\-/]*$/.test(inputDateString)) {
        // get the UTC datetime but make sure to decode the value so that it's valid text
        const dateString = decodeURIComponent(inputDateString);
        const date = (0, tempo_1.tzDate)(dateString, 'utc');
        if (date) {
            outputFormattedDate = date.toISOString().replace(/(.*)([.\d]{4})(Z)/gi, '$1$3');
        }
    }
    return outputFormattedDate;
}
exports.parseUtcDate = parseUtcDate;
//# sourceMappingURL=dateUtils.js.map