export var useKeyboardNavigation = function useKeyboardNavigation(_ref) {
  var itemsLength = _ref.itemsLength,
    _ref$onChange = _ref.onChange,
    onChange = _ref$onChange === void 0 ? function (_) {
      return null;
    } : _ref$onChange;
  var tabRefs = [];
  var getActive = function getActive() {
    return Object.values(tabRefs).findIndex(function (tab) {
      return tab === document.activeElement;
    });
  };
  var onKeyDown = function onKeyDown(e) {
    var active = getActive();
    var left = e.key && e.key === 'ArrowLeft' || e.keyCode === 37;
    var right = e.key && e.key === 'ArrowRight' || e.keyCode === 39;
    if (left) {
      if (active === 0) {
        return;
      }
      var newSelected = active - 1;
      if (tabRefs[newSelected]) tabRefs[newSelected].focus();
      onChange(newSelected);
      return;
    }
    if (right) {
      if (active === itemsLength - 1) {
        return;
      }
      var _newSelected = active + 1;
      if (tabRefs[_newSelected]) tabRefs[_newSelected].focus();
      onChange(_newSelected);
    }
  };
  var setRef = function setRef(index) {
    return function (ref) {
      if (ref) tabRefs[index] = ref;
    };
  };
  return {
    setRef: setRef,
    onKeyDown: onKeyDown
  };
};