"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAssociatedDateSortComparer = exports.compareDates = void 0;
const fieldType_enum_1 = require("../enums/fieldType.enum");
const dateUtils_1 = require("../services/dateUtils");
function compareDates(value1, value2, sortDirection, format, strict) {
    let diff = 0;
    if (value1 === value2) {
        diff = 0;
    }
    else {
        // try to parse the Date and validate it
        let date1 = (0, dateUtils_1.tryParseDate)(value1, format, strict);
        let date2 = (0, dateUtils_1.tryParseDate)(value2, format, strict);
        // when date is invalid (false), we'll create a temporary old Date
        if (!date1) {
            date1 = new Date(1001, 1, 1);
        }
        if (!date2) {
            date2 = new Date(1001, 1, 1);
        }
        // we can use Date valueOf to sort
        diff = date1.valueOf() - date2.valueOf();
    }
    return sortDirection * diff;
}
exports.compareDates = compareDates;
/** From a FieldType, return the associated Date SortComparer */
function getAssociatedDateSortComparer(fieldType) {
    const FORMAT = (fieldType === fieldType_enum_1.FieldType.date) ? undefined : (0, dateUtils_1.mapTempoDateFormatWithFieldType)(fieldType);
    return ((value1, value2, sortDirection) => {
        if (FORMAT === undefined) {
            return compareDates(value1, value2, sortDirection, FORMAT, false);
        }
        return compareDates(value1, value2, sortDirection, FORMAT, true);
    });
}
exports.getAssociatedDateSortComparer = getAssociatedDateSortComparer;
//# sourceMappingURL=dateUtilities.js.map