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

var _extends3 = _interopRequireDefault(_extends2);

var _class, _temp;

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 _index = require('../rc-instance/index');

var _index2 = _interopRequireDefault(_index);

require('./MToasts.scss');

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

var Dom = document.body;
var ToastsStatics = {};
var leaveTime = 201;
var cName = "MToasts";
ToastsStatics = {
	/**
  * 根据antd-mobile的设计改写
  * 多增加已个options选项
  * opts：{className, style}
  */
	info: function info(message) {
		var duration = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
		var onCallback = arguments[2];

		var _this = this;

		var showClose = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
		var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};

		return new Promise(function (resolve, reject) {
			var div = document.createElement('div');
			_index2.default.APIS[cName] && Toasts.hide();
			Dom.appendChild(div);
			opts = (0, _extends3.default)({}, opts, {
				message: message,
				duration: duration == 0 ? 1 * 60 * 60 * 24 : duration,
				onCallback: onCallback,
				showClose: showClose,
				onCloseSoon: function onCloseSoon() {
					_reactDom2.default.unmountComponentAtNode(div);
					console.log(div);
					Dom.removeChild(div);
					delete _index2.default.APIS[cName];
				},
				onClose: function onClose() {
					opts.onCloseSoon();
				}
			});
			_index2.default.APIS[cName] = div;
			_reactDom2.default.render(_react2.default.createElement(Toasts, (0, _extends3.default)({}, opts, { ref: function ref(instance) {
					return _this.comp = instance;
				} })), div, function () {
				return resolve(_this.comp);
			});
		});
	},
	fail: function fail(message, duration, onCallback, showClose) {
		var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { type: "fail" };

		console.error('还未开发');
		return ToastsStatics.info(message, duration, onCallback, showClose, opts);
	},
	success: function success(message, duration, onCallback, showClose) {
		var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { type: "success" };

		console.error('还未开发');
		return ToastsStatics.info(message, duration, onCallback, showClose, opts);
	},
	loading: function loading(message, duration, onCallback, showClose) {
		var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { type: "loading" };

		console.error('还未开发');
		return ToastsStatics.info(message, duration, onCallback, showClose, opts);
	},
	offline: function offline(message, duration, onCallback, showClose) {
		var opts = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : { type: "offline" };

		console.error('还未开发');
		return ToastsStatics.info(message, duration, onCallback, showClose, opts);
	}
};
var Toasts = (_temp = _class = function (_Component) {
	(0, _inherits3.default)(Toasts, _Component);

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

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

		_this2.handleClose = function (e) {
			e && e.preventDefault();
			e && e.stopPropagation();
			_this2.refs.fixed.classList.add("__leave");
			_this2.duration && clearTimeout(_this2.duration);
			_this2.timer = setTimeout(function () {
				_this2.close();
			}, leaveTime);
		};

		_this2.handlePress = function (event, atIndex) {
			// 关闭
			_this2.handleClose();
		};

		return _this2;
	}
	/**
  * 手动清理全部弹窗
  */


	(0, _createClass3.default)(Toasts, [{
		key: 'componentWillMount',
		value: function componentWillMount() {
			var _this3 = this;

			this.duration = setTimeout(function () {
				// 主线程
				_this3.handleClose();
			}, Number(this.props.duration) * 1000 - leaveTime);
		}
	}, {
		key: 'componentDidMount',
		value: function componentDidMount() {}
	}, {
		key: 'componentWillUnmount',
		value: function componentWillUnmount() {
			this.timer && clearTimeout(this.timer);
			this.duration && clearTimeout(this.duration);
		}
		/**
   * 给外包暴露方法，只删除自己
   */

	}, {
		key: 'close',
		value: function close() {
			// 主动触发，如果有回调就执行回调
			this && this.props.onCallback && this.props.onCallback();
			// 移除弹窗
			this && this.props.onClose && this.props.onClose();
		}
	}, {
		key: 'render',
		value: function render() {
			var _props = this.props,
			    _props$className = _props.className,
			    className = _props$className === undefined ? "" : _props$className,
			    _props$style = _props.style,
			    style = _props$style === undefined ? {} : _props$style,
			    message = _props.message,
			    showClose = _props.showClose;

			return _react2.default.createElement(
				'div',
				{ className: 'rcm-toast ' + className, style: (0, _extends3.default)({}, style) },
				showClose && _react2.default.createElement('div', { className: '__bg', ref: 'bg', onClick: this.handleClose }),
				_react2.default.createElement(
					'div',
					{ className: '__fixed', ref: 'fixed' },
					message
				)
			);
		}
	}]);
	return Toasts;
}(_react.Component), _class.info = ToastsStatics.info, _class.fail = ToastsStatics.fail, _class.success = ToastsStatics.success, _class.loading = ToastsStatics.loading, _class.offline = ToastsStatics.offline, _class.hide = function () {
	if (_index2.default.APIS[cName]) {
		_reactDom2.default.unmountComponentAtNode(_index2.default.APIS[cName]);
		delete _index2.default.APIS[cName];
	}
}, _temp);

Toasts.propTypes = {
	// className,
	// style,
	// message,
	// showClose
	// type
};
Toasts.defaultProps = {};
exports.default = Toasts;