"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _keyCodes = require("./keyCodes");

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var getFirstEle = function getFirstEle(node) {
  var first = node.parentNode.firstChild;

  while (first) {
    if (first.nodeType === Node.ELEMENT_NODE) {
      if (first.getAttribute("data-aria-listitem") === 'true') return first;
    }

    first = first.nextSibling;
  }

  return null;
};

var getLastEle = function getLastEle(node) {
  var last = node.parentNode.lastChild;

  while (last) {
    if (last.nodeType === Node.ELEMENT_NODE) {
      if (last.getAttribute("data-aria-listitem") === 'true') return last;
    }

    last = last.previousSibling;
  }

  return last;
};

var getNextEle = function getNextEle(node) {
  var next = node.nextSibling;

  while (next) {
    if (next.nodeType === Node.ELEMENT_NODE) {
      if (next.getAttribute("data-aria-listitem") === 'true') return next;
    }

    next = next.nextSibling;
  }

  return null;
};

var getPreviousEle = function getPreviousEle(node) {
  var prev = node.previousSibling;

  while (prev) {
    if (prev.nodeType === Node.ELEMENT_NODE) {
      if (prev.getAttribute("data-aria-listitem") === 'true') return prev;
    }

    prev = prev.previousSibling;
  }

  return null;
};

var setFocus = function setFocus(node) {
  var getCompType = node && node.getAttribute('role');

  if (getCompType === 'radio') {
    node['click'] && node.click();
  }

  node && node.setAttribute('tabIndex', '0');
  node && node.focus(); // node && node.scrollIntoView()
};

var arrowNavigate = function arrowNavigate(event, currentActiveEle) {
  var leftArrow = _keyCodes.keyCodes.leftArrow,
      rightArrow = _keyCodes.keyCodes.rightArrow,
      topArrow = _keyCodes.keyCodes.topArrow,
      bottomArrow = _keyCodes.keyCodes.bottomArrow;

  if (currentActiveEle.dataset.ariaNavigation) {
    var arrowPermission = (currentActiveEle.dataset.ariaNavigation || '').split(',');
    arrowPermission.forEach(function (item, i) {
      var _split = (item || '').split('-'),
          _split2 = _slicedToArray(_split, 1),
          keyName = _split2[0];

      var nextEle;

      if (keyName && (parseInt(keyName) === leftArrow || parseInt(keyName) === topArrow) && parseInt(keyName) === event.keyCode) {
        if (parseInt(keyName) === topArrow) {
          event && event.preventDefault();
        }

        nextEle = getPreviousEle(currentActiveEle);

        if (!nextEle) {
          nextEle = getLastEle(currentActiveEle);
        }
      }

      if (keyName && (parseInt(keyName) === rightArrow || parseInt(keyName) === bottomArrow) && parseInt(keyName) === event.keyCode) {
        if (parseInt(keyName) === bottomArrow) {
          event && event.preventDefault();
        }

        nextEle = getNextEle(currentActiveEle);

        if (!nextEle) {
          nextEle = getFirstEle(currentActiveEle);
        }
      }

      if (nextEle) {
        setFocus(nextEle);
      }
    });
  }
};

var _default = arrowNavigate;
exports["default"] = _default;