/**
 * DevExtreme (cjs/ui/calendar/ui.calendar.range.selection.strategy.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 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 _date = _interopRequireDefault(require("../../core/utils/date"));
var _uiCalendarSelection = _interopRequireDefault(require("./ui.calendar.selection.strategy"));

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

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const DAY_INTERVAL = 864e5;
let CalendarRangeSelectionStrategy = function(_CalendarSelectionStr) {
    _inheritsLoose(CalendarRangeSelectionStrategy, _CalendarSelectionStr);

    function CalendarRangeSelectionStrategy(component) {
        var _this;
        _this = _CalendarSelectionStr.call(this, component) || this;
        _this.NAME = "RangeSelection";
        return _this
    }
    var _proto = CalendarRangeSelectionStrategy.prototype;
    _proto.getViewOptions = function() {
        const value = this._getValue();
        const range = this._getDaysInRange(value[0], value[1]);
        return {
            value: value,
            range: range,
            selectionMode: "range",
            onCellHover: this._cellHoverHandler.bind(this),
            onWeekNumberClick: this._shouldHandleWeekNumberClick() ? this._weekNumberClickHandler.bind(this) : null
        }
    };
    _proto.selectValue = function(selectedValue, e) {
        const [startDate, endDate] = this._getValue();
        this.skipNavigate();
        this._updateCurrentDate(selectedValue);
        this._currentDateChanged = true;
        if (true === this.calendar.option("_allowChangeSelectionOrder")) {
            this.calendar._valueSelected = true;
            if ("startDate" === this.calendar.option("_currentSelection")) {
                if (this.calendar._convertToDate(selectedValue) > this.calendar._convertToDate(endDate)) {
                    this.dateValue([selectedValue, null], e)
                } else {
                    this.dateValue([selectedValue, endDate], e)
                }
            } else if (this.calendar._convertToDate(selectedValue) >= this.calendar._convertToDate(startDate)) {
                this.dateValue([startDate, selectedValue], e)
            } else {
                this.dateValue([selectedValue, null], e)
            }
        } else if (!startDate || endDate) {
            this.dateValue([selectedValue, null], e)
        } else {
            this.dateValue(startDate < selectedValue ? [startDate, selectedValue] : [selectedValue, startDate], e)
        }
    };
    _proto.updateAriaSelected = function(value, previousValue) {
        var _value, _previousValue;
        null !== (_value = value) && void 0 !== _value ? _value : value = this._getValue();
        null !== (_previousValue = previousValue) && void 0 !== _previousValue ? _previousValue : previousValue = [];
        _CalendarSelectionStr.prototype.updateAriaSelected.call(this, value, previousValue)
    };
    _proto.processValueChanged = function(value, previousValue) {
        _CalendarSelectionStr.prototype.processValueChanged.call(this, value, previousValue);
        const range = this._getRange();
        this._updateViewsOption("range", range)
    };
    _proto.getDefaultCurrentDate = function() {
        const {
            _allowChangeSelectionOrder: _allowChangeSelectionOrder,
            _currentSelection: _currentSelection
        } = this.calendar.option();
        const value = this.dateOption("value");
        if (_allowChangeSelectionOrder) {
            if ("startDate" === _currentSelection && value[0]) {
                return value[0]
            }
            if ("endDate" === _currentSelection && value[1]) {
                return value[1]
            }
        }
        const dates = value.filter(value => value);
        return this._getLowestDateInArray(dates)
    };
    _proto.restoreValue = function() {
        this.calendar.option("value", [null, null])
    };
    _proto._getValue = function() {
        const value = this.dateOption("value");
        if (!value.length) {
            return value
        }
        let [startDate, endDate] = value;
        if (startDate && endDate && startDate > endDate) {
            [startDate, endDate] = [endDate, startDate]
        }
        return [startDate, endDate]
    };
    _proto._getRange = function() {
        const [startDate, endDate] = this._getValue();
        return this._getDaysInRange(startDate, endDate)
    };
    _proto._getDaysInRange = function(startDate, endDate) {
        if (!startDate || !endDate) {
            return []
        }
        const {
            currentDate: currentDate,
            viewsCount: viewsCount
        } = this.calendar.option();
        const isAdditionalViewDate = this.calendar._isAdditionalViewDate(currentDate);
        const firstDateInViews = _date.default.getFirstMonthDate(_date.default.addDateInterval(currentDate, "month", isAdditionalViewDate ? -2 : -1));
        const lastDateInViews = _date.default.getLastMonthDate(_date.default.addDateInterval(currentDate, "month", isAdditionalViewDate ? 1 : viewsCount));
        const rangeStartDate = new Date(Math.max(firstDateInViews, startDate));
        const rangeEndDate = new Date(Math.min(lastDateInViews, endDate));
        return [..._date.default.getDatesOfInterval(rangeStartDate, rangeEndDate, 864e5), rangeEndDate]
    };
    _proto._cellHoverHandler = function(e) {
        const isMaxZoomLevel = this._isMaxZoomLevel();
        const [startDate, endDate] = this._getValue();
        const {
            _allowChangeSelectionOrder: _allowChangeSelectionOrder,
            _currentSelection: _currentSelection
        } = this.calendar.option();
        if (isMaxZoomLevel) {
            const skipHoveredRange = _allowChangeSelectionOrder && "startDate" === _currentSelection;
            if (startDate && !endDate && !skipHoveredRange) {
                if (e.value > startDate) {
                    this._updateViewsOption("hoveredRange", this._getDaysInRange(startDate, e.value));
                    return
                }
            } else if (!startDate && endDate && !(_allowChangeSelectionOrder && "endDate" === _currentSelection)) {
                if (e.value < endDate) {
                    this._updateViewsOption("hoveredRange", this._getDaysInRange(e.value, endDate));
                    return
                }
            } else if (startDate && endDate) {
                if ("startDate" === _currentSelection && e.value < startDate) {
                    this._updateViewsOption("hoveredRange", this._getDaysInRange(e.value, startDate));
                    return
                } else if ("endDate" === _currentSelection && e.value > endDate) {
                    this._updateViewsOption("hoveredRange", this._getDaysInRange(endDate, e.value));
                    return
                }
            }
            this._updateViewsOption("hoveredRange", [])
        }
    };
    _proto._weekNumberClickHandler = function(_ref) {
        let {
            rowDates: rowDates,
            event: event
        } = _ref;
        const selectedDates = rowDates.filter(date => !this._isDateDisabled(date));
        const value = selectedDates.length ? [selectedDates[0], selectedDates[selectedDates.length - 1]] : [null, null];
        this.dateValue(value, event)
    };
    return CalendarRangeSelectionStrategy
}(_uiCalendarSelection.default);
var _default = CalendarRangeSelectionStrategy;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
