"use strict";

var _typeof = require("@babel/runtime/helpers/typeof");

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

var _react = _interopRequireWildcard(require("react"));

var _utils = require("./utils");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var FocusRing = function FocusRing(_ref) {
  var topFocusBumperRef = _ref.topFocusBumperRef,
      bottomFocusBumperRef = _ref.bottomFocusBumperRef,
      children = _ref.children;
  return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
    ref: topFocusBumperRef,
    tabIndex: 0
  }), children, /*#__PURE__*/_react.default.createElement("div", {
    ref: bottomFocusBumperRef,
    tabIndex: 0
  }));
};

var openFocusTraps = new Set();

var getLastItemInSet = function getLastItemInSet(set) {
  var array = Array.from(set);
  return array[array.length - 1];
};

var useFocusTrap = function useFocusTrap(_ref2) {
  var active = _ref2.active,
      _ref2$returnFocusToSo = _ref2.returnFocusToSource,
      returnFocusToSource = _ref2$returnFocusToSo === void 0 ? true : _ref2$returnFocusToSo,
      initialFocusRef = _ref2.initialFocusRef;
  var ref = (0, _react.useRef)(null);
  var topFocusBumperRef = (0, _react.useRef)(null);
  var bottomFocusBumperRef = (0, _react.useRef)(null);
  var InternalFocusRing = (0, _react.useCallback)(function (_ref3) {
    var children = _ref3.children;
    return /*#__PURE__*/_react.default.createElement(FocusRing, {
      topFocusBumperRef: topFocusBumperRef,
      bottomFocusBumperRef: bottomFocusBumperRef
    }, children);
  }, []);
  (0, _react.useEffect)(function () {
    if (active) {
      var sourceFocusElement = returnFocusToSource && document.activeElement;
      var currentFocusRef = ref.current;

      if (!currentFocusRef) {
        return;
      }

      if (openFocusTraps.size > 0) {
        document.removeEventListener('focusin', getLastItemInSet(openFocusTraps), true);
      }

      if (initialFocusRef) {
        var currentInitialFocusRef = initialFocusRef.current;

        if (currentInitialFocusRef) {
          var foundFocus = (0, _utils.focusOnFirstDescendent)(currentInitialFocusRef);

          if (!foundFocus) {
            (0, _utils.focusOnFirstDescendent)(topFocusBumperRef.current);
          }
        }
      } else if (typeof initialFocusRef === 'undefined') {
        (0, _utils.focusOnFirstDescendent)(currentFocusRef);
      } else {
        (0, _utils.focusOnFirstDescendent)(topFocusBumperRef.current);
      }

      var trapFocus = function trapFocus(event) {
        var eventTarget = event.target; // If focus is called on CkeEditor Element (for example Link dialog), we want to keep focus there, not back in our dialog

        var isCkEditorElement = eventTarget.className.includes('cke');

        if (!isCkEditorElement && !currentFocusRef.contains(eventTarget)) {
          if (document.activeElement === event.target) {
            if (event.target === topFocusBumperRef.current) {
              // Reset the focus to the last element when focusing in reverse (shift-tab)
              (0, _utils.focusOnLastDescendent)(currentFocusRef);
            } else {
              // Reset the focus to the first element after reaching
              // the last element or any other element outside of the focus trap
              (0, _utils.focusOnFirstDescendent)(currentFocusRef);
            }
          }
        }
      };

      openFocusTraps.add(trapFocus);
      document.addEventListener('focusin', trapFocus, true);
      return function cleanup() {
        document.removeEventListener('focusin', trapFocus, true);
        openFocusTraps.delete(trapFocus);

        if (sourceFocusElement && (0, _utils.isHTMLElement)(sourceFocusElement)) {
          sourceFocusElement.focus();
        }

        if (openFocusTraps.size > 0) {
          document.addEventListener('focusin', getLastItemInSet(openFocusTraps), true);
        }
      };
    }
  }, [active, returnFocusToSource, ref, initialFocusRef]);
  return {
    ref: ref,
    FocusRing: InternalFocusRing
  };
};

var _default = useFocusTrap;
exports.default = _default;