var keys = {
  Enter: 'Enter',
  Space: 'Space',
  Tab: 'Tab',
  Escape: 'Escape',
  ArrowLeft: 'ArrowLeft',
  ArrowRight: 'ArrowRight',
  ArrowUp: 'ArrowUp',
  ArrowDown: 'ArrowDown',
  Home: 'Home',
  End: 'End',
  PageUp: 'PageUp',
  PageDown: 'PageDown'
};
var useKeyNavigation = function useKeyNavigation(_ref) {
  var isOpen = _ref.isOpen,
    setIsOpen = _ref.setIsOpen,
    focused = _ref.focused,
    dispatchFocused = _ref.dispatchFocused,
    finalResults = _ref.finalResults,
    handleItemClick = _ref.handleItemClick;
  var onKeyDown = function onKeyDown(event) {
    switch (event.code) {
      case keys.ArrowDown:
        {
          if (!isOpen) {
            setIsOpen(true);
            dispatchFocused({
              type: 'setFirstItem'
            });
            break;
          }
          dispatchFocused({
            type: 'setNextItem',
            listLength: finalResults.length
          });
          break;
        }
      case keys.ArrowUp:
        {
          if (!isOpen) {
            setIsOpen(true);
            dispatchFocused({
              type: 'setLastItem',
              listLength: finalResults.length
            });
            break;
          }
          dispatchFocused({
            type: 'setPrevItem',
            listLength: finalResults.length
          });
          break;
        }
      case keys.Home:
        {
          if (!isOpen) {
            return;
          }
          dispatchFocused({
            type: 'setFirstItem'
          });
          break;
        }
      case keys.End:
        {
          if (!isOpen) {
            return;
          }
          dispatchFocused({
            type: 'setLastItem',
            listLength: finalResults.length
          });
          break;
        }
      case keys.Enter:
        {
          if (!isOpen || focused === null) {
            return;
          }
          dispatchFocused({
            type: 'reset'
          });
          handleItemClick(finalResults[focused]);
          break;
        }
      case keys.Tab:
      case keys.Escape:
        {
          if (!isOpen) {
            return;
          }
          setIsOpen(false);
          dispatchFocused({
            type: 'reset'
          });
          break;
        }
    }
  };
  return {
    onKeyDown: onKeyDown
  };
};

export { keys, useKeyNavigation };
