"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "useKeyNavigation", {
    enumerable: true,
    get: function() {
        return useKeyNavigation;
    }
});
var _react = require("react");
var _Calendartypes = require("../Calendar.types");
var _utils = require("../utils");
var _reducer = require("../store/reducer");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
/**
 * Метод для получения стороны двойного календаря.
 */ var getDoubleCalendarSide = function getDoubleCalendarSide(currentIndex, calendarState) {
    if (currentIndex >= 0 && currentIndex < _utils.offsetMap[calendarState]) {
        return 'first';
    }
    if (currentIndex >= _utils.offsetMap[calendarState] && currentIndex < _utils.offsetMap[calendarState] * 2) {
        return 'second';
    }
    return '';
};
var isOutOfBound = function isOutOfBound(param, rowSize, columnSize) {
    var _param = _sliced_to_array(param, 2), rowIndex = _param[0], columnIndex = _param[1];
    return columnIndex === -1 || columnIndex === columnSize + 1 || rowIndex === -1 || rowIndex === rowSize + 1;
};
var isVisible = function isVisible(refs, row, column) {
    var _refs_current_row, _refs_current;
    return (_refs_current = refs.current) === null || _refs_current === void 0 ? void 0 : (_refs_current_row = _refs_current[row]) === null || _refs_current_row === void 0 ? void 0 : _refs_current_row[column];
};
var isAriaDisableItem = function isAriaDisableItem(item) {
    return (item === null || item === void 0 ? void 0 : item.getAttribute('aria-disabled')) === 'true';
};
var hasDisabledArrowKey = function hasDisabledArrowKey(param) {
    var refs = param.refs, payload = param.payload, key = param.key;
    var _refs_current_previousRowIndex_previousColumnIndex, _refs_current_previousRowIndex, _refs_current;
    var _payload = _sliced_to_array(payload, 2), previousRowIndex = _payload[0], previousColumnIndex = _payload[1];
    var disabledArrowKey = (_refs_current = refs.current) === null || _refs_current === void 0 ? void 0 : (_refs_current_previousRowIndex = _refs_current[previousRowIndex]) === null || _refs_current_previousRowIndex === void 0 ? void 0 : (_refs_current_previousRowIndex_previousColumnIndex = _refs_current_previousRowIndex[previousColumnIndex]) === null || _refs_current_previousRowIndex_previousColumnIndex === void 0 ? void 0 : _refs_current_previousRowIndex_previousColumnIndex.dataset.disabledArrowKey;
    return Boolean(disabledArrowKey === null || disabledArrowKey === void 0 ? void 0 : disabledArrowKey.includes(key));
};
var hasDisabledMonths = function hasDisabledMonths(param) {
    var item = param.item, key = param.key;
    var _item_dataset, _item_dataset1;
    return (item === null || item === void 0 ? void 0 : (_item_dataset = item.dataset) === null || _item_dataset === void 0 ? void 0 : _item_dataset.disabledMonths) ? item === null || item === void 0 ? void 0 : (_item_dataset1 = item.dataset) === null || _item_dataset1 === void 0 ? void 0 : _item_dataset1.disabledMonths.includes(key) : false;
};
var getNextCorrectPosition = function getNextCorrectPosition(param) {
    var refs = param.refs, rowSize = param.rowSize, newRowIndex = param.newRowIndex, newColumnIndex = param.newColumnIndex, columnSize = param.columnSize, minColumnIndex = param.minColumnIndex, _param_defaultState = param.defaultState, defaultState = _param_defaultState === void 0 ? [] : _param_defaultState;
    var _refs_current_newRowIndex, _refs_current;
    var item = (_refs_current = refs.current) === null || _refs_current === void 0 ? void 0 : (_refs_current_newRowIndex = _refs_current[newRowIndex]) === null || _refs_current_newRowIndex === void 0 ? void 0 : _refs_current_newRowIndex[newColumnIndex];
    while(isAriaDisableItem(item) && newColumnIndex <= columnSize){
        var _refs_current_newRowIndex1, _refs_current1;
        newColumnIndex++;
        if (newColumnIndex > columnSize && newRowIndex < rowSize) {
            newRowIndex++;
            newColumnIndex = minColumnIndex;
        }
        item = (_refs_current1 = refs.current) === null || _refs_current1 === void 0 ? void 0 : (_refs_current_newRowIndex1 = _refs_current1[newRowIndex]) === null || _refs_current_newRowIndex1 === void 0 ? void 0 : _refs_current_newRowIndex1[newColumnIndex];
        if (item) {
            var isDisabledArrowDown = hasDisabledArrowKey({
                refs: refs,
                payload: [
                    newRowIndex,
                    newColumnIndex
                ],
                key: 'down'
            });
            var isDisabledArrowRight = hasDisabledArrowKey({
                refs: refs,
                payload: [
                    newRowIndex,
                    newColumnIndex
                ],
                key: 'right'
            });
            if (isDisabledArrowDown || isDisabledArrowRight) {
                return defaultState;
            }
        }
    }
    return [
        newRowIndex,
        newColumnIndex
    ];
};
var getPreviousCorrectPosition = function getPreviousCorrectPosition(param) {
    var refs = param.refs, rowSize = param.rowSize, newRowIndex = param.newRowIndex, newColumnIndex = param.newColumnIndex, columnSize = param.columnSize, minColumnIndex = param.minColumnIndex, _param_defaultState = param.defaultState, defaultState = _param_defaultState === void 0 ? [] : _param_defaultState;
    var _refs_current_newRowIndex, _refs_current;
    var item = (_refs_current = refs.current) === null || _refs_current === void 0 ? void 0 : (_refs_current_newRowIndex = _refs_current[newRowIndex]) === null || _refs_current_newRowIndex === void 0 ? void 0 : _refs_current_newRowIndex[newColumnIndex];
    while(isAriaDisableItem(item) && newColumnIndex >= minColumnIndex){
        var _refs_current_newRowIndex1, _refs_current1;
        newColumnIndex--;
        if (newColumnIndex < minColumnIndex && newRowIndex <= rowSize) {
            newRowIndex--;
            newColumnIndex = columnSize;
        }
        item = (_refs_current1 = refs.current) === null || _refs_current1 === void 0 ? void 0 : (_refs_current_newRowIndex1 = _refs_current1[newRowIndex]) === null || _refs_current_newRowIndex1 === void 0 ? void 0 : _refs_current_newRowIndex1[newColumnIndex];
        if (item && item.dataset.day === '1') {
            var isDisabledArrowUp = hasDisabledArrowKey({
                refs: refs,
                payload: [
                    newRowIndex,
                    newColumnIndex
                ],
                key: 'up'
            });
            var isDisabledArrowLeft = hasDisabledArrowKey({
                refs: refs,
                payload: [
                    newRowIndex,
                    newColumnIndex
                ],
                key: 'left'
            });
            if (isDisabledArrowUp || isDisabledArrowLeft) {
                return defaultState;
            }
        }
    }
    return [
        newRowIndex,
        newColumnIndex
    ];
};
/**
 * Метод для нахождения стартового индекса дня в следующем/предыдущем месяце
 */ function getCorrectColumnIndex(param) {
    var refs = param.refs, rowSize = param.rowSize, isNext = param.isNext;
    if (isNext) {
        var index = refs[rowSize].filter(Boolean).length;
        if (!index) {
            index = refs[rowSize - 1].filter(Boolean).length;
        }
        return index;
    }
    // Смещение влево - начинаем поиск первого не-nullable элемента в первой строке
    return refs[0].findIndex(function(item) {
        return item;
    }) - 1;
}
var getCorrectIndexes = function getCorrectIndexes(refs, param, rowSize, columnSize, withShift, defaultState, calendarState) {
    var _param = _sliced_to_array(param, 2), rowIndex = _param[0], columnIndex = _param[1];
    var _refs_current_newRowIndex, _refs_current;
    var newRowIndex = rowIndex;
    var newColumnIndex = columnIndex;
    var minRowIndex = newRowIndex + 1;
    var maxRowIndex = newRowIndex - 1;
    var minColumnIndex = 0;
    var maxColumnIndex = columnSize;
    if (newColumnIndex === minColumnIndex - 1) {
        newColumnIndex += 1;
        while(newColumnIndex < maxColumnIndex && !isVisible(refs, newRowIndex, newColumnIndex)){
            newColumnIndex++;
        }
    }
    if (newColumnIndex === columnSize + 1) {
        newColumnIndex -= 1;
        while(newColumnIndex > minColumnIndex && !isVisible(refs, newRowIndex, newColumnIndex)){
            newColumnIndex--;
        }
    }
    if (newRowIndex === minColumnIndex - 1) {
        newRowIndex = _utils.offsetMap[calendarState] - 1;
        while(newRowIndex > minRowIndex && !isVisible(refs, newRowIndex, newColumnIndex)){
            newRowIndex--;
        }
    }
    if (newRowIndex === rowSize + 1) {
        newRowIndex = rowSize + 1 - _utils.offsetMap[calendarState];
        while(newRowIndex <= maxRowIndex && !isVisible(refs, newRowIndex, newColumnIndex)){
            newRowIndex++;
        }
    }
    // INFO: Логика для получения правильной позиции дня, когда переключились на другой месяц
    // INFO: и ставим указатель на первый доступный день. Только при зажатой клавиши Shift.
    if (isAriaDisableItem((_refs_current = refs.current) === null || _refs_current === void 0 ? void 0 : (_refs_current_newRowIndex = _refs_current[newRowIndex]) === null || _refs_current_newRowIndex === void 0 ? void 0 : _refs_current_newRowIndex[newColumnIndex]) && withShift) {
        var isNext = rowIndex === rowSize + 1;
        var isPrevious = rowIndex === minColumnIndex - 1;
        var state = {
            refs: refs,
            rowSize: rowSize,
            newColumnIndex: newColumnIndex,
            minColumnIndex: minColumnIndex,
            columnSize: columnSize,
            newRowIndex: newRowIndex,
            defaultState: defaultState
        };
        if (isNext) {
            return getNextCorrectPosition(state);
        }
        if (isPrevious) {
            return getPreviousCorrectPosition(state);
        }
    }
    return [
        newRowIndex,
        newColumnIndex
    ];
};
var useKeyNavigation = function useKeyNavigation(param) {
    var _param_isDouble = param.isDouble, isDouble = _param_isDouble === void 0 ? false : _param_isDouble, size = param.size, onPrev = param.onPrev, onNext = param.onNext, calendarState = param.calendarState;
    var _size = _sliced_to_array(size, 2), rowSize = _size[0], columnSize = _size[1];
    var _useState = _sliced_to_array((0, _react.useState)([
        0,
        0
    ]), 2), selectIndexes = _useState[0], setSelectIndexes = _useState[1];
    var _useState1 = _sliced_to_array((0, _react.useState)(false), 2), isOutOfMinMaxRange = _useState1[0], setIsOutOfMinMaxRange = _useState1[1];
    var withShiftState = (0, _react.useRef)(false);
    var currentIndexes = (0, _react.useRef)([
        0,
        0
    ]);
    var outerRefs = (0, _react.useRef)(Array(rowSize + 1).fill(0).map(function() {
        return Array(columnSize + 1);
    }));
    (0, _react.useLayoutEffect)(function() {
        if (!isOutOfBound(selectIndexes, rowSize, columnSize)) {
            return;
        }
        if (withShiftState.current) {
            var isNext = selectIndexes[0] === rowSize + 1;
            var refs = outerRefs.current;
            var isSecond = isDouble && isNext;
            var isFirst = isDouble && !isNext;
            var firstEndIndex = Math.floor(_reducer.sizeMap[calendarState].double[0] / 2);
            // Определяем какую часть сдвоенного календаря взять
            if (isFirst) {
                refs = outerRefs.current.slice(0, firstEndIndex);
            } else if (isSecond) {
                refs = outerRefs.current.slice(firstEndIndex, _reducer.sizeMap[calendarState].double[1]);
            }
            var refsList = refs === null || refs === void 0 ? void 0 : refs.flatMap(function(items) {
                return items.filter(Boolean);
            });
            // Если в месяце нет хотя бы одной none-disabled даты значит весь месяц выключен.
            var hasSomeEnabledDay = refsList.some(function(item) {
                return !isAriaDisableItem(item);
            });
            var isDisabledNextMonth = hasDisabledMonths({
                item: refsList[refsList.length - 1],
                key: 'next'
            });
            var isDisabledPreviousMonth = hasDisabledMonths({
                item: refsList[0],
                key: 'previous'
            });
            // Если следующий/предыдущий месяц находится за границами мин/макс, то переходить на него
            // с текущего выключенного, нет смысла.
            if (!hasSomeEnabledDay && (isDisabledNextMonth || isDisabledPreviousMonth)) {
                var inverseHandle = isNext ? onPrev : onNext;
                inverseHandle();
                setSelectIndexes(currentIndexes.current);
                // Если индексы равны, значит мы остались на текущей дате по причине, того
                // что в следующем(-их)/предыдущем(-их) месяце нет доступных дат
                setIsOutOfMinMaxRange(true);
                return;
            }
            if (!hasSomeEnabledDay) {
                var handle = isNext ? onNext : onPrev;
                var startColumnIndex = getCorrectColumnIndex({
                    refs: refs,
                    rowSize: isDouble ? refs.length - 1 : rowSize,
                    isNext: isNext
                });
                handle();
                setSelectIndexes([
                    selectIndexes[0],
                    startColumnIndex
                ]);
                return;
            }
        }
        var _getCorrectIndexes = _sliced_to_array(getCorrectIndexes(outerRefs, selectIndexes, rowSize, columnSize, withShiftState.current, currentIndexes.current, calendarState), 2), newRowIndex = _getCorrectIndexes[0], newColumnIndex = _getCorrectIndexes[1];
        if (selectIndexes[0] === newRowIndex && selectIndexes[1] === newColumnIndex) {
            return;
        }
        /**
         * Изменение состояния необходимо сделать здесь, т.к.
         * требуется дождаться обновление DOM и outerRefs
         */ setSelectIndexes([
            newRowIndex,
            newColumnIndex
        ]);
    }, [
        onPrev,
        onNext,
        selectIndexes,
        rowSize,
        columnSize,
        withShiftState,
        currentIndexes,
        isDouble
    ]);
    (0, _react.useLayoutEffect)(function() {
        var _outerRefs_current_rowIndex, _outerRefs_current;
        var _selectIndexes = _sliced_to_array(selectIndexes, 2), rowIndex = _selectIndexes[0], columnIndex = _selectIndexes[1];
        var item = outerRefs === null || outerRefs === void 0 ? void 0 : (_outerRefs_current = outerRefs.current) === null || _outerRefs_current === void 0 ? void 0 : (_outerRefs_current_rowIndex = _outerRefs_current[rowIndex]) === null || _outerRefs_current_rowIndex === void 0 ? void 0 : _outerRefs_current_rowIndex[columnIndex];
        if (item) {
            item.focus();
        }
    }, [
        selectIndexes,
        outerRefs
    ]);
    var onKeyDown = (0, _react.useCallback)(function(event) {
        setIsOutOfMinMaxRange(false);
        var keyCode = event.keyCode, withShift = event.shiftKey;
        var _selectIndexes = _sliced_to_array(selectIndexes, 2), currentRowIndex = _selectIndexes[0], currentColumnIndex = _selectIndexes[1];
        var newRowIndex = currentRowIndex;
        var newColumnIndex = currentColumnIndex;
        var minColumnIndex = 0;
        var minRowIndex = 0;
        var prevRowIndex = currentRowIndex - 1;
        var nextRowIndex = currentRowIndex + 1;
        var prevColumnIndex = currentColumnIndex - 1;
        var nextColumnIndex = currentColumnIndex + 1;
        currentIndexes.current = [
            currentRowIndex,
            currentColumnIndex
        ];
        var positionState = {
            refs: outerRefs,
            rowSize: rowSize,
            columnSize: columnSize,
            minColumnIndex: minColumnIndex,
            defaultState: [
                currentRowIndex,
                currentColumnIndex
            ]
        };
        switch(keyCode){
            case _Calendartypes.Keys.pageUp:
                {
                    event.preventDefault();
                    var isDisabledPreviousMonth = hasDisabledMonths({
                        item: outerRefs.current[currentRowIndex][currentColumnIndex],
                        key: 'previous'
                    });
                    if (isDisabledPreviousMonth) {
                        setIsOutOfMinMaxRange(true);
                        break;
                    }
                    onPrev(withShift);
                    break;
                }
            case _Calendartypes.Keys.pageDown:
                {
                    event.preventDefault();
                    var isDisabledNextMonth = hasDisabledMonths({
                        item: outerRefs.current[currentRowIndex][currentColumnIndex],
                        key: 'next'
                    });
                    if (isDisabledNextMonth) {
                        setIsOutOfMinMaxRange(true);
                        break;
                    }
                    onNext(withShift);
                    break;
                }
            case _Calendartypes.Keys.home:
                {
                    event.preventDefault();
                    newColumnIndex = minColumnIndex;
                    if (isVisible(outerRefs, newRowIndex, newColumnIndex)) {
                        break;
                    }
                    newColumnIndex = minColumnIndex - 1;
                    break;
                }
            case _Calendartypes.Keys.end:
                {
                    event.preventDefault();
                    newColumnIndex = columnSize;
                    if (isVisible(outerRefs, newRowIndex, newColumnIndex)) {
                        break;
                    }
                    newColumnIndex = columnSize + 1;
                    break;
                }
            case _Calendartypes.Keys.left:
                {
                    event.preventDefault();
                    newRowIndex = prevColumnIndex < minColumnIndex ? prevRowIndex : currentRowIndex;
                    newColumnIndex = prevColumnIndex < minColumnIndex ? columnSize : prevColumnIndex;
                    withShiftState.current = withShift;
                    var isCurrentDateDisabledArrowLeft = hasDisabledArrowKey({
                        refs: outerRefs,
                        payload: [
                            currentRowIndex,
                            currentColumnIndex
                        ],
                        key: 'left'
                    });
                    setIsOutOfMinMaxRange(isCurrentDateDisabledArrowLeft);
                    if (isCurrentDateDisabledArrowLeft) {
                        newRowIndex = currentRowIndex;
                        newColumnIndex = currentColumnIndex;
                        break;
                    }
                    // INFO: Для навигации только по доступным дням
                    if (withShift) {
                        var _getPreviousCorrectPosition = _sliced_to_array(getPreviousCorrectPosition(_object_spread_props(_object_spread({}, positionState), {
                            newRowIndex: newRowIndex,
                            newColumnIndex: newColumnIndex
                        })), 2), rowIndex = _getPreviousCorrectPosition[0], columnIndex = _getPreviousCorrectPosition[1];
                        // Если индексы равны, значит мы остались на текущей дате по причине, того
                        // что в следующем(-их)/предыдущем(-их) месяце нет доступных дат
                        setIsOutOfMinMaxRange(rowIndex === currentRowIndex && currentColumnIndex === columnIndex);
                        newRowIndex = rowIndex;
                        newColumnIndex = columnIndex;
                    }
                    if (isVisible(outerRefs, newRowIndex, newColumnIndex)) {
                        break;
                    }
                    if (!isDouble || getDoubleCalendarSide(currentRowIndex, calendarState) === 'first') {
                        if (isCurrentDateDisabledArrowLeft) {
                            newRowIndex = currentRowIndex;
                            newColumnIndex = currentColumnIndex;
                            break;
                        }
                        onPrev();
                    }
                    newRowIndex = minRowIndex - 1;
                    break;
                }
            case _Calendartypes.Keys.up:
                {
                    event.preventDefault();
                    newRowIndex = prevRowIndex < minRowIndex ? rowSize : prevRowIndex;
                    withShiftState.current = withShift;
                    var isCurrentDateDisabledArrowUp = hasDisabledArrowKey({
                        refs: outerRefs,
                        payload: [
                            currentRowIndex,
                            currentColumnIndex
                        ],
                        key: 'up'
                    });
                    setIsOutOfMinMaxRange(isCurrentDateDisabledArrowUp);
                    if (isCurrentDateDisabledArrowUp) {
                        newRowIndex = currentRowIndex;
                        newColumnIndex = currentColumnIndex;
                        break;
                    }
                    if (withShift) {
                        var item = outerRefs.current[newRowIndex][newColumnIndex];
                        var isNextDateDisabledArrowUp = !!item && hasDisabledArrowKey({
                            refs: outerRefs,
                            payload: [
                                newRowIndex,
                                newColumnIndex
                            ],
                            key: 'up'
                        });
                        var _getPreviousCorrectPosition1 = _sliced_to_array(getPreviousCorrectPosition(_object_spread_props(_object_spread({}, positionState), {
                            newRowIndex: newRowIndex,
                            newColumnIndex: newColumnIndex
                        })), 2), rowIndex1 = _getPreviousCorrectPosition1[0], columnIndex1 = _getPreviousCorrectPosition1[1];
                        // Если индексы равны, значит мы остались на текущей дате по причине, того
                        // что в следующем(-их)/предыдущем(-их) месяце нет доступных дат
                        var haseCurrentPosition = rowIndex1 === currentRowIndex && currentColumnIndex === columnIndex1;
                        setIsOutOfMinMaxRange(isNextDateDisabledArrowUp || haseCurrentPosition);
                        newRowIndex = rowIndex1;
                        newColumnIndex = columnIndex1;
                    }
                    if (isVisible(outerRefs, newRowIndex, newColumnIndex)) {
                        break;
                    }
                    if (!isDouble || getDoubleCalendarSide(currentRowIndex, calendarState) === 'first') {
                        if (isCurrentDateDisabledArrowUp) {
                            newRowIndex = currentRowIndex;
                            newColumnIndex = currentColumnIndex;
                            break;
                        }
                        onPrev();
                    }
                    newRowIndex = minRowIndex - 1;
                    break;
                }
            case _Calendartypes.Keys.right:
                {
                    event.preventDefault();
                    newRowIndex = nextColumnIndex > columnSize ? nextRowIndex : currentRowIndex;
                    newColumnIndex = nextColumnIndex > columnSize ? minColumnIndex : nextColumnIndex;
                    withShiftState.current = withShift;
                    var isCurrentDateDisabledArrowRight = hasDisabledArrowKey({
                        refs: outerRefs,
                        payload: [
                            currentRowIndex,
                            currentColumnIndex
                        ],
                        key: 'right'
                    });
                    setIsOutOfMinMaxRange(isCurrentDateDisabledArrowRight);
                    if (isCurrentDateDisabledArrowRight) {
                        newRowIndex = currentRowIndex;
                        newColumnIndex = currentColumnIndex;
                        break;
                    }
                    if (withShift) {
                        var _getNextCorrectPosition = _sliced_to_array(getNextCorrectPosition(_object_spread_props(_object_spread({}, positionState), {
                            newRowIndex: newRowIndex,
                            newColumnIndex: newColumnIndex
                        })), 2), rowIndex2 = _getNextCorrectPosition[0], columnIndex2 = _getNextCorrectPosition[1];
                        // Если индексы равны, значит мы остались на текущей дате по причине, того
                        // что в следующем(-их)/предыдущем(-их) месяце нет доступных дат
                        setIsOutOfMinMaxRange(rowIndex2 === currentRowIndex && currentColumnIndex === columnIndex2);
                        newRowIndex = rowIndex2;
                        newColumnIndex = columnIndex2;
                    }
                    if (isVisible(outerRefs, newRowIndex, newColumnIndex)) {
                        break;
                    }
                    if (!isDouble || getDoubleCalendarSide(currentRowIndex, calendarState) === 'second') {
                        if (isCurrentDateDisabledArrowRight) {
                            newRowIndex = currentRowIndex;
                            newColumnIndex = currentColumnIndex;
                            break;
                        }
                        onNext();
                    }
                    newRowIndex = rowSize + 1;
                    break;
                }
            case _Calendartypes.Keys.down:
                {
                    event.preventDefault();
                    newRowIndex = nextRowIndex > rowSize ? minRowIndex : nextRowIndex;
                    withShiftState.current = withShift;
                    var isCurrentDateDisabledArrowDown = hasDisabledArrowKey({
                        refs: outerRefs,
                        payload: [
                            currentRowIndex,
                            currentColumnIndex
                        ],
                        key: 'down'
                    });
                    setIsOutOfMinMaxRange(isCurrentDateDisabledArrowDown);
                    if (isCurrentDateDisabledArrowDown) {
                        newRowIndex = currentRowIndex;
                        newColumnIndex = currentColumnIndex;
                        break;
                    }
                    if (withShift) {
                        var item1 = outerRefs.current[newRowIndex][newColumnIndex];
                        var isNextDateDisabledArrowUp1 = !!item1 && hasDisabledArrowKey({
                            refs: outerRefs,
                            payload: [
                                newRowIndex,
                                newColumnIndex
                            ],
                            key: 'down'
                        });
                        var _getNextCorrectPosition1 = _sliced_to_array(getNextCorrectPosition(_object_spread_props(_object_spread({}, positionState), {
                            newRowIndex: newRowIndex,
                            newColumnIndex: newColumnIndex
                        })), 2), rowIndex3 = _getNextCorrectPosition1[0], columnIndex3 = _getNextCorrectPosition1[1];
                        // Если индексы равны, значит мы остались на текущей дате по причине, того
                        // что в следующем(-их)/предыдущем(-их) месяце нет доступных дат
                        var haseCurrentPosition1 = rowIndex3 === currentRowIndex && currentColumnIndex === columnIndex3;
                        setIsOutOfMinMaxRange(isNextDateDisabledArrowUp1 || haseCurrentPosition1);
                        newRowIndex = rowIndex3;
                        newColumnIndex = columnIndex3;
                    }
                    if (isVisible(outerRefs, newRowIndex, newColumnIndex)) {
                        break;
                    }
                    if (!isDouble || getDoubleCalendarSide(currentRowIndex, calendarState) === 'second') {
                        if (isCurrentDateDisabledArrowDown) {
                            newRowIndex = currentRowIndex;
                            newColumnIndex = currentColumnIndex;
                            break;
                        }
                        onNext();
                    }
                    newRowIndex = rowSize + 1;
                    break;
                }
            case _Calendartypes.Keys.enter:
            case _Calendartypes.Keys.space:
                {
                    var _outerRefs_current_newRowIndex, _outerRefs_current;
                    (_outerRefs_current = outerRefs.current) === null || _outerRefs_current === void 0 ? void 0 : (_outerRefs_current_newRowIndex = _outerRefs_current[newRowIndex]) === null || _outerRefs_current_newRowIndex === void 0 ? void 0 : _outerRefs_current_newRowIndex[newColumnIndex].click();
                    break;
                }
            default:
                return;
        }
        setSelectIndexes([
            newRowIndex,
            newColumnIndex
        ]);
    }, [
        selectIndexes,
        outerRefs,
        rowSize,
        columnSize,
        onNext,
        onPrev,
        isDouble
    ]);
    return [
        selectIndexes,
        onKeyDown,
        setSelectIndexes,
        outerRefs,
        isOutOfMinMaxRange
    ];
};
