'use strict';

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

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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

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

	function ToImg(props, context) {
		var _this2 = this;

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

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

		_this.getImg = function () {
			var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
				var fileName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'image';
				var getFile = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

				var html2canvas, canvas, _ref2, file, base64Image;

				return _regenerator2.default.wrap(function _callee$(_context) {
					while (1) {
						switch (_context.prev = _context.next) {
							case 0:
								_context.prev = 0;
								_context.next = 3;
								return import('html2canvas');

							case 3:
								html2canvas = _context.sent;

								// 兼容webpack 3.0/4.0 写法	
								html2canvas = html2canvas.hasOwnProperty('default') ? html2canvas.default : html2canvas;

								_context.next = 7;
								return html2canvas(_this.refs.img);

							case 7:
								canvas = _context.sent;
								_context.next = 10;
								return (0, _utils.getCroppedImg)(canvas, fileName, getFile);

							case 10:
								_ref2 = _context.sent;
								file = _ref2.file;
								base64Image = _ref2.base64Image;
								return _context.abrupt('return', {
									file: file,
									base64Image: base64Image
								});

							case 16:
								_context.prev = 16;
								_context.t0 = _context['catch'](0);

								console.log(_context.t0);
								return _context.abrupt('return', false);

							case 20:
							case 'end':
								return _context.stop();
						}
					}
				}, _callee, _this2, [[0, 16]]);
			}));

			return function () {
				return _ref.apply(this, arguments);
			};
		}();

		_this.download = function () {
			var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
				var fileName = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'image';
				var getFile = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

				var _ref4, file, base64Image, $this, evt;

				return _regenerator2.default.wrap(function _callee2$(_context2) {
					while (1) {
						switch (_context2.prev = _context2.next) {
							case 0:
								_context2.prev = 0;
								_context2.next = 3;
								return _this.getImg(fileName, getFile);

							case 3:
								_ref4 = _context2.sent;
								file = _ref4.file;
								base64Image = _ref4.base64Image;
								$this = document.createElement('a');
								// initEvent 不加后两个参数在FF下会报错  事件类型，是否冒泡，是否阻止浏览器的默认行为

								evt = document.createEvent("HTMLEvents");

								evt.initEvent("click", true, true);
								// load
								$this.download = fileName;
								$this.href = URL.createObjectURL(file);
								$this.click();
								return _context2.abrupt('return', {
									file: file,
									base64Image: base64Image
								});

							case 15:
								_context2.prev = 15;
								_context2.t0 = _context2['catch'](0);

								console.log(_context2.t0);
								return _context2.abrupt('return', false);

							case 19:
							case 'end':
								return _context2.stop();
						}
					}
				}, _callee2, _this2, [[0, 15]]);
			}));

			return function () {
				return _ref3.apply(this, arguments);
			};
		}();

		return _this;
	}

	(0, _createClass3.default)(ToImg, [{
		key: 'render',
		value: function render() {
			return (0, _react.cloneElement)(_react.Children.only(this.props.children), {
				ref: 'img'
			});
		}
	}]);
	return ToImg;
}(_react.Component);

ToImg.propTypes = {};
ToImg.defaultProps = {};
exports.default = ToImg;