text: function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  
  function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  
  function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  
  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  
  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  
  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  
  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  
  import React from 'react';
  import ReactDOM from 'react-dom';
  import PropTypes from 'prop-types';
  import { polyfill } from 'react-lifecycles-compat';
  import ContainerRender from './ContainerRender';
  import Portal from './Portal';
  var openCount = 0;
  var windowIsUndefined = !(typeof window !== 'undefined' && window.document && window.document.createElement);
  var IS_REACT_16 = 'createPortal' in ReactDOM;
  
  var PortalWrapper =
  /*#__PURE__*/
  function (_React$Component) {
    _inherits(PortalWrapper, _React$Component);
  
    function PortalWrapper(props) {
      var _this;
  
      _classCallCheck(this, PortalWrapper);
  
      _this = _possibleConstructorReturn(this, _getPrototypeOf(PortalWrapper).call(this, props));
  
      _this.getParent = function () {
        var getContainer = _this.props.getContainer;
  
        if (getContainer) {
          if (typeof getContainer === 'string') {
            return document.querySelectorAll(getContainer)[0];
          }
  
          if (typeof getContainer === 'function') {
            return getContainer();
          }
  
          if (_typeof(getContainer) === 'object' && getContainer instanceof window.HTMLElement) {
            return getContainer;
          }
        }
  
        return document.body;
      };
  
      _this.getContainer = function () {
        if (windowIsUndefined) {
          return null;
        }
  
        if (!_this.container) {
          _this.container = document.createElement('div');
  
          var parent = _this.getParent();
  
          if (parent) {
            parent.appendChild(_this.container);
          }
        }
  
        _this.setWrapperClassName();
  
        return _this.container;
      };
  
      _this.setWrapperClassName = function () {
        var wrapperClassName = _this.props.wrapperClassName;
  
        if (_this.container && wrapperClassName && wrapperClassName !== _this.container.className) {
          _this.container.className = wrapperClassName;
        }
      };
  
      _this.savePortal = function (c) {
        _this._component = c;
      };
  
      _this.removeCurrentContainer = function (visible) {
        _this.container = null;
        _this._component = null;
  
        if (!IS_REACT_16) {
          if (visible) {
            _this.renderComponent({
              afterClose: _this.removeContainer,
              onClose: function onClose() {},
              visible: false
            });
          } else {
            _this.removeContainer();
          }
        }
      };
  
      var _visible = props.visible;
      openCount = _visible ? openCount + 1 : openCount;
      _this.state = {
        _self: _assertThisInitialized(_this)
      };
      return _this;
    }
  
    _createClass(PortalWrapper, [{
      key: "componentDidUpdate",
      value: function componentDidUpdate() {
        this.setWrapperClassName();
      }
    }, {
      key: "componentWillUnmount",
      value: function componentWillUnmount() {
        var visible = this.props.visible; // 离开时不会 render， 导到离开时数值不变，改用 func 。。
  
        openCount = visible && openCount ? openCount - 1 : openCount;
        this.removeCurrentContainer(visible);
      }
    }, {
      key: "render",
      value: function render() {
        var _this2 = this;
  
        var _this$props = this.props,
            children = _this$props.children,
            forceRender = _this$props.forceRender,
            visible = _this$props.visible;
        var portal = null;
        var childProps = {
          getOpenCount: function getOpenCount() {
            return openCount;
          },
          getContainer: this.getContainer
        }; // suppport react15
  
        if (!IS_REACT_16) {
          return React.createElement(ContainerRender, {
            parent: this,
            visible: visible,
            autoDestroy: false,
            getComponent: function getComponent() {
              var extra = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
              return children(_objectSpread({}, extra, {}, childProps, {
                ref: _this2.savePortal
              }));
            },
            getContainer: this.getContainer,
            forceRender: forceRender
          }, function (_ref) {
            var renderComponent = _ref.renderComponent,
                removeContainer = _ref.removeContainer;
            _this2.renderComponent = renderComponent;
            _this2.removeContainer = removeContainer;
            return null;
          });
        }
  
        if (forceRender || visible || this._component) {
          portal = React.createElement(Portal, {
            getContainer: this.getContainer,
            ref: this.savePortal
          }, children(childProps));
        }
  
        return portal;
      }
    }], [{
      key: "getDerivedStateFromProps",
      value: function getDerivedStateFromProps(props, _ref2) {
        var prevProps = _ref2.prevProps,
            _self = _ref2._self;
        var visible = props.visible,
            getContainer = props.getContainer;
  
        if (prevProps) {
          var prevVisible = prevProps.visible,
              prevGetContainer = prevProps.getContainer;
  
          if (visible !== prevVisible) {
            openCount = visible && !prevVisible ? openCount + 1 : openCount - 1;
          }
  
          var getContainerIsFunc = typeof getContainer === 'function' && typeof prevGetContainer === 'function';
  
          if (getContainerIsFunc ? getContainer.toString() !== prevGetContainer.toString() : getContainer !== prevGetContainer) {
            _self.removeCurrentContainer(false);
          }
        }
  
        return {
          prevProps: props
        };
      }
    }]);
  
    return PortalWrapper;
  }(React.Component);
  
  PortalWrapper.propTypes = {
    wrapperClassName: PropTypes.string,
    forceRender: PropTypes.bool,
    getContainer: PropTypes.any,
    children: PropTypes.func,
    visible: PropTypes.bool
  };
  export default polyfill(PortalWrapper);