"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.RenderContainer = exports.PORTAL_OUTLET_ATTR = exports.PORTAL_INLET_ATTR = 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 _utils = require("../../lib/utils");
var _Upgrades = require("../../lib/Upgrades");
var _callChildRef = require("../../lib/callChildRef/callChildRef");

var _RenderInnerContainer = require("./RenderInnerContainer");






var PORTAL_INLET_ATTR = exports.PORTAL_INLET_ATTR = 'data-render-container-id';
var PORTAL_OUTLET_ATTR = exports.PORTAL_OUTLET_ATTR = 'data-rendered-container-id';var

RenderContainer = exports.RenderContainer = /*#__PURE__*/function (_React$Component) {function RenderContainer() {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.




    domContainer = null;_this.

    rootId = RenderContainer.getRootId();return _this;}(0, _inheritsLoose2.default)(RenderContainer, _React$Component);var _proto = RenderContainer.prototype;_proto.

  shouldComponentUpdate = function shouldComponentUpdate(nextProps) {
    if (!this.props.children && nextProps.children) {
      this.mountContainer();
    }
    if (this.props.children && !nextProps.children) {
      this.unmountContainer();
    }
    return true;
  };_proto.

  componentWillUnmount = function componentWillUnmount() {
    this.destroyContainer();
  };_proto.

  render = function render() {
    if (this.props.children) {
      this.mountContainer();
    }

    return /*#__PURE__*/_react.default.createElement(_RenderInnerContainer.RenderInnerContainer, (0, _extends2.default)({}, this.props, { domContainer: this.domContainer, rootId: this.rootId }));
  };_proto.

  createContainer = function createContainer() {var _globalObject$documen;
    var domContainer = (_globalObject$documen = _globalObject.globalObject.document) == null ? void 0 : _globalObject$documen.createElement('div');
    if (domContainer) {
      domContainer.setAttribute('class', _Upgrades.Upgrade.getSpecificityClassName());
      domContainer.setAttribute(PORTAL_OUTLET_ATTR, "" + this.rootId);
      this.domContainer = domContainer;
    }
  };_proto.

  mountContainer = function mountContainer() {var _globalObject$documen2;
    var globalWithReactTesting = _globalObject.globalObject;

    if (!this.domContainer) {
      this.createContainer();
    }
    if (this.domContainer && this.domContainer.parentNode !== ((_globalObject$documen2 = _globalObject.globalObject.document) == null ? void 0 : _globalObject$documen2.body)) {var _globalObject$documen3;
      (_globalObject$documen3 = _globalObject.globalObject.document) == null || _globalObject$documen3.body.appendChild(this.domContainer);

      if (this.props.containerRef) {
        (0, _callChildRef.callChildRef)(this.props.containerRef, this.domContainer);
      }
      if (globalWithReactTesting.ReactTesting) {
        globalWithReactTesting.ReactTesting.addRenderContainer(this.rootId, this);
      }
    }
  };_proto.

  destroyContainer = function destroyContainer() {
    if (this.domContainer) {
      this.unmountContainer();
      this.domContainer = null;
    }
  };_proto.

  unmountContainer = function unmountContainer() {
    if (this.domContainer && this.domContainer.parentNode) {
      this.domContainer.parentNode.removeChild(this.domContainer);

      if (this.props.containerRef) {
        (0, _callChildRef.callChildRef)(this.props.containerRef, null);
      }

      var globalWithReactTesting = _globalObject.globalObject;
      if (globalWithReactTesting.ReactTesting) {
        globalWithReactTesting.ReactTesting.removeRenderContainer(this.rootId);
      }
    }
  };return RenderContainer;}(_react.default.Component);RenderContainer.__KONTUR_REACT_UI__ = 'RenderContainer';RenderContainer.displayName = 'RenderContainer';RenderContainer.getRootId = function () {return (0, _utils.getRandomID)();};