'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 _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');

var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _reactDom2 = _interopRequireDefault(_reactDom);

var _index = require('../rc-instance/index');

var _index2 = _interopRequireDefault(_index);

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 _class, _temp;

		var isNeedWaiting = false;
		var cName = options.cName,
		    onBefore = options.onBefore,
		    el = options.el,
		    _root = options.root;


		if (!cName) {
			console.log('cName 必传');
			return;
		}

		var Statics = {
			init: function init() {
				var _this = this;

				var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

				return new Promise(function (resolve, reject) {
					// create container
					var container = document.createElement(el || 'div');
					var target = document.querySelector(_root || 'body');
					container.setAttribute('rc-root-uuid', (0, _utils.getUid)());

					// init opts
					var _opts = opts,
					    parent = _opts.parent,
					    getInstance = _opts.getInstance,
					    _onBefore = _opts.onBefore,
					    _cName = _opts.cName,
					    rest = (0, _objectWithoutProperties3.default)(_opts, ['parent', 'getInstance', 'onBefore', 'cName']);

					onBefore = _onBefore || onBefore;
					cName = _cName || cName;

					// constructor opts
					opts = (0, _extends3.default)({}, rest, {
						show: true,
						onCloseSoon: function onCloseSoon() {
							_reactDom2.default.unmountComponentAtNode(container);
							target.removeChild(container);
							delete _index2.default.APIS[cName];
						},
						onSure: function onSure(res) {
							opts.onCloseSoon();
							resolve(res);
						},
						onClose: function onClose(res) {
							opts.onCloseSoon();
							reject(res);
						}
					});
					var render = function render() {
						var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

						target.appendChild(container);

						// destory
						_index2.default.APIS[cName] && Viewer.destroy();
						_index2.default.APIS[cName] = container;

						var element = _react2.default.createElement(Viewer, (0, _extends3.default)({}, opts, {
							data: res.data,
							ref: function ref(instance) {
								return _this.comp = instance;
							}
						}));
						var callback = function callback() {
							isNeedWaiting = false;
							getInstance && getInstance(_this.comp, opts.onSure, opts.onClose);
						};

						if (parent) {
							// 可以接入redux
							_reactDom2.default.unstable_renderSubtreeIntoContainer(parent, element, container, callback);
						} else {
							_reactDom2.default.render(element, container, callback);
						}
					};
					if (onBefore) {
						if (isNeedWaiting) {
							container = null;
							opts = null;
							render = null;
						} else {
							isNeedWaiting = true;
							onBefore((0, _extends3.default)({}, opts)).then(function () {
								var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

								render(res);
							}).catch(function () {
								var res = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

								isNeedWaiting = false;
								reject(res);
							});
						}
						return;
					}
					isNeedWaiting = false;
					render();
				});
			},


			/**
    * 弹出项目，验证数据结构是否合法
    * opts {
    * 	parent
    * 	getInstance,
    * 	param: {
    * 	}
    * }
    */
			popup: function popup() {
				var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

				if ((typeof opts === 'undefined' ? 'undefined' : (0, _typeof3.default)(opts)) !== 'object') {
					opts = {};
				}
				return Statics.init(opts);
			}
		};

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

			function Viewer() {
				var _ref;

				(0, _classCallCheck3.default)(this, Viewer);

				for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
					params[_key] = arguments[_key];
				}

				return (0, _possibleConstructorReturn3.default)(this, (_ref = Viewer.__proto__ || Object.getPrototypeOf(Viewer)).call.apply(_ref, [this].concat(params)));
			}

			(0, _createClass3.default)(Viewer, [{
				key: 'render',
				value: function render() {
					return _react2.default.createElement(WrappedComponent, (0, _extends3.default)({}, this.props, { ref: 'WrappedComponent' }));
				}
			}]);
			return Viewer;
		}(_react.Component), _class.popup = Statics.popup, _class.destroy = function (_cName) {
			cName = _cName || cName;
			if (!!_index2.default.APIS[cName]) {
				_reactDom2.default.unmountComponentAtNode(_index2.default.APIS[cName]);
				delete _index2.default.APIS[cName];
			}
		}, _temp);
		;

		// Viewer 编译后 变量提升
		return Viewer;
	};
};