function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import React from 'react';
import PropTypes from 'prop-types';
import { Portal } from 'react-portal';

var TabBody = function (_React$Component) {
  _inherits(TabBody, _React$Component);

  function TabBody(props) {
    _classCallCheck(this, TabBody);

    var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));

    _this.state = {
      nodeList: []
    };
    if (global.MutationObserver) {
      _this.observer = new MutationObserver(function (mutations) {
        var tabContainer = document.getElementById(_this.props.tabContainerBodyId);
        mutations.forEach(function (mutation) {
          if (!mutation.target) {
            return;
          }
          if (_this.props.active && mutation.target.contains(tabContainer)) {
            _this.setState({ nodeList: [tabContainer] });
          } else if (!_this.props.active && mutation.target.contains(tabContainer)) {
            _this.setState({ nodeList: [] });
          }
        });
      });
    }
    return _this;
  }

  TabBody.prototype.componentDidMount = function componentDidMount() {
    var nodeList = document.getElementById(this.props.tabContainerBodyId);
    if (nodeList) {
      // eslint-disable-next-line react/no-did-mount-set-state
      this.setState({ nodeList: [nodeList] });
    }
    if (global.MutationObserver) {
      this.observer.observe(document, { attributes: true, childList: true, subtree: true });
    }
  };

  TabBody.prototype.componentWillUnmount = function componentWillUnmount() {
    if (global.MutationObserver) {
      this.observer.disconnect();
    }
  };

  TabBody.prototype.render = function render() {
    if (this.state.nodeList.length > 0 && this.props.active) {
      return React.createElement(
        Portal,
        { node: this.state.nodeList[0] },
        this.props.children
      );
    }
    return null;
  };

  return TabBody;
}(React.Component);

TabBody.propTypes = process.env.NODE_ENV !== "production" ? {
  /** The tab container body to render Tab children into. */
  tabContainerBodyId: PropTypes.string.isRequired,
  /** Sets if the tab is the currently active tab or not. */
  active: PropTypes.bool,
  /** Children passed to tab body (tab content) */
  children: PropTypes.node
} : {};

export default TabBody;