import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
var _class, _RenderLayer;
import React from 'react';
import { globalObject } from '@skbkontur/global-object';
import { listen as listenFocusOutside, containsTargetOrRenderContainer } from "../../../lib/listenFocusOutside";
import { CommonWrapper } from "../../CommonWrapper";
import { getRootNode, rootNode } from "../../../lib/rootNode";
import { createPropsGetter } from "../../../lib/createPropsGetter";
import { isInstanceOf } from "../../../lib/isInstanceOf";
export var RenderLayer = 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 = 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 || isInstanceOf(target, globalObject.Element) && containsTargetOrRenderContainer(target)(node)) {
        return;
      }
      if (_this.props.onClickOutside) {
        _this.props.onClickOutside(event);
      }
    };
    return _this;
  }
  _inheritsLoose(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.createElement(CommonWrapper, _extends({
      rootNodeRef: this.setRootNode
    }, this.props), React.Children.only(this.props.children));
  };
  _proto.getAnchorNode = function getAnchorNode() {
    var getAnchorElement = this.props.getAnchorElement;
    return getAnchorElement ? getAnchorElement() : getRootNode(this);
  };
  _proto.attachListeners = function attachListeners() {
    var _globalObject$documen;
    var node = this.getAnchorNode();
    if (!node) {
      return;
    }
    this.focusOutsideListenerToken = listenFocusOutside(function () {
      return [node];
    }, this.handleFocusOutside);
    globalObject.addEventListener == null || globalObject.addEventListener('blur', this.handleFocusOutside);
    (_globalObject$documen = globalObject.document) == null || _globalObject$documen.addEventListener('ontouchstart' in globalObject.document.documentElement && 'onpointerup' in globalObject.document.documentElement ? 'pointerup' : 'mousedown', this.handleNativeDocClick);
  };
  _proto.detachListeners = function detachListeners() {
    var _globalObject$documen2;
    if (this.focusOutsideListenerToken) {
      this.focusOutsideListenerToken.remove();
      this.focusOutsideListenerToken = null;
    }
    globalObject.removeEventListener == null || globalObject.removeEventListener('blur', this.handleFocusOutside);
    (_globalObject$documen2 = globalObject.document) == null || _globalObject$documen2.removeEventListener('ontouchstart' in globalObject.document.documentElement && 'onpointerup' in globalObject.document.documentElement ? 'pointerup' : 'mousedown', this.handleNativeDocClick);
  };
  return RenderLayer;
}(React.Component), _RenderLayer.__KONTUR_REACT_UI__ = 'RenderLayer', _RenderLayer.displayName = 'RenderLayer', _RenderLayer.defaultProps = {
  active: true
}, _RenderLayer)) || _class;