/**
 * DevExtreme (esm/__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/
 */
import _extends from "@babel/runtime/helpers/esm/extends";
import {
    extend
} from "../../../../core/utils/extend";
import {
    isFunction
} from "../../../../core/utils/type";
import eventsEngine from "../../../../events/core/events_engine";
import CalendarStrategy from "../../date_box/m_date_box.strategy.calendar";
import {
    getDeserializedDate,
    isSameDateArrays,
    isSameDates
} from "../m_date_range.utils";
class RangeCalendarStrategy extends CalendarStrategy {
    constructor(dateBox) {
        super();
        this._dateSelectedCounter = 0;
        this.dateBox = dateBox;
        this.dateRangeBox = dateBox.option("_dateRangeBoxInstance")
    }
    popupConfig(popupConfig) {
        return extend(true, super.popupConfig(popupConfig), {
            position: {
                of: this.getDateRangeBox().$element()
            }
        })
    }
    popupShowingHandler() {
        this.getWidget()._restoreViewsMinMaxOptions();
        this._dateSelectedCounter = 0
    }
    _getPopup() {
        return super._getPopup() || this.getDateRangeBox().getStartDateBox()._popup
    }
    supportedKeys() {
        var dateRangeBox = this.getDateRangeBox();
        return _extends(_extends({}, super.supportedKeys()), {
            rightArrow: () => {
                if (dateRangeBox.option("opened")) {
                    return true
                }
                return
            },
            leftArrow: () => {
                if (dateRangeBox.option("opened")) {
                    return true
                }
                return
            },
            enter: e => {
                if (dateRangeBox.option("opened")) {
                    var dateBoxValue = this.dateBox.dateOption("value");
                    this.dateBox._valueChangeEventHandler(e);
                    var newDateBoxValue = this.dateBox.dateOption("value");
                    var dateBoxValueChanged = !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
                }
                var $focusableElement = e.shiftKey ? dateRangeBox.getStartDateBox()._getLastPopupElement() : dateRangeBox.getStartDateBox()._getFirstPopupElement();
                if ($focusableElement) {
                    eventsEngine.trigger($focusableElement, "focus");
                    $focusableElement.select()
                }
                e.preventDefault()
            }
        })
    }
    _getWidgetOptions() {
        var {
            disabledDates: disabledDatesValue,
            value: value,
            multiView: multiView
        } = this.dateRangeBox.option();
        var disabledDates = isFunction(disabledDatesValue) ? this._injectComponent(disabledDatesValue) : null !== disabledDatesValue && void 0 !== disabledDatesValue ? disabledDatesValue : void 0;
        return extend(super._getWidgetOptions(), {
            disabledDates: disabledDates,
            value: value,
            selectionMode: "range",
            viewsCount: multiView ? 2 : 1,
            _allowChangeSelectionOrder: true,
            _currentSelection: this.getCurrentSelection()
        })
    }
    _refreshActiveDescendant(e) {
        this.getDateRangeBox().setAria("activedescendant", e.actionValue)
    }
    _injectComponent(func) {
        return params => func(extend(params, {
            component: this.getDateRangeBox()
        }))
    }
    getKeyboardListener() {
        var dateRangeBox = this.getDateRangeBox();
        return dateRangeBox.getStartDateBox() ? dateRangeBox.getStartDateBox().getStrategy().getWidget() : this.getWidget()
    }
    getValue() {
        return this.getWidget().option("value")
    }
    _updateValue() {
        var {
            value: value
        } = this.getDateRangeBox().option();
        if (!this.getWidget()) {
            return
        }
        this._shouldPreventFocusChange = true;
        this.getWidget().option("value", value)
    }
    _isInstantlyMode() {
        return "instantly" === this.getDateRangeBox().option("applyValueMode")
    }
    _valueChangedHandler(_ref) {
        var {
            value: value,
            previousValue: previousValue,
            event: event
        } = _ref;
        if (isSameDateArrays(value, previousValue) && !this.getWidget()._valueSelected) {
            this._shouldPreventFocusChange = false;
            return
        }
        this.getWidget()._valueSelected = false;
        var 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 (getDeserializedDate(value[0]) > 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] && getDeserializedDate(value[0]) > getDeserializedDate(value[1])) {
                return
            }
        }
        if (!this._shouldPreventFocusChange) {
            this._moveFocusToNextInput()
        }
        this._shouldPreventFocusChange = false
    }
    _moveFocusToNextInput() {
        var targetDateBox = "startDate" === this._getCalendarCurrentSelection() ? this.getDateRangeBox().getEndDateBox() : this.getDateRangeBox().getStartDateBox();
        targetDateBox.focus();
        eventsEngine.trigger(targetDateBox.field(), "dxclick")
    }
    getCurrentSelection() {
        return this.getDateRangeBox().option("_currentSelection")
    }
    _getCalendarCurrentSelection() {
        return this.getWidget().option("_currentSelection")
    }
    _closeDropDownByEnter() {
        if ("startDate" === this._getCalendarCurrentSelection()) {
            return false
        }
        return true
    }
    dateBoxValue() {
        var {
            dateBox: dateBox
        } = this;
        if (arguments.length) {
            return dateBox.dateValue.apply(dateBox, arguments)
        }
        return dateBox.dateOption.apply(dateBox, ["value"])
    }
    _cellClickHandler() {}
    setActiveStartDateBox() {
        this.dateBox = this.getDateRangeBox().getStartDateBox()
    }
    setActiveEndDateBox() {
        this.dateBox = this.getDateRangeBox().getEndDateBox()
    }
    getDateRangeBox() {
        return this.dateRangeBox
    }
    getWidget() {
        return this._widget
    }
}
export default RangeCalendarStrategy;
