var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import React from 'react';
import { RenderEnvironmentContext } from '../../lib/renderEnvironment/index.js';
import { getRandomID } from '../../lib/utils.js';
import { Upgrade } from '../../lib/Upgrades.js';
import { callChildRef } from '../../lib/callChildRef/callChildRef.js';
import { RenderInnerContainer } from './RenderInnerContainer.js';
export var PORTAL_INLET_ATTR = 'data-render-container-id';
export var PORTAL_OUTLET_ATTR = 'data-rendered-container-id';
var RenderContainer = /** @class */ (function (_super) {
    __extends(RenderContainer, _super);
    function RenderContainer() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.domContainer = RenderContainer.defaultRootNode;
        _this.rootId = RenderContainer.getRootId();
        _this.getRootNode = function () { return _this.domContainer; };
        return _this;
    }
    RenderContainer.prototype.shouldComponentUpdate = function (nextProps) {
        if (!this.props.children && nextProps.children) {
            this.mountContainer();
        }
        if (this.props.children && !nextProps.children) {
            this.unmountContainer();
        }
        return true;
    };
    RenderContainer.prototype.componentWillUnmount = function () {
        this.destroyContainer();
    };
    RenderContainer.prototype.render = function () {
        if (this.props.children) {
            this.mountContainer();
        }
        return React.createElement(RenderInnerContainer, __assign({}, this.props, { domContainer: this.domContainer, rootId: this.rootId }));
    };
    RenderContainer.prototype.createContainer = function () {
        var _a;
        var domContainer = (_a = this.context.globalObject.document) === null || _a === void 0 ? void 0 : _a.createElement('div');
        if (domContainer) {
            domContainer.setAttribute('class', Upgrade.getSpecificityClassName());
            domContainer.setAttribute(PORTAL_OUTLET_ATTR, "".concat(this.rootId));
            this.domContainer = domContainer;
        }
    };
    RenderContainer.prototype.mountContainer = function () {
        var _a, _b;
        var globalWithReactTesting = this.context.globalObject;
        if (!this.domContainer) {
            this.createContainer();
        }
        if (this.domContainer && this.domContainer.parentNode !== ((_a = this.context.globalObject.document) === null || _a === void 0 ? void 0 : _a.body)) {
            (_b = this.context.globalObject.document) === null || _b === void 0 ? void 0 : _b.body.appendChild(this.domContainer);
            if (this.props.containerRef) {
                callChildRef(this.props.containerRef, this.domContainer);
            }
            if (globalWithReactTesting.ReactTesting) {
                globalWithReactTesting.ReactTesting.addRenderContainer(this.rootId, this);
            }
        }
    };
    RenderContainer.prototype.destroyContainer = function () {
        if (this.domContainer) {
            this.unmountContainer();
            this.domContainer = null;
        }
    };
    RenderContainer.prototype.unmountContainer = function () {
        if (this.domContainer && this.domContainer.parentNode) {
            this.domContainer.parentNode.removeChild(this.domContainer);
            if (this.props.containerRef) {
                callChildRef(this.props.containerRef, null);
            }
            var globalWithReactTesting = this.context.globalObject;
            if (globalWithReactTesting.ReactTesting) {
                globalWithReactTesting.ReactTesting.removeRenderContainer(this.rootId);
            }
        }
    };
    RenderContainer.__KONTUR_REACT_UI__ = 'RenderContainer';
    RenderContainer.displayName = 'RenderContainer';
    RenderContainer.contextType = RenderEnvironmentContext;
    RenderContainer.getRootId = function () { return getRandomID(); };
    // see #2873 and #2895
    RenderContainer.defaultRootNode = null;
    return RenderContainer;
}(React.Component));
export { RenderContainer };
//# sourceMappingURL=RenderContainer.js.map