import _extends from "@babel/runtime/helpers/esm/extends";
import _inheritsLoose from "@babel/runtime/helpers/esm/inheritsLoose";
import React from 'react';
import { globalObject } from '@skbkontur/global-object';
import { getRandomID } from "../../../lib/utils";
import { Upgrade } from "../../../lib/Upgrades";
import { callChildRef } from "../../../lib/callChildRef/callChildRef";
import { RenderInnerContainer } from "../RenderInnerContainer";
export var PORTAL_INLET_ATTR = 'data-render-container-id';
export var PORTAL_OUTLET_ATTR = 'data-rendered-container-id';
export var 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;
  }
  _inheritsLoose(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.createElement(RenderInnerContainer, _extends({}, this.props, {
      domContainer: this.domContainer,
      rootId: this.rootId
    }));
  };
  _proto.createContainer = function createContainer() {
    var _globalObject$documen;
    var domContainer = (_globalObject$documen = globalObject.document) == null ? void 0 : _globalObject$documen.createElement('div');
    if (domContainer) {
      domContainer.setAttribute('class', Upgrade.getSpecificityClassName());
      domContainer.setAttribute(PORTAL_OUTLET_ATTR, "" + this.rootId);
      this.domContainer = domContainer;
    }
  };
  _proto.mountContainer = function mountContainer() {
    var _globalObject$documen2;
    var globalWithReactTesting = globalObject;
    if (!this.domContainer) {
      this.createContainer();
    }
    if (this.domContainer && this.domContainer.parentNode !== ((_globalObject$documen2 = globalObject.document) == null ? void 0 : _globalObject$documen2.body)) {
      var _globalObject$documen3;
      (_globalObject$documen3 = globalObject.document) == null || _globalObject$documen3.body.appendChild(this.domContainer);
      if (this.props.containerRef) {
        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) {
        callChildRef(this.props.containerRef, null);
      }
      var globalWithReactTesting = globalObject;
      if (globalWithReactTesting.ReactTesting) {
        globalWithReactTesting.ReactTesting.removeRenderContainer(this.rootId);
      }
    }
  };
  return RenderContainer;
}(React.Component);
RenderContainer.__KONTUR_REACT_UI__ = 'RenderContainer';
RenderContainer.displayName = 'RenderContainer';
RenderContainer.getRootId = function () {
  return getRandomID();
};