"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.RenderLayer = 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 _listenFocusOutside = require("../../lib/listenFocusOutside");

var _CommonWrapper = require("../CommonWrapper");

var _rootNode = require("../../lib/rootNode");

var _createPropsGetter = require("../../lib/createPropsGetter");
var _isInstanceOf = require("../../lib/isInstanceOf");var _class, _RenderLayer;var












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







    getProps = (0, _createPropsGetter.createPropsGetter)(RenderLayer.defaultProps);_this.

    focusOutsideListenerToken =

    null;_this.






































































    handleFocusOutside = function (event) {
      if (_this.props.onFocusOutside) {
        _this.props.onFocusOutside(event);
      }
    };_this.

    handleNativeDocClick = function (event) {
      var target = event.target || event.srcElement;
      var node = _this.getAnchorNode();

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

      if (_this.props.onClickOutside) {
        _this.props.onClickOutside(event);
      }
    };return _this;}(0, _inheritsLoose2.default)(RenderLayer, _React$Component);var _proto = RenderLayer.prototype;_proto.componentDidMount = function componentDidMount() {if (this.getProps().active) {this.attachListeners();}};_proto.componentDidUpdate = function componentDidUpdate(prevProps) {var _this$getProps = this.getProps(),active = _this$getProps.active;if (!prevProps.active && active) {this.attachListeners();}if (prevProps.active && !active) {this.detachListeners();}};_proto.componentWillUnmount = function componentWillUnmount() {if (this.getProps().active) {this.detachListeners();}};_proto.render = function render() {return /*#__PURE__*/_react.default.createElement(_CommonWrapper.CommonWrapper, (0, _extends2.default)({ rootNodeRef: this.setRootNode }, this.props), _react.default.Children.only(this.props.children));};_proto.getAnchorNode = function getAnchorNode() {var getAnchorElement = this.props.getAnchorElement;return getAnchorElement ? getAnchorElement() : (0, _rootNode.getRootNode)(this);};_proto.attachListeners = function attachListeners() {var _globalObject$documen;var node = this.getAnchorNode();if (!node) {return;}this.focusOutsideListenerToken = (0, _listenFocusOutside.listen)(function () {return [node];}, this.handleFocusOutside);_globalObject.globalObject.addEventListener == null || _globalObject.globalObject.addEventListener('blur', this.handleFocusOutside);(_globalObject$documen = _globalObject.globalObject.document) == null || _globalObject$documen.addEventListener('ontouchstart' in _globalObject.globalObject.document.documentElement && 'onpointerup' in _globalObject.globalObject.document.documentElement ? 'pointerup' : 'mousedown', this.handleNativeDocClick);};_proto.detachListeners = function detachListeners() {var _globalObject$documen2;if (this.focusOutsideListenerToken) {this.focusOutsideListenerToken.remove();this.focusOutsideListenerToken = null;}_globalObject.globalObject.removeEventListener == null || _globalObject.globalObject.removeEventListener('blur', this.handleFocusOutside);(_globalObject$documen2 = _globalObject.globalObject.document) == null || _globalObject$documen2.removeEventListener('ontouchstart' in _globalObject.globalObject.document.documentElement && 'onpointerup' in _globalObject.globalObject.document.documentElement ? 'pointerup' : 'mousedown', this.handleNativeDocClick);};return RenderLayer;}(_react.default.Component), _RenderLayer.__KONTUR_REACT_UI__ = 'RenderLayer', _RenderLayer.displayName = 'RenderLayer', _RenderLayer.defaultProps = { active: true }, _RenderLayer)) || _class;