var navigateList = {
  methods: {
    /**
     * function to navigate a drop down list or similar via key up/down
     * @param {Array} list - the list to navigate in
     * @param {boolean} isArrowDown - was key navigation up or down
     * @param {number} currentIndex - the index of the current active option in the array
     * @param {boolean} [loop=false] - should index return to last/first
     *  item if first/last item was reached
     * @param {boolean} [allowNegativeIndex=false] - allow index to become -1 (no active option -->
     *  function will return null)
     * @returns {?Object|?string} - the new active element
     */
    navigate: function navigate(list, isArrowDown, currentIndex) {
      var loop = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
      var allowNegativeIndex = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
      // depending if arrow was up or down set +/- one to add or subtract
      // generically
      var numberToAdd = isArrowDown ? 1 : -1;

      if (this.isWithinArrayLimit(list, isArrowDown, currentIndex + numberToAdd)) {
        // set new active entry
        return list[currentIndex + numberToAdd];
      }

      if (currentIndex >= 0 && allowNegativeIndex) {
        return null;
      }

      if (loop) {
        return list[isArrowDown ? 0 : list.length - 1];
      }

      return list[currentIndex];
    },
    isWithinArrayLimit: function isWithinArrayLimit(list, isArrowDown, newIndex) {
      // set variable for limit to check against
      // 0 for arrow up and array length for arrow down
      var listLimit = isArrowDown ? list.length : 0; // define operator functions to be able to use condition generically

      var operators = {
        '>=': function _(a, b) {
          return a >= b;
        },
        '<': function _(a, b) {
          return a < b;
        }
      }; // set the actual operator

      var operator = isArrowDown ? '<' : '>='; // return Boolean if it is within limits of array or not

      return operators[operator](newIndex, listLimit);
    }
  }
};

export { navigateList as n };
