"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeDateFilterCondition = executeDateFilterCondition;
exports.getFilterParsedDates = getFilterParsedDates;
const tempo_1 = require("@formkit/tempo");
const index_js_1 = require("../enums/index.js");
const filterUtilities_js_1 = require("./filterUtilities.js");
const index_js_2 = require("../services/index.js");
/**
 * Execute Date filter condition check on each cell and use correct date format depending on it's field type (or filterSearchType when that is provided)
 */
function executeDateFilterCondition(options, parsedSearchDates) {
    const filterSearchType = (options && (options.filterSearchType || options.fieldType)) || index_js_1.FieldType.dateIso;
    const FORMAT = (0, index_js_2.mapTempoDateFormatWithFieldType)(filterSearchType);
    const [searchDate1, searchDate2] = parsedSearchDates;
    // cell value in Date format
    const dateCell = (0, index_js_2.tryParseDate)(options.cellValue, FORMAT, true);
    // return when cell value is not a valid date
    if ((!searchDate1 && !searchDate2) || !dateCell) {
        return false;
    }
    // when comparing with Dates only (without time), we need to disregard the time portion, we can do so by setting our time to start at midnight
    // ref, see https://stackoverflow.com/a/19699447/1212166
    const dateCellTimestamp = FORMAT === 'ISO8601' || FORMAT.toLowerCase().includes('h') ? dateCell.valueOf() : (0, tempo_1.dayStart)(new Date(dateCell)).valueOf();
    // having 2 search dates, we assume that it's a date range filtering and we'll compare against both dates
    if (searchDate1 && searchDate2) {
        let operator = options?.operator ?? options.defaultFilterRangeOperator;
        if (operator !== index_js_1.OperatorType.rangeInclusive && operator !== index_js_1.OperatorType.rangeExclusive) {
            operator = options.defaultFilterRangeOperator;
        }
        const isInclusive = operator === index_js_1.OperatorType.rangeInclusive;
        const resultCondition1 = (0, filterUtilities_js_1.testFilterCondition)(isInclusive ? '>=' : '>', dateCellTimestamp, searchDate1.valueOf());
        const resultCondition2 = (0, filterUtilities_js_1.testFilterCondition)(isInclusive ? '<=' : '<', dateCellTimestamp, searchDate2.valueOf());
        return resultCondition1 && resultCondition2;
    }
    // comparing against a single search date
    const dateSearchTimestamp1 = FORMAT === 'ISO8601' || FORMAT.toLowerCase().includes('h') ? searchDate1.valueOf() : (0, tempo_1.dayStart)(new Date(searchDate1)).valueOf();
    return (0, filterUtilities_js_1.testFilterCondition)(options.operator || '==', dateCellTimestamp, dateSearchTimestamp1);
}
/**
 * From our search filter value(s), get the parsed value(s), they are parsed as Date objects.
 * This is called only once per filter before running the actual filter condition check on each cell
 */
function getFilterParsedDates(inputSearchTerms, inputFilterSearchType) {
    const searchTerms = (Array.isArray(inputSearchTerms) && inputSearchTerms) || [];
    const filterSearchType = inputFilterSearchType || index_js_1.FieldType.dateIso;
    const FORMAT = (0, index_js_2.mapTempoDateFormatWithFieldType)(filterSearchType);
    const parsedSearchValues = [];
    if (searchTerms.length === 2 || (typeof searchTerms[0] === 'string' && searchTerms[0].indexOf('..') > 0)) {
        const searchValues = searchTerms.length === 2 ? searchTerms : searchTerms[0].split('..');
        const searchValue1 = ((Array.isArray(searchValues) && searchValues[0]) || '');
        const searchValue2 = ((Array.isArray(searchValues) && searchValues[1]) || '');
        const searchDate1 = (0, index_js_2.tryParseDate)(searchValue1, FORMAT, true);
        const searchDate2 = (0, index_js_2.tryParseDate)(searchValue2, FORMAT, true);
        // return if any of the 2 values are invalid dates
        if (!searchDate1 || !searchDate2) {
            return [];
        }
        parsedSearchValues.push(searchDate1, searchDate2);
    }
    else {
        // return if the search term is an invalid date
        const searchDate1 = (0, index_js_2.tryParseDate)(searchTerms[0], FORMAT, true);
        if (!searchDate1) {
            return [];
        }
        parsedSearchValues.push(searchDate1);
    }
    return parsedSearchValues;
}
//# sourceMappingURL=dateFilterCondition.js.map