"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.FocusTrap = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));var _react = _interopRequireDefault(require("react"));
var _globalObject = require("@skbkontur/global-object");


var _CommonWrapper = require("../../internal/CommonWrapper");
var _listenFocusOutside = require("../../lib/listenFocusOutside");

var _rootNode = require("../../lib/rootNode");
var _isInstanceOf = require("../../lib/isInstanceOf");var _class, _FocusTrap;var







FocusTrap = exports.FocusTrap = (0, _rootNode.rootNode)(_class = (_FocusTrap = /*#__PURE__*/function (_React$PureComponent) {function FocusTrap() {var _this;for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {args[_key] = arguments[_key];}_this = _React$PureComponent.call.apply(_React$PureComponent, [this].concat(args)) || this;_this.






    focusOutsideListenerToken =

    null;_this.

























    onClickOutside = function (e) {
      if (_this.props.onBlur) {
        _this.props.onBlur(e);
      }
      _this.detachListeners();
    };_this.

    attachListeners = function () {
      var rootNode = (0, _rootNode.getRootNode)(_this);
      if (!_this.focusOutsideListenerToken && rootNode) {var _globalObject$documen;
        _this.focusOutsideListenerToken = (0, _listenFocusOutside.listen)([rootNode], _this.onClickOutside);

        (_globalObject$documen = _globalObject.globalObject.document) == null || _globalObject$documen.addEventListener(
          'ontouchstart' in _globalObject.globalObject.document.documentElement ? 'touchstart' : 'mousedown',
          _this.handleNativeDocClick
        );
      }
    };_this.













    handleNativeDocClick = function (event) {
      var target = event.target || event.srcElement;
      var node = (0, _rootNode.getRootNode)(_this);

      if (node && (0, _isInstanceOf.isInstanceOf)(target, _globalObject.globalObject.Element) && (0, _listenFocusOutside.containsTargetOrRenderContainer)(target)(node)) {
        return;
      }

      _this.onClickOutside(event);
    };return _this;}(0, _inheritsLoose2.default)(FocusTrap, _React$PureComponent);var _proto = FocusTrap.prototype;_proto.componentWillUnmount = function componentWillUnmount() {if (this.focusOutsideListenerToken) {this.detachListeners();}};_proto.render = function render() {var _this2 = this;var _this$props = this.props,children = _this$props.children,onBlur = _this$props.onBlur;return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: this.setRootNode }, this.props), /*#__PURE__*/_react.default.cloneElement(_react.default.Children.only(children), { onFocus: function onFocus() {if (onBlur) {_this2.attachListeners();}if (children.props && children.props.onFocus) {var _children$props;(_children$props = children.props).onFocus.apply(_children$props, arguments);}} }));};_proto.detachListeners = function detachListeners() {if (this.focusOutsideListenerToken) {var _globalObject$documen2;this.focusOutsideListenerToken.remove();this.focusOutsideListenerToken = null;(_globalObject$documen2 = _globalObject.globalObject.document) == null || _globalObject$documen2.removeEventListener('ontouchstart' in _globalObject.globalObject.document.documentElement ? 'touchstart' : 'mousedown', this.handleNativeDocClick);}};return FocusTrap;}(_react.default.PureComponent), _FocusTrap.__KONTUR_REACT_UI__ = 'FocusTrap', _FocusTrap.displayName = 'FocusTrap', _FocusTrap)) || _class;