import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _class, _FocusTrap;
import React from 'react';
import { globalObject } from '@skbkontur/global-object';
import { CommonWrapper } from "../../../internal/CommonWrapper";
import { listen as listenFocusOutside, containsTargetOrRenderContainer } from "../../../lib/listenFocusOutside";
import { getRootNode, rootNode } from "../../../lib/rootNode";
import { isInstanceOf } from "../../../lib/isInstanceOf";
export var FocusTrap = 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 = getRootNode(_this);
      if (!_this.focusOutsideListenerToken && rootNode) {
        var _globalObject$documen;
        _this.focusOutsideListenerToken = listenFocusOutside([rootNode], _this.onClickOutside);
        (_globalObject$documen = globalObject.document) == null || _globalObject$documen.addEventListener('ontouchstart' in globalObject.document.documentElement ? 'touchstart' : 'mousedown', _this.handleNativeDocClick);
      }
    };
    _this.handleNativeDocClick = function (event) {
      var target = event.target || event.srcElement;
      var node = getRootNode(_this);
      if (node && isInstanceOf(target, globalObject.Element) && containsTargetOrRenderContainer(target)(node)) {
        return;
      }
      _this.onClickOutside(event);
    };
    return _this;
  }
  _inheritsLoose(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.createElement(CommonWrapper, _extends({
      rootNodeRef: this.setRootNode
    }, this.props), /*#__PURE__*/React.cloneElement(React.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.document) == null || _globalObject$documen2.removeEventListener('ontouchstart' in globalObject.document.documentElement ? 'touchstart' : 'mousedown', this.handleNativeDocClick);
    }
  };
  return FocusTrap;
}(React.PureComponent), _FocusTrap.__KONTUR_REACT_UI__ = 'FocusTrap', _FocusTrap.displayName = 'FocusTrap', _FocusTrap)) || _class;