import { isNumber } from '@slickgrid-universal/utils';
import { OperatorType } from '../enums/index';
import { testFilterCondition } from './filterUtilities';
/** Execute filter condition check on each cell */
export const executeNumberFilterCondition = ((options, parsedSearchValues) => {
    const cellValue = parseFloat(options.cellValue);
    const [searchValue1, searchValue2] = parsedSearchValues;
    if (searchValue1 === undefined && !options.operator) {
        return true;
    }
    if (searchValue1 !== undefined && searchValue2 !== undefined) {
        let operator = options?.operator ?? options.defaultFilterRangeOperator;
        if (operator !== OperatorType.rangeInclusive && operator !== OperatorType.rangeExclusive) {
            operator = options.defaultFilterRangeOperator;
        }
        const isInclusive = operator === OperatorType.rangeInclusive;
        const resultCondition1 = testFilterCondition((isInclusive ? '>=' : '>'), cellValue, +searchValue1);
        const resultCondition2 = testFilterCondition((isInclusive ? '<=' : '<'), cellValue, +searchValue2);
        return (resultCondition1 && resultCondition2);
    }
    return testFilterCondition(options.operator || '==', cellValue, +searchValue1);
});
/**
 * From our search filter value(s), get the parsed value(s).
 * This is called only once per filter before running the actual filter condition check on each cell
 */
export function getFilterParsedNumbers(inputSearchTerms) {
    const defaultSearchTerm = 0; // when nothing is provided, we'll default to 0
    const searchTerms = Array.isArray(inputSearchTerms) && inputSearchTerms || [defaultSearchTerm];
    const parsedSearchValues = [];
    let searchValue1;
    let searchValue2;
    if (searchTerms.length === 2 || (typeof searchTerms[0] === 'string' && searchTerms[0].indexOf('..') > 0)) {
        const searchValues = (searchTerms.length === 2) ? searchTerms : searchTerms[0].split('..');
        searchValue1 = parseFloat(Array.isArray(searchValues) ? searchValues[0] : '');
        searchValue2 = parseFloat(Array.isArray(searchValues) ? searchValues[1] : '');
    }
    else {
        searchValue1 = parseFloat(searchTerms[0]);
    }
    if (isNumber(searchValue1, true) && isNumber(searchValue2, true)) {
        parsedSearchValues.push(searchValue1, searchValue2);
    }
    else if (isNumber(searchValue1, true)) {
        parsedSearchValues.push(searchValue1);
    }
    return parsedSearchValues;
}
//# sourceMappingURL=numberFilterCondition.js.map