"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDateValue = exports.parseToISO = exports.dateToISO = exports.isValueValidDate = exports.isValidDate = void 0;
const date_fns_1 = require("date-fns");
const FormatHelper_1 = require("./FormatHelper");
const AdaptableLogger_1 = require("../../agGrid/AdaptableLogger");
const isValidDate = (date) => {
    return date instanceof Date && !isNaN(date.getTime());
};
exports.isValidDate = isValidDate;
/**
 * Checks if the data contains a value that represents a valid date.
 *
 * @param data any kind of data
 */
const isValueValidDate = (data) => {
    if (!data || typeof data === 'boolean') {
        return false;
    }
    const date = new Date(data);
    return !isNaN(date.getTime());
};
exports.isValueValidDate = isValueValidDate;
const dateToISO = (date) => {
    return (0, FormatHelper_1.DateFormatter)(date, { Pattern: 'yyyy-MM-dd' });
};
exports.dateToISO = dateToISO;
const parseToISO = (date, dateFormat) => {
    const dateInstance = (0, exports.parseDateValue)(date, dateFormat);
    return (0, exports.isValidDate)(dateInstance) ? (0, exports.dateToISO)(dateInstance) : '';
};
exports.parseToISO = parseToISO;
// !!!
// AFL: ideally we should NEVER call directly this method, but use the AdaptableInternalApi.parseDateValue() instead
// we should refactor this with the first opportunity
const parseDateValue = (dateValue, dateFormat) => {
    if (dateValue == undefined || (typeof dateValue === 'string' && dateValue.trim() === '')) {
        return undefined;
    }
    if (dateValue instanceof Date) {
        return !isNaN(dateValue.getTime()) ? dateValue : undefined;
    }
    try {
        let dateInstance;
        if (typeof dateValue === 'number') {
            dateInstance = new Date(dateValue);
        }
        else {
            // typeof dateValue === 'string'
            if (dateFormat) {
                dateInstance = (0, date_fns_1.parse)(dateValue, dateFormat, new Date());
            }
            else {
                dateInstance = (0, date_fns_1.parseISO)(dateValue);
            }
            if (!(0, exports.isValidDate)(dateInstance)) {
                // last chance: try to use the native Date.parse(), https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
                dateInstance = new Date(Date.parse(dateValue));
            }
        }
        if (!(0, exports.isValidDate)(dateInstance)) {
            AdaptableLogger_1.AdaptableLogger.consoleWarnBase(`Invalid date value "${dateValue}" - cannot be converted to a Date instance. Please make sure you specify adaptableOptions.userInterfaceOptions.dateInputOptions.dateFormat correctly.`);
            dateInstance = undefined;
        }
        return dateInstance;
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        const errorStack = error instanceof Error ? error.stack : undefined;
        AdaptableLogger_1.AdaptableLogger.consoleErrorBase(`Error parsing date value "${dateValue}": ${errorMessage}`, {
            stack: errorStack,
            originalError: error,
        });
    }
};
exports.parseDateValue = parseDateValue;
