'use strict';

exports.__esModule = true;
exports.default = undefined;

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

var _regenerator2 = _interopRequireDefault(_regenerator);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

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

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

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; }

var VideoFull = function (_Component) {
  _inherits(VideoFull, _Component);

  function VideoFull() {
    var _temp, _this, _ret;

    _classCallCheck(this, VideoFull);

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

    return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.loadTcPlayer = function () {
      return new Promise(function (resolve) {
        var script = document.getElementById('_typlayer_lib_');
        if (script) {
          resolve(true);
          return;
        }
        script = document.createElement('script');
        script.id = '_typlayer_lib_';
        script.type = 'text/javascript';
        script.charset = 'utf-8';
        script.src = _this.props.libSrc || '//imgcache.qq.com/open/qcloud/video/vcplayer/TcPlayer-2.3.2.js';
        document.body.appendChild(script);
        script.onload = function () {
          resolve(true);
        };
        script.onerror = function () {
          resolve(false);
        };
      });
    }, _temp), _possibleConstructorReturn(_this, _ret);
  }

  VideoFull.prototype.componentWillMount = function componentWillMount() {};

  VideoFull.prototype.componentDidMount = function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
      var _props, poster, m3u8, flv, mp4, autoPlay, width, height, params;

      return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _props = this.props, poster = _props.poster, m3u8 = _props.m3u8, flv = _props.flv, mp4 = _props.mp4, autoPlay = _props.autoPlay;
              width = this.$el.clientWidth;
              height = this.$el.clientHeight;
              _context.next = 5;
              return this.loadTcPlayer();

            case 5:
              if (_context.sent) {
                _context.next = 8;
                break;
              }

              console.error('加载TcPlayer出错, 请稍后再试');
              return _context.abrupt('return');

            case 8:
              // 构建参数
              params = {
                autoplay: autoPlay || false, // iOS 下 safari 浏览器，以及大部分移动端浏览器是不开放视频自动播放这个能力的
                poster: {
                  style: 'stretch',
                  src: poster
                },
                width: width, // 视频的显示宽度，请尽量使用视频分辨率宽度
                height: height // 视频的显示高度，请尽量使用视频分辨率高度
              };

              if (m3u8) params.m3u8 = m3u8;
              if (flv) params.flv = flv; // 增加了一个 flv 的播放地址，用于PC平台的播放 请替换成实际可用的播放地址
              if (mp4) params.mp4 = mp4;

              if (!(!m3u8 && !flv && !mp4)) {
                _context.next = 15;
                break;
              }

              console.warn('请传入视频源');
              return _context.abrupt('return');

            case 15:
              // eslint-disable-next-line
              this.instance = new TcPlayer('_id_tcplayer_video_container_', params);

            case 16:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

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

    return componentDidMount;
  }();

  VideoFull.prototype.render = function render() {
    var _this2 = this;

    return _react2.default.createElement(
      'div',
      { className: 'videofull-page', ref: function ref(el) {
          _this2.$el = el;
        } },
      _react2.default.createElement('div', { id: '_id_tcplayer_video_container_' })
    );
  };

  return VideoFull;
}(_react.Component);

exports.default = VideoFull;
VideoFull.propTypes = process.env.NODE_ENV !== "production" ? {
  poster: _propTypes2.default.string,
  m3u8: _propTypes2.default.string,
  flv: _propTypes2.default.string,
  mp4: _propTypes2.default.string,
  autoPlay: _propTypes2.default.bool,
  libSrc: _propTypes2.default.string
} : {};
module.exports = exports['default'];