/**
 * DevExtreme (cjs/ui/shared/filtering.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _type = require("../../core/utils/type");
var _iterator = require("../../core/utils/iterator");
const DEFAULT_DATE_INTERVAL = ["year", "month", "day"];
const DEFAULT_DATETIME_INTERVAL = ["year", "month", "day", "hour", "minute"];
const isDateType = function(dataType) {
    return "date" === dataType || "datetime" === dataType
};
const getGroupInterval = function(column) {
    let index;
    let result = [];
    const dateIntervals = ["year", "month", "day", "hour", "minute", "second"];
    const groupInterval = column.headerFilter && column.headerFilter.groupInterval;
    const interval = "quarter" === groupInterval ? "month" : groupInterval;
    if (isDateType(column.dataType) && null !== groupInterval) {
        result = "datetime" === column.dataType ? DEFAULT_DATETIME_INTERVAL : DEFAULT_DATE_INTERVAL;
        index = dateIntervals.indexOf(interval);
        if (index >= 0) {
            result = dateIntervals.slice(0, index);
            result.push(groupInterval);
            return result
        }
        return result
    } else if ((0, _type.isDefined)(groupInterval)) {
        return Array.isArray(groupInterval) ? groupInterval : [groupInterval]
    }
};
var _default = exports.default = function() {
    const getFilterSelector = function(column, target) {
        let selector = column.dataField || column.selector;
        if ("search" === target) {
            selector = column.displayField || column.calculateDisplayValue || selector
        }
        return selector
    };
    const getFilterExpressionByRange = function(filterValue, target) {
        const column = this;
        let endFilterValue;
        let startFilterExpression;
        let endFilterExpression;
        const selector = getFilterSelector(column, target);
        if (Array.isArray(filterValue) && (0, _type.isDefined)(filterValue[0]) && (0, _type.isDefined)(filterValue[1])) {
            startFilterExpression = [selector, ">=", filterValue[0]];
            endFilterExpression = [selector, "<=", filterValue[1]];
            if (isDateType(column.dataType) && (date = filterValue[1], date.getHours() + date.getMinutes() + date.getSeconds() + date.getMilliseconds() < 1)) {
                endFilterValue = new Date(filterValue[1].getTime());
                if ("date" === column.dataType) {
                    endFilterValue.setDate(filterValue[1].getDate() + 1)
                }
                endFilterExpression = [selector, "<", endFilterValue]
            }
            return [startFilterExpression, "and", endFilterExpression]
        }
        var date
    };
    const getFilterExpressionForDate = function(filterValue, selectedFilterOperation, target) {
        const column = this;
        let dateStart;
        let dateEnd;
        let dateInterval;
        const values = function(dateValue) {
            if ((0, _type.isDate)(dateValue)) {
                return [dateValue.getFullYear(), dateValue.getMonth(), dateValue.getDate(), dateValue.getHours(), dateValue.getMinutes(), dateValue.getSeconds()]
            }
            return (0, _iterator.map)(("" + dateValue).split("/"), (function(value, index) {
                return 1 === index ? Number(value) - 1 : Number(value)
            }))
        }(filterValue);
        const selector = getFilterSelector(column, target);
        if ("headerFilter" === target) {
            dateInterval = getGroupInterval(column)[values.length - 1]
        } else if ("datetime" === column.dataType) {
            dateInterval = "minute"
        }
        switch (dateInterval) {
            case "year":
                dateStart = new Date(values[0], 0, 1);
                dateEnd = new Date(values[0] + 1, 0, 1);
                break;
            case "month":
                dateStart = new Date(values[0], values[1], 1);
                dateEnd = new Date(values[0], values[1] + 1, 1);
                break;
            case "quarter":
                dateStart = new Date(values[0], 3 * values[1], 1);
                dateEnd = new Date(values[0], 3 * values[1] + 3, 1);
                break;
            case "hour":
                dateStart = new Date(values[0], values[1], values[2], values[3]);
                dateEnd = new Date(values[0], values[1], values[2], values[3] + 1);
                break;
            case "minute":
                dateStart = new Date(values[0], values[1], values[2], values[3], values[4]);
                dateEnd = new Date(values[0], values[1], values[2], values[3], values[4] + 1);
                break;
            case "second":
                dateStart = new Date(values[0], values[1], values[2], values[3], values[4], values[5]);
                dateEnd = new Date(values[0], values[1], values[2], values[3], values[4], values[5] + 1);
                break;
            default:
                dateStart = new Date(values[0], values[1], values[2]);
                dateEnd = new Date(values[0], values[1], values[2] + 1)
        }
        switch (selectedFilterOperation) {
            case "<":
                return [selector, "<", dateStart];
            case "<=":
                return [selector, "<", dateEnd];
            case ">":
                return [selector, ">=", dateEnd];
            case ">=":
                return [selector, ">=", dateStart];
            case "<>":
                return [
                    [selector, "<", dateStart], "or", [selector, ">=", dateEnd]
                ];
            default:
                return [
                    [selector, ">=", dateStart], "and", [selector, "<", dateEnd]
                ]
        }
    };
    const getFilterExpressionForNumber = function(filterValue, selectedFilterOperation, target) {
        const selector = getFilterSelector(this, target);
        const groupInterval = getGroupInterval(this);
        if ("headerFilter" === target && groupInterval && (0, _type.isDefined)(filterValue)) {
            const values = ("" + filterValue).split("/");
            const value = Number(values[values.length - 1]);
            const interval = groupInterval[values.length - 1];
            const startFilterValue = [selector, ">=", value];
            const endFilterValue = [selector, "<", value + interval];
            const condition = [startFilterValue, "and", endFilterValue];
            return condition
        }
        return [selector, selectedFilterOperation || "=", filterValue]
    };
    return {
        defaultCalculateFilterExpression: function(filterValue, selectedFilterOperation, target) {
            const column = this;
            const selector = getFilterSelector(column, target);
            const isSearchByDisplayValue = column.calculateDisplayValue && "search" === target;
            const dataType = isSearchByDisplayValue && column.lookup && column.lookup.dataType || column.dataType;
            let filter = null;
            if (("headerFilter" === target || "filterBuilder" === target) && null === filterValue) {
                filter = [selector, selectedFilterOperation || "=", null];
                if ("string" === dataType) {
                    filter = [filter, "=" === selectedFilterOperation ? "or" : "and", [selector, selectedFilterOperation || "=", ""]]
                }
            } else if ("string" === dataType && (!column.lookup || isSearchByDisplayValue)) {
                filter = [selector, selectedFilterOperation || "contains", filterValue]
            } else if ("between" === selectedFilterOperation) {
                return getFilterExpressionByRange.apply(column, [filterValue, target])
            } else if (isDateType(dataType) && (0, _type.isDefined)(filterValue)) {
                return getFilterExpressionForDate.apply(column, arguments)
            } else if ("number" === dataType) {
                return getFilterExpressionForNumber.apply(column, arguments)
            } else {
                filter = [selector, selectedFilterOperation || "=", filterValue]
            }
            return filter
        },
        getGroupInterval: getGroupInterval
    }
}();
module.exports = exports.default;
module.exports.default = exports.default;
