/**
 * DevExtreme (esm/renovation/ui/scheduler/utils/filtering/remote.js)
 * Version: 22.2.7 (build 23185-0311)
 * Build date: Tue Jul 04 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    equalByValue
} from "../../../../../core/utils/common";
import {
    extend
} from "../../../../../core/utils/extend";
import dateSerialization from "../../../../../core/utils/date_serialization";
import {
    isDefined,
    isString
} from "../../../../../core/utils/type";
import getDatesWithoutTime from "./getDatesWithoutTime";
var FilterPosition = {
    dateFilter: 0,
    userFilter: 1
};
class RemoteFilterCombiner {
    constructor(options) {
        this.options = options
    }
    get dataAccessors() {
        return this.options.dataAccessors
    }
    get dataSourceFilter() {
        return this.options.dataSourceFilter
    }
    get dateSerializationFormat() {
        return this.options.dateSerializationFormat
    }
    get forceIsoDateParsing() {
        return isDefined(this.options.forceIsoDateParsing) ? this.options.forceIsoDateParsing : true
    }
    makeDateFilter(min, max) {
        var {
            endDateExpr: endDateExpr,
            recurrenceRuleExpr: recurrenceRuleExpr,
            startDateExpr: startDateExpr
        } = this.dataAccessors.expr;
        var dateFilter = [
            [
                [endDateExpr, ">=", min],
                [startDateExpr, "<", max]
            ], "or", [recurrenceRuleExpr, "startswith", "freq"], "or", [
                [endDateExpr, min],
                [startDateExpr, min]
            ]
        ];
        if (!recurrenceRuleExpr) {
            dateFilter.splice(1, 2)
        }
        return dateFilter
    }
    combineFilters(dateFilter, userFilter) {
        var combinedFilter = [];
        dateFilter && combinedFilter.push(dateFilter);
        userFilter && combinedFilter.push(userFilter);
        return this.serializeRemoteFilter(combinedFilter)
    }
    serializeRemoteFilter(combinedFilter) {
        if (!Array.isArray(combinedFilter)) {
            return combinedFilter
        }
        var {
            endDateExpr: endDateExpr,
            startDateExpr: startDateExpr
        } = this.dataAccessors.expr;
        var filter = extend([], combinedFilter);
        if (isString(filter[0])) {
            if (this.forceIsoDateParsing && filter.length > 1) {
                if (filter[0] === startDateExpr || filter[0] === endDateExpr) {
                    var lastFilterValue = filter[filter.length - 1];
                    filter[filter.length - 1] = dateSerialization.serializeDate(new Date(lastFilterValue), this.dateSerializationFormat)
                }
            }
        }
        for (var i = 0; i < filter.length; i += 1) {
            filter[i] = this.serializeRemoteFilter(filter[i])
        }
        return filter
    }
    getUserFilter(dateFilter) {
        if (!this.dataSourceFilter || equalByValue(this.dataSourceFilter, dateFilter)) {
            return
        }
        var containsDateFilter = this.dataSourceFilter.length > 0 && equalByValue(this.dataSourceFilter[FilterPosition.dateFilter], dateFilter);
        var userFilter = containsDateFilter ? this.dataSourceFilter[FilterPosition.userFilter] : this.dataSourceFilter;
        return userFilter
    }
    combine(min, max) {
        var [trimMin, trimMax] = getDatesWithoutTime(min, max);
        var dateFilter = this.makeDateFilter(trimMin, trimMax);
        var userFilter = this.getUserFilter(dateFilter);
        var combinedFilter = this.combineFilters(dateFilter, userFilter);
        return combinedFilter
    }
}
var combineRemoteFilter = options => new RemoteFilterCombiner(options).combine(options.min, options.max);
export default combineRemoteFilter;
