'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactDom = require('react-dom');

var _reactDom2 = _interopRequireDefault(_reactDom);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _utils = require('../utils/utils');

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

// decorator
exports.default = function () {
	var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
	return function (WrappedComponent) {
		var el = options.el,
		    _root = options.root;

		var Viewer = function (_Component) {
			(0, _inherits3.default)(Viewer, _Component);

			function Viewer(props) {
				(0, _classCallCheck3.default)(this, Viewer);

				var _this = (0, _possibleConstructorReturn3.default)(this, (Viewer.__proto__ || Object.getPrototypeOf(Viewer)).call(this, props));

				_this.el = document.createElement(el || 'div');
				_this.root = document.querySelector(_root || 'body');

				_this.el.setAttribute('rc-root-uuid', (0, _utils.getUid)());
				return _this;
			}

			(0, _createClass3.default)(Viewer, [{
				key: 'componentDidMount',
				value: function componentDidMount() {
					this.root.appendChild(this.el);
				}
			}, {
				key: 'componentWillReceiveProps',
				value: function componentWillReceiveProps() {
					// ??
				}
			}, {
				key: 'componentWillUnmount',
				value: function componentWillUnmount() {
					this.root.removeChild(this.el);
				}
			}, {
				key: 'render',
				value: function render() {
					return _reactDom2.default.createPortal(_react2.default.createElement(WrappedComponent, this.props), this.el);
				}
			}]);
			return Viewer;
		}(_react.Component);

		return Viewer;
	};
};