"use strict";
Object.defineProperty(exports, "__esModule", {
  value: !0
}), exports.default = _default;
var _react = _interopRequireWildcard(require("react")),
  _reactDom = _interopRequireDefault(require("react-dom")),
  _propTypes = _interopRequireDefault(require("prop-types")),
  _amosFramework = require("amos-framework");

function _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  }
}

function _getRequireWildcardCache() {
  if ("function" != typeof WeakMap) return null;
  var e = new WeakMap;
  return _getRequireWildcardCache = function() {
    return e
  }, e
}

function _interopRequireWildcard(e) {
  if (e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var t = _getRequireWildcardCache();
  if (t && t.has(e)) return t.get(e);
  var r, n, o = {},
    i = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (r in e) Object.prototype.hasOwnProperty.call(e, r) && ((n = i ? Object.getOwnPropertyDescriptor(e, r) : null) && (n.get || n.set) ? Object.defineProperty(o, r, n) : o[r] = e[r]);
  return o.default = e, t && t.set(e, o), o
}

function _typeof(e) {
  return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e
  })(e)
}

function _extends() {
  return (_extends = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var r, n = arguments[t];
      for (r in n) Object.prototype.hasOwnProperty.call(n, r) && (e[r] = n[r])
    }
    return e
  }).apply(this, arguments)
}

function _classCallCheck(e, t) {
  if (!(e instanceof t)) throw new TypeError("Cannot call a class as a function")
}

function _defineProperties(e, t) {
  for (var r = 0; r < t.length; r++) {
    var n = t[r];
    n.enumerable = n.enumerable || !1, n.configurable = !0, "value" in n && (n.writable = !0), Object.defineProperty(e, n.key, n)
  }
}

function _createClass(e, t, r) {
  return t && _defineProperties(e.prototype, t), r && _defineProperties(e, r), e
}

function _inherits(e, t) {
  if ("function" != typeof t && null !== t) throw new TypeError("Super expression must either be null or a function");
  e.prototype = Object.create(t && t.prototype, {
    constructor: {
      value: e,
      writable: !0,
      configurable: !0
    }
  }), t && _setPrototypeOf(e, t)
}

function _setPrototypeOf(e, t) {
  return (_setPrototypeOf = Object.setPrototypeOf || function(e, t) {
    return e.__proto__ = t, e
  })(e, t)
}

function _createSuper(r) {
  var n = _isNativeReflectConstruct();
  return function() {
    var e, t = _getPrototypeOf(r);
    return _possibleConstructorReturn(this, n ? (e = _getPrototypeOf(this).constructor, Reflect.construct(t, arguments, e)) : t.apply(this, arguments))
  }
}

function _possibleConstructorReturn(e, t) {
  return !t || "object" !== _typeof(t) && "function" != typeof t ? _assertThisInitialized(e) : t
}

function _assertThisInitialized(e) {
  if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  return e
}

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

function _getPrototypeOf(e) {
  return (_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(e) {
    return e.__proto__ || Object.getPrototypeOf(e)
  })(e)
}

function _defineProperty(e, t, r) {
  return t in e ? Object.defineProperty(e, t, {
    value: r,
    enumerable: !0,
    configurable: !0,
    writable: !0
  }) : e[t] = r, e
}
var IS_REACT_16 = !!_reactDom.default.createPortal;

function _default(o, e) {
  var t = function() {
    _inherits(n, _react.Component);
    var t = _createSuper(n);

    function n(e) {
      var r;
      return _classCallCheck(this, n), _defineProperty(_assertThisInitialized(r = t.call(this, e)), "getComponent", function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
        return _react.default.createElement(o, _extends({
          ref: function(e) {
            return r._component = e
          }
        }, r.props, e, {
          key: "portal-wrapper"
        }))
      }), _defineProperty(_assertThisInitialized(r), "getContainer", function() {
        if (r.props.getContainer) return r.props.getContainer();
        var e = document.createElement("div");
        return document.body.appendChild(e), e
      }), _defineProperty(_assertThisInitialized(r), "removeContainer", function() {
        var e;
        r._container && (e = r._container, _reactDom.default.unmountComponentAtNode(e), e.parentNode.removeChild(e), r._container = null)
      }), _defineProperty(_assertThisInitialized(r), "renderComponent", function(e, t) {
        r.props.visible && !r._component || (r._container || (r._container = r.getContainer()), e = r.getComponent(e), _reactDom.default.unstable_renderSubtreeIntoContainer(_assertThisInitialized(r), e, r._container, function() {
          this._component = this, t && t.call(this)
        }))
      }), r.state = {
        visible: !1
      }, r
    }
    return _createClass(n, [{
      key: "shouldComponentUpdate",
      value: function(e) {
        e = e.visible;
        return !(!this.props.visible && !e)
      }
    }, {
      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
      }
    }]), n
  }();
  return _defineProperty(t, "propTypes", {
    visible: _propTypes.default.bool,
    getContainer: _propTypes.default.func
  }), t.displayName = "WrapperComponent", t
}
module.exports = exports.default;
