text: "use strict";
  
  var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
  
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = exports.onFocus = exports.onBlur = void 0;
  
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  
  var _react = _interopRequireDefault(require("react"));
  
  var _propTypes = _interopRequireDefault(require("prop-types"));
  
  var _reactClientsideEffect = _interopRequireDefault(require("react-clientside-effect"));
  
  var _focusLock = _interopRequireWildcard(require("focus-lock"));
  
  var _util = require("./util");
  
  var focusOnBody = function focusOnBody() {
    return document && document.activeElement === document.body;
  };
  
  var isFreeFocus = function isFreeFocus() {
    return focusOnBody() || (0, _focusLock.focusIsHidden)();
  };
  
  var lastActiveTrap = null;
  var lastActiveFocus = null;
  var lastPortaledElement = null;
  var focusWasOutsideWindow = false;
  
  var defaultWhitelist = function defaultWhitelist() {
    return true;
  };
  
  var focusWhitelisted = function focusWhitelisted(activeElement) {
    return (lastActiveTrap.whiteList || defaultWhitelist)(activeElement);
  };
  
  var recordPortal = function recordPortal(observerNode, portaledElement) {
    lastPortaledElement = {
      observerNode: observerNode,
      portaledElement: portaledElement
    };
  };
  
  var focusIsPortaledPair = function focusIsPortaledPair(element) {
    return lastPortaledElement && lastPortaledElement.portaledElement === element;
  };
  
  function autoGuard(startIndex, end, step, allNodes) {
    var lastGuard = null;
    var i = startIndex;
  
    do {
      var item = allNodes[i];
  
      if (item.guard) {
        if (item.node.dataset.focusAutoGuard) {
          lastGuard = item;
        }
      } else if (item.lockItem) {
        if (i !== startIndex) {
          // we will tab to the next element
          return;
        }
  
        lastGuard = null;
      } else {
        break;
      }
    } while ((i += step) !== end);
  
    if (lastGuard) {
      lastGuard.node.tabIndex = 0;
    }
  }
  
  var extractRef = function extractRef(ref) {
    return ref && 'current' in ref ? ref.current : ref;
  };
  
  var activateTrap = function activateTrap() {
    var result = false;
  
    if (lastActiveTrap) {
      var _lastActiveTrap = lastActiveTrap,
          observed = _lastActiveTrap.observed,
          persistentFocus = _lastActiveTrap.persistentFocus,
          autoFocus = _lastActiveTrap.autoFocus,
          shards = _lastActiveTrap.shards;
      var workingNode = observed || lastPortaledElement && lastPortaledElement.portaledElement;
      var activeElement = document && document.activeElement;
  
      if (workingNode) {
        var workingArea = [workingNode].concat((0, _toConsumableArray2.default)(shards.map(extractRef).filter(Boolean)));
  
        if (!activeElement || focusWhitelisted(activeElement)) {
          if (persistentFocus || focusWasOutsideWindow || !isFreeFocus() || !lastActiveFocus && autoFocus) {
            if (workingNode && !((0, _focusLock.focusInside)(workingArea) || focusIsPortaledPair(activeElement, workingNode))) {
              if (document && !lastActiveFocus && activeElement && !autoFocus) {
                activeElement.blur();
                document.body.focus();
              } else {
                result = (0, _focusLock.default)(workingArea, lastActiveFocus);
                lastPortaledElement = {};
              }
            }
  
            focusWasOutsideWindow = false;
            lastActiveFocus = document && document.activeElement;
          }
        }
  
        if (document) {
          var newActiveElement = document && document.activeElement;
          var allNodes = (0, _focusLock.getFocusabledIn)(workingArea);
          var focusedItem = allNodes.find(function (_ref) {
            var node = _ref.node;
            return node === newActiveElement;
          });
  
          if (focusedItem) {
            // remove old focus
            allNodes.filter(function (_ref2) {
              var guard = _ref2.guard,
                  node = _ref2.node;
              return guard && node.dataset.focusAutoGuard;
            }).forEach(function (_ref3) {
              var node = _ref3.node;
              return node.removeAttribute('tabIndex');
            });
            var focusedIndex = allNodes.indexOf(focusedItem);
            autoGuard(focusedIndex, allNodes.length, +1, allNodes);
            autoGuard(focusedIndex, -1, -1, allNodes);
          }
        }
      }
    }
  
    return result;
  };
  
  var onTrap = function onTrap(event) {
    if (activateTrap() && event) {
      // prevent scroll jump
      event.stopPropagation();
      event.preventDefault();
    }
  };
  
  var onBlur = function onBlur() {
    return (0, _util.deferAction)(activateTrap);
  };
  
  exports.onBlur = onBlur;
  
  var onFocus = function onFocus(event) {
    // detect portal
    var source = event.target;
    var currentNode = event.currentTarget;
  
    if (!currentNode.contains(source)) {
      recordPortal(currentNode, source);
    }
  };
  
  exports.onFocus = onFocus;
  
  var FocusWatcher = function FocusWatcher() {
    return null;
  };
  
  var FocusTrap = function FocusTrap(_ref4) {
    var children = _ref4.children;
    return _react.default.createElement("div", {
      onBlur: onBlur,
      onFocus: onFocus
    }, children);
  };
  
  FocusTrap.propTypes = process.env.NODE_ENV !== "production" ? {
    children: _propTypes.default.node.isRequired
  } : {};
  
  var onWindowBlur = function onWindowBlur() {
    focusWasOutsideWindow = true;
  };
  
  var attachHandler = function attachHandler() {
    document.addEventListener('focusin', onTrap, true);
    document.addEventListener('focusout', onBlur);
    window.addEventListener('blur', onWindowBlur);
  };
  
  var detachHandler = function detachHandler() {
    document.removeEventListener('focusin', onTrap, true);
    document.removeEventListener('focusout', onBlur);
    window.removeEventListener('blur', onWindowBlur);
  };
  
  function reducePropsToState(propsList) {
    return propsList.filter(function (_ref5) {
      var disabled = _ref5.disabled;
      return !disabled;
    }).slice(-1)[0];
  }
  
  function handleStateChangeOnClient(trap) {
    if (trap && !lastActiveTrap) {
      attachHandler();
    }
  
    var lastTrap = lastActiveTrap;
    var sameTrap = lastTrap && trap && trap.onActivation === lastTrap.onActivation;
    lastActiveTrap = trap;
  
    if (lastTrap && !sameTrap) {
      lastTrap.onDeactivation();
    }
  
    if (trap) {
      lastActiveFocus = null;
  
      if (!sameTrap || lastTrap.observed !== trap.observed) {
        trap.onActivation();
      }
  
      activateTrap(true);
      (0, _util.deferAction)(activateTrap);
    } else {
      detachHandler();
      lastActiveFocus = null;
    }
  }
  
  var _default = (0, _reactClientsideEffect.default)(reducePropsToState, handleStateChangeOnClient)(FocusWatcher);
  
  exports.default = _default;