/**
 * DevExtreme (cjs/__internal/ui/date_range_box/strategy/m_rangeCalendar.js)
 * Version: 23.2.9 (build 24294-0112)
 * Build date: Sun Oct 20 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = void 0;
var _extend = require("../../../../core/utils/extend");
var _type = require("../../../../core/utils/type");
var _events_engine = _interopRequireDefault(require("../../../../events/core/events_engine"));
var _m_date_boxStrategy = _interopRequireDefault(require("../../date_box/m_date_box.strategy.calendar"));
var _m_date_range = require("../m_date_range.utils");

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

function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}

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)
}
let RangeCalendarStrategy = function(_CalendarStrategy) {
    _inheritsLoose(RangeCalendarStrategy, _CalendarStrategy);

    function RangeCalendarStrategy(dateBox) {
        var _this;
        _this = _CalendarStrategy.call(this) || this;
        _this._dateSelectedCounter = 0;
        _this.dateBox = dateBox;
        _this.dateRangeBox = dateBox.option("_dateRangeBoxInstance");
        return _this
    }
    var _proto = RangeCalendarStrategy.prototype;
    _proto.popupConfig = function(_popupConfig) {
        return (0, _extend.extend)(true, _CalendarStrategy.prototype.popupConfig.call(this, _popupConfig), {
            position: {
                of: this.getDateRangeBox().$element()
            }
        })
    };
    _proto.popupShowingHandler = function() {
        this.getWidget()._restoreViewsMinMaxOptions();
        this._dateSelectedCounter = 0
    };
    _proto._getPopup = function() {
        return _CalendarStrategy.prototype._getPopup.call(this) || this.getDateRangeBox().getStartDateBox()._popup
    };
    _proto.supportedKeys = function() {
        const dateRangeBox = this.getDateRangeBox();
        return _extends(_extends({}, _CalendarStrategy.prototype.supportedKeys.call(this)), {
            rightArrow: () => {
                if (dateRangeBox.option("opened")) {
                    return true
                }
                return
            },
            leftArrow: () => {
                if (dateRangeBox.option("opened")) {
                    return true
                }
                return
            },
            enter: e => {
                if (dateRangeBox.option("opened")) {
                    const dateBoxValue = this.dateBox.dateOption("value");
                    this.dateBox._valueChangeEventHandler(e);
                    const newDateBoxValue = this.dateBox.dateOption("value");
                    const dateBoxValueChanged = !(0, _m_date_range.isSameDates)(dateBoxValue, newDateBoxValue);
                    if (dateBoxValueChanged) {
                        dateRangeBox.getStartDateBox().getStrategy().getWidget().option("value", dateRangeBox.option("value"))
                    } else {
                        dateRangeBox.getStartDateBox().getStrategy().getWidget()._enterKeyHandler(e)
                    }
                    return false
                }
                return
            },
            tab: e => {
                if (!dateRangeBox.option("opened")) {
                    return
                }
                if (!this._getPopup().getFocusableElements().length) {
                    if (!e.shiftKey && dateRangeBox._isEndDateActiveElement() || e.shiftKey && dateRangeBox._isStartDateActiveElement()) {
                        dateRangeBox.close()
                    }
                    return
                }
                if (!e.shiftKey && dateRangeBox._isStartDateActiveElement() || e.shiftKey && dateRangeBox._isEndDateActiveElement()) {
                    return
                }
                const $focusableElement = e.shiftKey ? dateRangeBox.getStartDateBox()._getLastPopupElement() : dateRangeBox.getStartDateBox()._getFirstPopupElement();
                if ($focusableElement) {
                    _events_engine.default.trigger($focusableElement, "focus");
                    $focusableElement.select()
                }
                e.preventDefault()
            }
        })
    };
    _proto._getWidgetOptions = function() {
        const {
            disabledDates: disabledDatesValue,
            value: value,
            multiView: multiView
        } = this.dateRangeBox.option();
        const disabledDates = (0, _type.isFunction)(disabledDatesValue) ? this._injectComponent(disabledDatesValue) : null !== disabledDatesValue && void 0 !== disabledDatesValue ? disabledDatesValue : void 0;
        return (0, _extend.extend)(_CalendarStrategy.prototype._getWidgetOptions.call(this), {
            disabledDates: disabledDates,
            value: value,
            selectionMode: "range",
            viewsCount: multiView ? 2 : 1,
            _allowChangeSelectionOrder: true,
            _currentSelection: this.getCurrentSelection()
        })
    };
    _proto._refreshActiveDescendant = function(e) {
        this.getDateRangeBox().setAria("activedescendant", e.actionValue)
    };
    _proto._injectComponent = function(func) {
        return params => func((0, _extend.extend)(params, {
            component: this.getDateRangeBox()
        }))
    };
    _proto.getKeyboardListener = function() {
        const dateRangeBox = this.getDateRangeBox();
        return dateRangeBox.getStartDateBox() ? dateRangeBox.getStartDateBox().getStrategy().getWidget() : this.getWidget()
    };
    _proto.getValue = function() {
        return this.getWidget().option("value")
    };
    _proto._updateValue = function() {
        const {
            value: value
        } = this.getDateRangeBox().option();
        if (!this.getWidget()) {
            return
        }
        this._shouldPreventFocusChange = true;
        this.getWidget().option("value", value)
    };
    _proto._isInstantlyMode = function() {
        return "instantly" === this.getDateRangeBox().option("applyValueMode")
    };
    _proto._valueChangedHandler = function(_ref) {
        let {
            value: value,
            previousValue: previousValue,
            event: event
        } = _ref;
        if ((0, _m_date_range.isSameDateArrays)(value, previousValue) && !this.getWidget()._valueSelected) {
            this._shouldPreventFocusChange = false;
            return
        }
        this.getWidget()._valueSelected = false;
        const dateRangeBox = this.getDateRangeBox();
        if (this._isInstantlyMode()) {
            if (!dateRangeBox.option("disableOutOfRangeSelection")) {
                if ("startDate" === this._getCalendarCurrentSelection()) {
                    this._dateSelectedCounter = 0
                } else {
                    this._dateSelectedCounter = 1;
                    if (!value[0]) {
                        this._dateSelectedCounter = -1
                    } else if ((0, _m_date_range.getDeserializedDate)(value[0]) > (0, _m_date_range.getDeserializedDate)(value[1])) {
                        dateRangeBox.updateValue([value[0], null], event);
                        return
                    }
                }
            }
            dateRangeBox.updateValue(value, event);
            this._dateSelectedCounter += 1;
            if (2 === this._dateSelectedCounter) {
                dateRangeBox.close();
                return
            }
        } else if ("endDate" === this._getCalendarCurrentSelection()) {
            if (value[0] && (0, _m_date_range.getDeserializedDate)(value[0]) > (0, _m_date_range.getDeserializedDate)(value[1])) {
                return
            }
        }
        if (!this._shouldPreventFocusChange) {
            this._moveFocusToNextInput()
        }
        this._shouldPreventFocusChange = false
    };
    _proto._moveFocusToNextInput = function() {
        const targetDateBox = "startDate" === this._getCalendarCurrentSelection() ? this.getDateRangeBox().getEndDateBox() : this.getDateRangeBox().getStartDateBox();
        targetDateBox.focus();
        _events_engine.default.trigger(targetDateBox.field(), "dxclick")
    };
    _proto.getCurrentSelection = function() {
        return this.getDateRangeBox().option("_currentSelection")
    };
    _proto._getCalendarCurrentSelection = function() {
        return this.getWidget().option("_currentSelection")
    };
    _proto._closeDropDownByEnter = function() {
        if ("startDate" === this._getCalendarCurrentSelection()) {
            return false
        }
        return true
    };
    _proto.dateBoxValue = function() {
        const {
            dateBox: dateBox
        } = this;
        if (arguments.length) {
            return dateBox.dateValue.apply(dateBox, arguments)
        }
        return dateBox.dateOption.apply(dateBox, ["value"])
    };
    _proto._cellClickHandler = function() {};
    _proto.setActiveStartDateBox = function() {
        this.dateBox = this.getDateRangeBox().getStartDateBox()
    };
    _proto.setActiveEndDateBox = function() {
        this.dateBox = this.getDateRangeBox().getEndDateBox()
    };
    _proto.getDateRangeBox = function() {
        return this.dateRangeBox
    };
    _proto.getWidget = function() {
        return this._widget
    };
    return RangeCalendarStrategy
}(_m_date_boxStrategy.default);
var _default = RangeCalendarStrategy;
exports.default = _default;
