'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');

var Keys = {
  Enter: 'Enter',
  Space: 'Space',
  Tab: 'Tab',
  Escape: 'Escape',
  ArrowLeft: 'ArrowLeft',
  ArrowRight: 'ArrowRight',
  ArrowUp: 'ArrowUp',
  ArrowDown: 'ArrowDown',
  Backspace: 'Backspace',
  ShiftLeft: 'ShiftLeft',
  ShiftRight: 'ShiftRight'
};
var useKeyNavigation = function useKeyNavigation(_ref) {
  var _ref$controlledRefs = _ref.controlledRefs,
    targetRef = _ref$controlledRefs.targetRef,
    chipsRefs = _ref$controlledRefs.chipsRefs,
    selectRef = _ref$controlledRefs.selectRef,
    itemsRefs = _ref$controlledRefs.itemsRefs,
    inputRef = _ref$controlledRefs.inputRef,
    opened = _ref.opened,
    enumerationType = _ref.enumerationType,
    valueType = _ref.valueType,
    _ref$componentType = _ref.componentType,
    componentType = _ref$componentType === void 0 ? 'select' : _ref$componentType,
    value = _ref.value,
    search = _ref.search,
    updateValue = _ref.updateValue,
    updateOpened = _ref.updateOpened;
  var _useState = React.useState(-1),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    selectItemIndex = _useState2[0],
    setSelectItemIndex = _useState2[1];
  var _useState3 = React.useState(-1),
    _useState4 = _rollupPluginBabelHelpers.slicedToArray(_useState3, 2),
    selectChipIndex = _useState4[0],
    setSelectChipIndex = _useState4[1];
  React.useEffect(function () {
    var _itemsRefs$current;
    if (!opened) {
      setSelectItemIndex(-1);
      setSelectChipIndex(-1);
      return;
    }
    itemsRefs === null || itemsRefs === void 0 || (_itemsRefs$current = itemsRefs.current) === null || _itemsRefs$current === void 0 || (_itemsRefs$current = _itemsRefs$current[selectItemIndex]) === null || _itemsRefs$current === void 0 || _itemsRefs$current.focus();
  }, [opened]);
  React.useEffect(function () {
    var _itemsRefs$current2;
    itemsRefs === null || itemsRefs === void 0 || (_itemsRefs$current2 = itemsRefs.current) === null || _itemsRefs$current2 === void 0 || (_itemsRefs$current2 = _itemsRefs$current2[selectItemIndex]) === null || _itemsRefs$current2 === void 0 || _itemsRefs$current2.focus();
  }, [value, search]);
  var onKeyDownTarget = React.useCallback(function (event) {
    var _focusedItem;
    var code = event.code;
    var chipsRefsCount = chipsRefs.current.length;
    var scrollIntoView = false;
    var newOpened = opened;
    var newSelectItemIndex = selectItemIndex;
    var newSelectChipIndex = selectChipIndex;
    var focusedItem = null;
    if (code === Keys.Enter || code === Keys.Space && componentType !== 'combobox') {
      event.preventDefault();
    }
    if (code === Keys.Escape) {
      newOpened = false;
      newSelectItemIndex = -1;
      newSelectChipIndex = -1;
      focusedItem = targetRef.current;
    }
    if (code === Keys.ArrowLeft && valueType === 'multiple' && enumerationType === 'chip') {
      var _chipsRefs$current;
      var index = selectChipIndex - 1 <= 0 ? 0 : selectChipIndex - 1;
      newSelectChipIndex = document.activeElement === targetRef.current ? 0 : index;
      newSelectChipIndex = document.activeElement === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) ? chipsRefsCount - 1 : newSelectChipIndex;
      focusedItem = (_chipsRefs$current = chipsRefs.current) === null || _chipsRefs$current === void 0 ? void 0 : _chipsRefs$current[newSelectChipIndex];
      scrollIntoView = true;
    }
    if (code === Keys.ArrowRight && valueType === 'multiple' && enumerationType === 'chip') {
      var _chipsRefs$current2;
      var _index = selectChipIndex + 1 === chipsRefsCount ? selectChipIndex : selectChipIndex + 1;
      newSelectChipIndex = document.activeElement === targetRef.current ? 0 : _index;
      focusedItem = _index === selectChipIndex ? inputRef === null || inputRef === void 0 ? void 0 : inputRef.current : (_chipsRefs$current2 = chipsRefs.current) === null || _chipsRefs$current2 === void 0 ? void 0 : _chipsRefs$current2[newSelectChipIndex];
      scrollIntoView = true;
    }
    if (code === Keys.Backspace && document.activeElement !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
      var _ref2, _chipsRefs$current3;
      var _index2 = selectChipIndex - 1 <= 0 ? 0 : selectChipIndex - 1;
      newSelectChipIndex = chipsRefsCount === 0 ? -1 : _index2;
      focusedItem = chipsRefsCount === 0 ? (_ref2 = inputRef || targetRef) === null || _ref2 === void 0 ? void 0 : _ref2.current : chipsRefs === null || chipsRefs === void 0 || (_chipsRefs$current3 = chipsRefs.current) === null || _chipsRefs$current3 === void 0 ? void 0 : _chipsRefs$current3[_index2];
      scrollIntoView = true;
    }
    if (code === Keys.ArrowUp || code === Keys.ArrowDown || code === Keys.Enter && componentType !== 'combobox' || code === Keys.Space && componentType !== 'combobox') {
      var _itemsRefs$current3;
      newOpened = true;
      newSelectItemIndex = selectItemIndex === -1 ? 0 : selectItemIndex;
      focusedItem = (_itemsRefs$current3 = itemsRefs.current) === null || _itemsRefs$current3 === void 0 ? void 0 : _itemsRefs$current3[newSelectItemIndex];
    }

    // INFO: Для кейсов, когда поведение закрытия может контролироваться снаружи
    if (code !== Keys.Enter || componentType !== 'combobox') {
      updateOpened(newOpened, event);
    }
    setSelectItemIndex(newSelectItemIndex);
    setSelectChipIndex(newSelectChipIndex);
    (_focusedItem = focusedItem) === null || _focusedItem === void 0 || _focusedItem.focus();
    if (scrollIntoView) {
      var _focusedItem2;
      (_focusedItem2 = focusedItem) === null || _focusedItem2 === void 0 || _focusedItem2.scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'center'
      });
    }
  }, [opened, selectItemIndex, selectChipIndex, valueType, enumerationType, componentType, updateOpened]);
  var onKeyDownSelect = React.useCallback(function (event) {
    var _focusedItem3;
    var code = event.code,
      shiftKey = event.shiftKey;
    var itemsRefsCount = itemsRefs.current.length;
    var chipsRefsCount = chipsRefs.current.length;
    var scrollIntoView = false;
    var newOpened = opened;
    var newSelectItemIndex = selectItemIndex;
    var newSelectChipIndex = selectChipIndex;
    var focusedItem = null;
    if (code === Keys.Enter || code === Keys.Tab) {
      event.preventDefault();
    }
    if (code === Keys.Escape) {
      newOpened = false;
      newSelectItemIndex = -1;
      newSelectChipIndex = -1;
      focusedItem = (inputRef || targetRef).current;
    }
    if (code === Keys.Tab) {
      var _selectRef$current, _selectRef$current2;
      newOpened = false;
      newSelectItemIndex = -1;
      newSelectChipIndex = -1;
      focusedItem = shiftKey ? (_selectRef$current = selectRef.current) === null || _selectRef$current === void 0 ? void 0 : _selectRef$current.previousSibling : (_selectRef$current2 = selectRef.current) === null || _selectRef$current2 === void 0 ? void 0 : _selectRef$current2.nextSibling;
    }
    if (code === Keys.ArrowUp) {
      var _itemsRefs$current4;
      newSelectItemIndex = newSelectItemIndex - 1 === -1 ? itemsRefsCount - 1 : newSelectItemIndex - 1;
      focusedItem = itemsRefs === null || itemsRefs === void 0 || (_itemsRefs$current4 = itemsRefs.current) === null || _itemsRefs$current4 === void 0 ? void 0 : _itemsRefs$current4[newSelectItemIndex];
    }
    if (code === Keys.ArrowDown) {
      var _itemsRefs$current5;
      newSelectItemIndex = newSelectItemIndex + 1 === itemsRefsCount ? 0 : newSelectItemIndex + 1;
      focusedItem = itemsRefs === null || itemsRefs === void 0 || (_itemsRefs$current5 = itemsRefs.current) === null || _itemsRefs$current5 === void 0 ? void 0 : _itemsRefs$current5[newSelectItemIndex];
    }
    if ((code === Keys.ArrowRight || code === Keys.ArrowLeft) && valueType === 'multiple' && enumerationType === 'chip' && chipsRefsCount) {
      var _chipsRefs$current4;
      newSelectChipIndex = chipsRefsCount - 1;
      newSelectItemIndex = -1;
      focusedItem = chipsRefs === null || chipsRefs === void 0 || (_chipsRefs$current4 = chipsRefs.current) === null || _chipsRefs$current4 === void 0 ? void 0 : _chipsRefs$current4[newSelectChipIndex];
      scrollIntoView = true;
    }
    if (code === Keys.Enter || code === Keys.Space && componentType !== 'combobox') {
      var item = itemsRefs.current[newSelectItemIndex];

      // TODO: #1016
      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
      // @ts-ignore
      if (item.ariaDisabled) {
        return;
      }
      updateValue(item, event);
      newOpened = valueType === 'single' ? false : opened;
      focusedItem = valueType === 'single' ? targetRef.current : focusedItem;
    }
    var isValidKeyCode = [Keys.ShiftLeft, Keys.ShiftRight, Keys.ArrowDown, Keys.ArrowLeft, Keys.ArrowRight, Keys.ArrowUp, Keys.Enter, Keys.Escape, Keys.Tab].includes(code);
    if (componentType === 'combobox' && !isValidKeyCode) {
      event.stopPropagation();
      focusedItem = inputRef === null || inputRef === void 0 ? void 0 : inputRef.current;
      newSelectItemIndex = -1;
      newSelectChipIndex = -1;
    }
    updateOpened(newOpened, event);
    setSelectItemIndex(newSelectItemIndex);
    setSelectChipIndex(newSelectChipIndex);
    (_focusedItem3 = focusedItem) === null || _focusedItem3 === void 0 || _focusedItem3.focus();
    if (scrollIntoView) {
      var _focusedItem4;
      (_focusedItem4 = focusedItem) === null || _focusedItem4 === void 0 || _focusedItem4.scrollIntoView({
        behavior: 'smooth',
        block: 'center',
        inline: 'center'
      });
    }
  }, [opened, selectItemIndex, selectChipIndex, valueType, enumerationType, componentType, updateOpened, updateValue]);
  return {
    onKeyDownTarget: onKeyDownTarget,
    onKeyDownSelect: onKeyDownSelect
  };
};

exports.Keys = Keys;
exports.useKeyNavigation = useKeyNavigation;
//# sourceMappingURL=useKeyNavigation.js.map
