"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"), _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = _default;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _reactDom = _interopRequireDefault(require("react-dom")), _propTypes = _interopRequireDefault(require("prop-types")), _amosFramework = require("amos-framework");

function _createSuper(e) {
    var t = _isNativeReflectConstruct();
    return function() {
        var r, n = (0, _getPrototypeOf2.default)(e);
        if (t) {
            var i = (0, _getPrototypeOf2.default)(this).constructor;
            r = Reflect.construct(n, arguments, i);
        } else r = n.apply(this, arguments);
        return (0, _possibleConstructorReturn2.default)(this, r);
    };
}

function _isNativeReflectConstruct() {
    if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
    if (Reflect.construct.sham) return !1;
    if ("function" == typeof Proxy) return !0;
    try {
        return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))), 
        !0;
    } catch (e) {
        return !1;
    }
}

var IS_REACT_16 = !!_reactDom.default.createPortal;

function _default(e, t) {
    var r = function(t) {
        (0, _inherits2.default)(n, t);
        var r = _createSuper(n);
        function n(t) {
            var i;
            return (0, _classCallCheck2.default)(this, n), i = r.call(this, t), (0, _defineProperty2.default)((0, 
            _assertThisInitialized2.default)(i), "getComponent", (function() {
                var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : {};
                return _react.default.createElement(e, (0, _extends2.default)({
                    ref: function(e) {
                        return i._component = e;
                    }
                }, i.props, t, {
                    key: "portal-wrapper"
                }));
            })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(i), "getContainer", (function() {
                if (i.props.getContainer) return i.props.getContainer();
                var e = document.createElement("div");
                return document.body.appendChild(e), e;
            })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(i), "removeContainer", (function() {
                if (i._container) {
                    var e = i._container;
                    _reactDom.default.unmountComponentAtNode(e), e.parentNode.removeChild(e), i._container = null;
                }
            })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(i), "renderComponent", (function(e, t) {
                if (!i.props.visible || i._component) {
                    i._container || (i._container = i.getContainer());
                    var r = i.getComponent(e);
                    _reactDom.default.unstable_renderSubtreeIntoContainer((0, _assertThisInitialized2.default)(i), r, i._container, (function() {
                        this._component = this, t && t.call(this);
                    }));
                }
            })), i.state = {
                visible: !1
            }, i;
        }
        return (0, _createClass2.default)(n, [ {
            key: "shouldComponentUpdate",
            value: function(e) {
                var t = e.visible;
                return !(!this.props.visible && !t);
            }
        }, {
            key: "componentWillUnmount",
            value: function() {
                IS_REACT_16 || (this.props.visible ? this.renderComponent({
                    afterLeave: this.removeContainer,
                    onLeave: function() {},
                    visible: !1
                }) : this.removeContainer());
            }
        }, {
            key: "render",
            value: function() {
                var e = this.props.visible, t = null;
                return IS_REACT_16 ? ((e || this._component) && (t = _react.default.createElement(_amosFramework.Portal, {
                    getContainer: this.getContainer
                }, this.getComponent())), t) : t;
            }
        } ]), n;
    }(_react.Component);
    return (0, _defineProperty2.default)(r, "propTypes", {
        visible: _propTypes.default.bool,
        getContainer: _propTypes.default.func
    }), r.displayName = "WrapperComponent", r;
}