'use strict';

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

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

var Keys = {
  Enter: 'Enter',
  Tab: 'Tab',
  Escape: 'Escape',
  ArrowLeft: 'ArrowLeft',
  ArrowRight: 'ArrowRight',
  Backspace: 'Backspace'
};
var useKeyNavigation = function useKeyNavigation(_ref) {
  var _ref$controlledRefs = _ref.controlledRefs,
    chipsRefs = _ref$controlledRefs.chipsRefs,
    contentRef = _ref$controlledRefs.contentRef,
    inputRef = _ref$controlledRefs.inputRef,
    disabled = _ref.disabled,
    readOnly = _ref.readOnly,
    chips = _ref.chips,
    newCustomChips = _ref.newCustomChips,
    enumerationType = _ref.enumerationType,
    updateChips = _ref.updateChips,
    onChange = _ref.onChange,
    onSearch = _ref.onSearch,
    _ref$onEnterDisabled = _ref.onEnterDisabled,
    onEnterDisabled = _ref$onEnterDisabled === void 0 ? false : _ref$onEnterDisabled,
    onChipCloseClick = _ref.onChipCloseClick,
    _interaction_disabled = _ref._interaction_disabled;
  var _useState = React.useState(-1),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    activeChipIndex = _useState2[0],
    setActiveChipIndex = _useState2[1];
  var handleInputKeydown = function handleInputKeydown(event) {
    var _inputRef$current, _inputRef$current2;
    var _ref2 = event.target,
      value = _ref2.value;
    if (disabled || readOnly || _interaction_disabled) {
      return;
    }
    if (event.key === Keys.Enter && !onEnterDisabled) {
      if (enumerationType === 'chip' && value) {
        var nextIndex = (chips === null || chips === void 0 ? void 0 : chips.length) || 0;
        var newChips = [].concat(_rollupPluginBabelHelpers.toConsumableArray(chips), [{
          id: "".concat(nextIndex, "_").concat(value),
          text: value
        }]);
        var newValues = newChips.map(function (_ref3) {
          var text = _ref3.text;
          return text;
        });
        updateChips(newChips, newValues);
        event.target.value = '';
        onChange === null || onChange === void 0 || onChange(event);
      }
      if (onSearch) {
        onSearch(value, event);
      }
    }
    if (event.key === Keys.Backspace && newCustomChips !== null && newCustomChips !== void 0 && newCustomChips.length && (inputRef === null || inputRef === void 0 || (_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.selectionStart) === 0) {
      var lastChip = newCustomChips.at(-1);
      if (onChipCloseClick && lastChip) {
        onChipCloseClick(lastChip);
        chipsRefs === null || chipsRefs === void 0 || chipsRefs.current.pop();
      }
    }
    if (event.key === Keys.Backspace && chips.length && (inputRef === null || inputRef === void 0 || (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.selectionStart) === 0) {
      var _newChips = _rollupPluginBabelHelpers.toConsumableArray(chips);
      _newChips === null || _newChips === void 0 || _newChips.pop();
      chipsRefs === null || chipsRefs === void 0 || chipsRefs.current.pop();
      var _newValues = _newChips.map(function (_ref4) {
        var text = _ref4.text;
        return text;
      });
      updateChips(_newChips, _newValues);
    }
  };
  var onChipClear = function onChipClear(clearId, index) {
    if (disabled || readOnly || !chips.length && !(newCustomChips !== null && newCustomChips !== void 0 && newCustomChips.length)) {
      return;
    }
    var newChips = (chips === null || chips === void 0 ? void 0 : chips.filter(function (_ref5) {
      var chipId = _ref5.id;
      return chipId !== clearId;
    })) || [];
    var newValues = newChips.map(function (_ref6) {
      var text = _ref6.text;
      return text;
    });
    chipsRefs === null || chipsRefs === void 0 || chipsRefs.current.splice(index, 1);
    updateChips(newChips, newValues);
  };
  var handleChipKeyDown = function handleChipKeyDown(event, chipId, chipIndex) {
    if (disabled || readOnly) {
      return;
    }
    if (event.key === Keys.Tab) {
      event.preventDefault();
    }
    if (event.key === Keys.Backspace) {
      onChipClear(chipId, chipIndex);
    }
  };
  var handleContentKeyDown = function handleContentKeyDown(event) {
    var _inputRef$current3, _focusedItem, _focusedItem2;
    var chipsRefsCount = chipsRefs.current.length;
    var currentCaretPosition = (inputRef === null || inputRef === void 0 || (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.selectionStart) || 0;
    var newActiveChipIndex = activeChipIndex;
    var focusedItem = null;
    if (event.key === Keys.ArrowLeft && enumerationType === 'chip' && currentCaretPosition <= 0) {
      var _chipsRefs$current;
      var index = activeChipIndex - 1 <= 0 ? 0 : activeChipIndex - 1;
      newActiveChipIndex = document.activeElement === (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current) ? chipsRefsCount - 1 : index;
      focusedItem = (_chipsRefs$current = chipsRefs.current) === null || _chipsRefs$current === void 0 ? void 0 : _chipsRefs$current[newActiveChipIndex];
    }
    if (event.key === Keys.ArrowRight && enumerationType === 'chip' && document.activeElement !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
      var _chipsRefs$current2;
      var _index = activeChipIndex + 1 >= chipsRefsCount ? activeChipIndex : activeChipIndex + 1;
      newActiveChipIndex = document.activeElement === (contentRef === null || contentRef === void 0 ? void 0 : contentRef.current) ? 0 : _index;
      focusedItem = _index === activeChipIndex ? inputRef === null || inputRef === void 0 ? void 0 : inputRef.current : (_chipsRefs$current2 = chipsRefs.current) === null || _chipsRefs$current2 === void 0 ? void 0 : _chipsRefs$current2[newActiveChipIndex];
    }
    if (event.key === Keys.Backspace && document.activeElement !== (inputRef === null || inputRef === void 0 ? void 0 : inputRef.current)) {
      var _chipsRefs$current3;
      var _index2 = activeChipIndex - 1 <= 0 ? 0 : activeChipIndex - 1;
      newActiveChipIndex = chipsRefsCount === 0 ? -1 : _index2;
      focusedItem = chipsRefsCount === 0 ? inputRef === null || inputRef === void 0 ? void 0 : inputRef.current : chipsRefs === null || chipsRefs === void 0 || (_chipsRefs$current3 = chipsRefs.current) === null || _chipsRefs$current3 === void 0 ? void 0 : _chipsRefs$current3[newActiveChipIndex];
    }
    (_focusedItem = focusedItem) === null || _focusedItem === void 0 || _focusedItem.focus();
    (_focusedItem2 = focusedItem) === null || _focusedItem2 === void 0 || _focusedItem2.scrollIntoView({
      behavior: 'smooth',
      block: 'center',
      inline: 'center'
    });
    setActiveChipIndex(newActiveChipIndex);
  };
  return {
    handleInputKeydown: handleInputKeydown,
    handleChipKeyDown: handleChipKeyDown,
    onChipClear: onChipClear,
    handleContentKeyDown: handleContentKeyDown
  };
};

exports.Keys = Keys;
exports.useKeyNavigation = useKeyNavigation;
