"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _focusScopeUtil = _interopRequireDefault(require("./focusScopeUtil"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

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; }

/**
 * Automatically start autofocus the focusable first element once mounted within the container(elementRef) and 
 * Also create inside focus looping the container(elementRef)
 * Also we do restore focus for popup and Modal scenarios  
 * Example : use
function Component(props){

    let elemenetRef = React.createRef();

    useFocusScope({ autoFocus: true , focusLoop: false, elemenetRef });

    return <div ref={elemenetRef}>

            <input /> <!-- default focusable tags   -->
            <button /> <!-- default focusable tags  -->
            <div data-aria-focus tabindex /> <!-- create own focusable tag with focus options  -->
    </div>

}
 * By default all (autoFocus, focusLoop, restoreFocus) will be false.
 * param - autoFocus , focusLoop, restoreFocus, ref
 * return void 
 */
var _default = function _default(_ref) {
  var _ref$autoFocus = _ref.autoFocus,
      autoFocus = _ref$autoFocus === void 0 ? false : _ref$autoFocus,
      _ref$focusLoop = _ref.focusLoop,
      focusLoop = _ref$focusLoop === void 0 ? false : _ref$focusLoop,
      _ref$restoreFocus = _ref.restoreFocus,
      restoreFocus = _ref$restoreFocus === void 0 ? false : _ref$restoreFocus,
      _ref$getElementRef = _ref.getElementRef,
      getElementRef = _ref$getElementRef === void 0 ? null : _ref$getElementRef,
      _ref$restoreFocusId = _ref.restoreFocusId,
      restoreFocusId = _ref$restoreFocusId === void 0 ? '1' : _ref$restoreFocusId,
      _ref$isNested = _ref.isNested,
      isNested = _ref$isNested === void 0 ? false : _ref$isNested;

  /*let getElementRef =()=>{
      return elementRef && elementRef.current || null;
  };*/
  (0, _react.useEffect)(function () {
    var elemenetRef = getElementRef && getElementRef();
    autoFocus && _focusScopeUtil["default"].autoFocus(elemenetRef, autoFocus);
    focusLoop && _focusScopeUtil["default"].focusLoop(elemenetRef, focusLoop);
    restoreFocus && _focusScopeUtil["default"].restoreFocus(restoreFocus, restoreFocusId, isNested);
    return function () {
      _focusScopeUtil["default"].focusLoop(elemenetRef, false);

      _focusScopeUtil["default"].restoreFocus(false, restoreFocusId, isNested);
    };
  }, []); // Didmount , un-mount

  (0, _react.useEffect)(function () {
    var elemenetRef = getElementRef && getElementRef();

    _focusScopeUtil["default"].autoFocus(elemenetRef, autoFocus);
  }, [autoFocus]); // didUpdate when updatinge the component by api.

  (0, _react.useEffect)(function () {
    var elemenetRef = getElementRef && getElementRef();

    _focusScopeUtil["default"].focusLoop(elemenetRef, focusLoop);
  }, [focusLoop]); // didUpdate when updatinge the component by api.
};

exports["default"] = _default;