/**
 * DevExtreme (renovation/ui/scheduler/utils/filtering/remote.js)
 * Version: 22.1.7 (build 23032-0308)
 * Build date: Wed Feb 01 2023
 *
 * Copyright (c) 2012 - 2023 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _common = require("../../../../../core/utils/common");
var _extend = require("../../../../../core/utils/extend");
var _date_serialization = _interopRequireDefault(require("../../../../../core/utils/date_serialization"));
var _type = require("../../../../../core/utils/type");
var _getDatesWithoutTime3 = _interopRequireDefault(require("./getDatesWithoutTime"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _typeof(obj) {
    return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
        return typeof obj
    } : function(obj) {
        return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
    }, _typeof(obj)
}

function _slicedToArray(arr, i) {
    return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest()
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

function _unsupportedIterableToArray(o, minLen) {
    if (!o) {
        return
    }
    if ("string" === typeof o) {
        return _arrayLikeToArray(o, minLen)
    }
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if ("Object" === n && o.constructor) {
        n = o.constructor.name
    }
    if ("Map" === n || "Set" === n) {
        return Array.from(o)
    }
    if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
        return _arrayLikeToArray(o, minLen)
    }
}

function _arrayLikeToArray(arr, len) {
    if (null == len || len > arr.length) {
        len = arr.length
    }
    for (var i = 0, arr2 = new Array(len); i < len; i++) {
        arr2[i] = arr[i]
    }
    return arr2
}

function _iterableToArrayLimit(arr, i) {
    var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
    if (null != _i) {
        var _s, _e, _x, _r, _arr = [],
            _n = !0,
            _d = !1;
        try {
            if (_x = (_i = _i.call(arr)).next, 0 === i) {
                if (Object(_i) !== _i) {
                    return
                }
                _n = !1
            } else {
                for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) {}
            }
        } catch (err) {
            _d = !0, _e = err
        } finally {
            try {
                if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) {
                    return
                }
            } finally {
                if (_d) {
                    throw _e
                }
            }
        }
        return _arr
    }
}

function _arrayWithHoles(arr) {
    if (Array.isArray(arr)) {
        return arr
    }
}

function _defineProperties(target, props) {
    for (var i = 0; i < props.length; i++) {
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) {
            descriptor.writable = true
        }
        Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor)
    }
}

function _createClass(Constructor, protoProps, staticProps) {
    if (protoProps) {
        _defineProperties(Constructor.prototype, protoProps)
    }
    if (staticProps) {
        _defineProperties(Constructor, staticProps)
    }
    Object.defineProperty(Constructor, "prototype", {
        writable: false
    });
    return Constructor
}

function _toPropertyKey(arg) {
    var key = _toPrimitive(arg, "string");
    return "symbol" === _typeof(key) ? key : String(key)
}

function _toPrimitive(input, hint) {
    if ("object" !== _typeof(input) || null === input) {
        return input
    }
    var prim = input[Symbol.toPrimitive];
    if (void 0 !== prim) {
        var res = prim.call(input, hint || "default");
        if ("object" !== _typeof(res)) {
            return res
        }
        throw new TypeError("@@toPrimitive must return a primitive value.")
    }
    return ("string" === hint ? String : Number)(input)
}
var FilterPosition = {
    dateFilter: 0,
    userFilter: 1
};
var RemoteFilterCombiner = function() {
    function RemoteFilterCombiner(options) {
        this.options = options
    }
    var _proto = RemoteFilterCombiner.prototype;
    _proto.makeDateFilter = function(min, max) {
        var _this$dataAccessors$e = this.dataAccessors.expr,
            endDateExpr = _this$dataAccessors$e.endDateExpr,
            recurrenceRuleExpr = _this$dataAccessors$e.recurrenceRuleExpr,
            startDateExpr = _this$dataAccessors$e.startDateExpr;
        var dateFilter = [
            [
                [endDateExpr, ">=", min],
                [startDateExpr, "<", max]
            ], "or", [recurrenceRuleExpr, "startswith", "freq"], "or", [
                [endDateExpr, min],
                [startDateExpr, min]
            ]
        ];
        if (!recurrenceRuleExpr) {
            dateFilter.splice(1, 2)
        }
        return dateFilter
    };
    _proto.combineFilters = function(dateFilter, userFilter) {
        var combinedFilter = [];
        dateFilter && combinedFilter.push(dateFilter);
        userFilter && combinedFilter.push(userFilter);
        return this.serializeRemoteFilter(combinedFilter)
    };
    _proto.serializeRemoteFilter = function(combinedFilter) {
        if (!Array.isArray(combinedFilter)) {
            return combinedFilter
        }
        var _this$dataAccessors$e2 = this.dataAccessors.expr,
            endDateExpr = _this$dataAccessors$e2.endDateExpr,
            startDateExpr = _this$dataAccessors$e2.startDateExpr;
        var filter = (0, _extend.extend)([], combinedFilter);
        if ((0, _type.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] = _date_serialization.default.serializeDate(new Date(lastFilterValue), this.dateSerializationFormat)
                }
            }
        }
        for (var i = 0; i < filter.length; i += 1) {
            filter[i] = this.serializeRemoteFilter(filter[i])
        }
        return filter
    };
    _proto.getUserFilter = function(dateFilter) {
        if (!this.dataSourceFilter || (0, _common.equalByValue)(this.dataSourceFilter, dateFilter)) {
            return
        }
        var containsDateFilter = this.dataSourceFilter.length > 0 && (0, _common.equalByValue)(this.dataSourceFilter[FilterPosition.dateFilter], dateFilter);
        var userFilter = containsDateFilter ? this.dataSourceFilter[FilterPosition.userFilter] : this.dataSourceFilter;
        return userFilter
    };
    _proto.combine = function(min, max) {
        var _getDatesWithoutTime = (0, _getDatesWithoutTime3.default)(min, max),
            _getDatesWithoutTime2 = _slicedToArray(_getDatesWithoutTime, 2),
            trimMin = _getDatesWithoutTime2[0],
            trimMax = _getDatesWithoutTime2[1];
        var dateFilter = this.makeDateFilter(trimMin, trimMax);
        var userFilter = this.getUserFilter(dateFilter);
        var combinedFilter = this.combineFilters(dateFilter, userFilter);
        return combinedFilter
    };
    _createClass(RemoteFilterCombiner, [{
        key: "dataAccessors",
        get: function() {
            return this.options.dataAccessors
        }
    }, {
        key: "dataSourceFilter",
        get: function() {
            return this.options.dataSourceFilter
        }
    }, {
        key: "dateSerializationFormat",
        get: function() {
            return this.options.dateSerializationFormat
        }
    }, {
        key: "forceIsoDateParsing",
        get: function() {
            return (0, _type.isDefined)(this.options.forceIsoDateParsing) ? this.options.forceIsoDateParsing : true
        }
    }]);
    return RemoteFilterCombiner
}();
var combineRemoteFilter = function(options) {
    return new RemoteFilterCombiner(options).combine(options.min, options.max)
};
var _default = combineRemoteFilter;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
