/**
 * DevExtreme (cjs/__internal/ui/date_range_box/m_multiselect_date_box.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 _renderer = _interopRequireDefault(require("../../../core/renderer"));
var _size = require("../../../core/utils/size");
var _events_engine = _interopRequireDefault(require("../../../events/core/events_engine"));
var _utils = require("../../../events/utils");
var _date_box = _interopRequireDefault(require("../../../ui/date_box"));
var _m_date_range = require("./m_date_range.utils");
var _m_rangeCalendar = _interopRequireDefault(require("./strategy/m_rangeCalendar"));

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 START_DATEBOX_CLASS = "dx-start-datebox";
const TypedDateBox = _date_box.default;
let MultiselectDateBox = function(_TypedDateBox) {
    _inheritsLoose(MultiselectDateBox, _TypedDateBox);

    function MultiselectDateBox() {
        return _TypedDateBox.apply(this, arguments) || this
    }
    var _proto = MultiselectDateBox.prototype;
    _proto._initStrategy = function() {
        this._strategy = new _m_rangeCalendar.default(this)
    };
    _proto._initMarkup = function() {
        _TypedDateBox.prototype._initMarkup.call(this);
        this._renderInputClickEvent()
    };
    _proto._renderInputClickEvent = function() {
        const clickEventName = (0, _utils.addNamespace)("dxclick", this.NAME);
        _events_engine.default.off(this._input(), clickEventName);
        _events_engine.default.on(this._input(), clickEventName, e => {
            this._processValueChange(e)
        })
    };
    _proto._applyButtonHandler = function(_ref) {
        let {
            event: event
        } = _ref;
        const strategy = this.getStrategy();
        const value = strategy.getValue();
        strategy.getDateRangeBox().updateValue(value, event);
        this.close();
        this.option("focusStateEnabled") && this.focus()
    };
    _proto._openHandler = function(e) {
        if (this.getStrategy().getDateRangeBox().option("opened")) {
            return
        }
        _TypedDateBox.prototype._openHandler.call(this, e)
    };
    _proto._renderOpenedState = function() {
        const {
            opened: opened
        } = this.option();
        this._getDateRangeBox().option("opened", opened);
        if (this._isStartDateBox()) {
            if (opened) {
                this._createPopup()
            }
            this._getDateRangeBox()._popupContentIdentifier(this._getControlsAria());
            this._setPopupOption("visible", opened);
            this._getDateRangeBox()._setAriaAttributes()
        }
    };
    _proto._getDateRangeBox = function() {
        return this.getStrategy().getDateRangeBox()
    };
    _proto._isStartDateBox = function() {
        return (0, _renderer.default)(this.element()).hasClass("dx-start-datebox")
    };
    _proto._renderPopup = function() {
        _TypedDateBox.prototype._renderPopup.call(this);
        if (this._isStartDateBox()) {
            this._getDateRangeBox()._bindInnerWidgetOptions(this._popup, "dropDownOptions")
        }
    };
    _proto._popupShownHandler = function() {
        var _a;
        _TypedDateBox.prototype._popupShownHandler.call(this);
        null === (_a = this._getDateRangeBox()._validationMessage) || void 0 === _a ? void 0 : _a.option("positionSide", this._getValidationMessagePositionSide())
    };
    _proto._popupHiddenHandler = function() {
        var _a;
        _TypedDateBox.prototype._popupHiddenHandler.call(this);
        null === (_a = this._getDateRangeBox()._validationMessage) || void 0 === _a ? void 0 : _a.option("positionSide", this._getValidationMessagePositionSide())
    };
    _proto._focusInHandler = function(e) {
        _TypedDateBox.prototype._focusInHandler.call(this, e);
        this._processValueChange(e)
    };
    _proto._popupTabHandler = function(e) {
        const $element = (0, _renderer.default)(e.target);
        if (e.shiftKey && $element.is(this._getFirstPopupElement())) {
            this._getDateRangeBox().getEndDateBox().focus();
            e.preventDefault()
        }
        if (!e.shiftKey && $element.is(this._getLastPopupElement())) {
            this._getDateRangeBox().getStartDateBox().focus();
            e.preventDefault()
        }
    };
    _proto._processValueChange = function(e) {
        const {
            target: target
        } = e;
        const dateRangeBox = this._getDateRangeBox();
        const [startDateInput, endDateInput] = dateRangeBox.field();
        if ((0, _renderer.default)(target).is((0, _renderer.default)(startDateInput))) {
            dateRangeBox.option("_currentSelection", "startDate")
        }
        if ((0, _renderer.default)(target).is((0, _renderer.default)(endDateInput))) {
            dateRangeBox.option("_currentSelection", "endDate")
        }
        if (!dateRangeBox.getStartDateBox().getStrategy().getWidget()) {
            return
        }
        const calendar = dateRangeBox.getStartDateBox().getStrategy().getWidget();
        const {
            value: value
        } = calendar.option();
        const startDate = (0, _m_date_range.getDeserializedDate)(null === value || void 0 === value ? void 0 : value[0]);
        const endDate = (0, _m_date_range.getDeserializedDate)(null === value || void 0 === value ? void 0 : value[1]);
        if ((0, _renderer.default)(target).is((0, _renderer.default)(startDateInput))) {
            if (startDate) {
                calendar._skipNavigate = true;
                calendar.option("currentDate", startDate)
            }
            this.getStrategy().setActiveStartDateBox();
            calendar.option("_currentSelection", "startDate");
            if (dateRangeBox.option("disableOutOfRangeSelection")) {
                calendar._setViewsMaxOption(endDate)
            }
        }
        if ((0, _renderer.default)(target).is((0, _renderer.default)(endDateInput))) {
            if (endDate) {
                if (startDate && (0, _m_date_range.monthDifference)(startDate, endDate) > 1) {
                    calendar.option("currentDate", calendar._getDateByOffset(null, endDate));
                    calendar.option("currentDate", calendar._getDateByOffset(-1, endDate))
                }
                calendar._skipNavigate = true;
                calendar.option("currentDate", endDate)
            }
            dateRangeBox.getStartDateBox().getStrategy().setActiveEndDateBox();
            calendar.option("_currentSelection", "endDate");
            if (dateRangeBox.option("disableOutOfRangeSelection")) {
                calendar._setViewsMinOption(startDate)
            }
        }
    };
    _proto._invalidate = function() {
        _TypedDateBox.prototype._invalidate.call(this);
        this._refreshStrategy()
    };
    _proto._updateInternalValidationState = function(isValid, validationMessage) {
        this.option({
            isValid: isValid,
            validationError: isValid ? null : {
                message: validationMessage
            }
        })
    };
    _proto._recallInternalValidation = function(value) {
        this._applyInternalValidation(value)
    };
    _proto._isTargetOutOfComponent = function(target) {
        const $dateRangeBox = (0, _renderer.default)(this._getDateRangeBox().element());
        const isTargetOutOfDateRangeBox = 0 === (0, _renderer.default)(target).closest($dateRangeBox).length;
        return _TypedDateBox.prototype._isTargetOutOfComponent.call(this, target) && isTargetOutOfDateRangeBox
    };
    _proto._updateLabelWidth = function() {
        const $beforeButtonsContainer = this._getDateRangeBox()._$beforeButtonsContainer;
        const {
            labelMode: labelMode
        } = this.option();
        if ("outside" === labelMode && $beforeButtonsContainer && this._isStartDateBox()) {
            this._label._updateLabelTransform((0, _size.getWidth)($beforeButtonsContainer));
            return
        }
        _TypedDateBox.prototype._updateLabelWidth.call(this)
    };
    _proto._optionChanged = function(args) {
        switch (args.name) {
            case "isValid": {
                const isValid = this._getDateRangeBox().option("isValid");
                if (this._skipIsValidOptionChange || isValid === args.value) {
                    _TypedDateBox.prototype._optionChanged.call(this, args);
                    return
                }
                this._skipIsValidOptionChange = true;
                this.option({
                    isValid: isValid
                });
                this._skipIsValidOptionChange = false;
                break
            }
            default:
                _TypedDateBox.prototype._optionChanged.call(this, args)
        }
    };
    _proto.close = function() {
        this.getStrategy().getDateRangeBox().getStartDateBox().option("opened", false)
    };
    _proto.getStrategy = function() {
        return this._strategy
    };
    return MultiselectDateBox
}(TypedDateBox);
var _default = MultiselectDateBox;
exports.default = _default;
