"use strict";

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

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

var focusableElements = ['input:not([disabled]):not([type=hidden])', 'textarea:not([disabled])', 'button:not([disabled])', 'select', 'a[href]', 'iframe', '[tabindex]:not([tabindex="-1"])'];
var TAB_ELEMENT = focusableElements.join(':not([tabindex="-1"]),');

var findElement = function findElement(elements) {
  var ele = [];
  Object.keys(elements).forEach(function (element) {
    var elem = elements[element];

    if (elem.getAttribute('data-aria-autofocus') === 'true') {
      ele.push(elem);
    }
  });
  return ele.length ? ele[0] : null;
};

var getFocusEle = function getFocusEle(contentRef) {
  var focusEle = contentRef && contentRef.querySelectorAll(TAB_ELEMENT);
  var findFocus = focusEle && findElement(focusEle);
  var firstEle = focusEle && focusEle[0];
  var lastEle = focusEle && focusEle[focusEle.length - 1];
  return {
    firstEle: firstEle,
    lastEle: lastEle,
    findFocus: findFocus
  };
};

var preventDOMAvailabllityAndFocus = function preventDOMAvailabllityAndFocus(el) {
  requestAnimationFrame(function () {
    el && el.focus();
  });
};

var restoreFocusTypes = {
  stack: "stack",
  list: "list"
};
exports.restoreFocusTypes = restoreFocusTypes;
var restoreFocusHandler = {
  restoreNodes: null,
  set: function set(restoreFocusId, isNested, updateOnSetRestoreFocus, ownRestoreRef) {
    restoreFocusHandler.restoreNodes = restoreFocusHandler.restoreNodes || [];
    var newRestoreEle = {
      el: ownRestoreRef || document.activeElement,
      id: restoreFocusId
    };

    if (isNested) {
      restoreFocusHandler.restoreNodes.push(newRestoreEle);
    } else {
      restoreFocusHandler.restoreNodes = [newRestoreEle];
    }

    if (updateOnSetRestoreFocus) {
      restoreFocusHandler.restoreNodes = updateOnSetRestoreFocus(restoreFocusHandler.restoreNodes);
    }
  },
  getLastActiveEl: function getLastActiveEl(restoreFocusId) {
    return (restoreFocusHandler.restoreNodes || []).some(function (item) {
      if (item.id === restoreFocusId) {
        return item.el;
      }
    });
  },
  clear: function clear(restoreFocusId, isNested, updateOnClearRestoreFocus) {
    restoreFocusHandler.fireFocus(restoreFocusId);

    if (restoreFocusHandler.restoreNodes && isNested) {
      restoreFocusHandler.restoreNodes = restoreFocusHandler.restoreNodes.filter(function (item) {
        return item.id === restoreFocusId;
      });

      if (restoreFocusHandler.restoreNodes.length === 0) {
        restoreFocusHandler.restoreNodes = null;
      }
    } else {
      restoreFocusHandler.restoreNodes = null;
    }

    if (updateOnClearRestoreFocus) {
      restoreFocusHandler.restoreNodes = updateOnClearRestoreFocus(restoreFocusHandler.restoreNodes);
    }
  },
  fireFocus: function fireFocus(restoreFocusId) {
    var restoreEl = restoreFocusHandler.getLastActiveEl(restoreFocusId);
    preventDOMAvailabllityAndFocus(restoreEl);
  }
};

var restoreFocuser = function restoreFocuser() {
  var isRestoreFocus = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  var restoreFocusId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '1';
  var isNested = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  var updateOnSetRestoreFocus = arguments.length > 3 ? arguments[3] : undefined;
  var updateOnClearRestoreFocus = arguments.length > 4 ? arguments[4] : undefined;
  var ownRestoreRef = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : null;

  if (isRestoreFocus) {
    restoreFocusHandler.set(restoreFocusId, isNested, updateOnSetRestoreFocus, ownRestoreRef);
  } else {
    restoreFocusHandler.clear(restoreFocusId, isNested, updateOnClearRestoreFocus);
  }
};

var autoFocuser = function autoFocuser(contentRef) {
  var isAutoFocus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

  if (!contentRef) {
    return null;
  }

  var _getFocusEle = getFocusEle(contentRef),
      firstEle = _getFocusEle.firstEle,
      lastEle = _getFocusEle.lastEle,
      findFocus = _getFocusEle.findFocus;

  var focusAbleEl = findFocus || firstEle;
  var getActiveClass = focusAbleEl && focusAbleEl.getAttribute('data-active-style');

  if (isAutoFocus) {
    preventDOMAvailabllityAndFocus(focusAbleEl);
    focusAbleEl && focusAbleEl.classList.add(getActiveClass);
  } else {
    focusAbleEl && focusAbleEl.classList.remove(getActiveClass);
  }
};

var focusLoopHandler = {
  create: function create(contentRef) {
    if (!focusLoopHandler.firstEle || !focusLoopHandler.lastEle) {
      var _getFocusEle2 = getFocusEle(contentRef),
          firstEle = _getFocusEle2.firstEle,
          lastEle = _getFocusEle2.lastEle;

      focusLoopHandler.firstEle = firstEle;
      focusLoopHandler.lastEle = lastEle;
    }

    contentRef && contentRef.addEventListener('keydown', focusLoopHandler.keyDownHandler);
  },
  clear: function clear(contentRef) {
    focusLoopHandler.firstEle = null;
    focusLoopHandler.lastEle = null;
    contentRef.removeEventListener('keydown', focusLoopHandler.keyDownHandler);
  },
  keyDownHandler: function keyDownHandler(event) {
    if (event.key === 'Tab' || event.keyCode === 9 && event.shiftKey) {
      if (document.activeElement === focusLoopHandler.firstEle && event.keyCode === 9 && event.shiftKey) {
        preventDOMAvailabllity(focusLoopHandler.lastEle);
        event.preventDefault();
      } else if (document.activeElement === focusLoopHandler.lastEle && event.key === 'Tab' && !event.shiftKey) {
        preventDOMAvailabllity(focusLoopHandler.firstEle);
        event.preventDefault();
      }
    }
  }
};

var focusLooper = function focusLooper(contentRef) {
  var isFocusLoop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

  if (!contentRef) {
    return null;
  }

  if (isFocusLoop) {
    focusLoopHandler.create(contentRef);
  } else {
    focusLoopHandler.clear(contentRef);
  }
};

var _default = {
  autoFocus: autoFocuser,
  focusLoop: focusLooper,
  restoreFocus: restoreFocuser
};
exports["default"] = _default;