text: 'use strict';
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.FilePlayer = undefined;
  
  var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  
  var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  
  var _react = require('react');
  
  var _react2 = _interopRequireDefault(_react);
  
  var _utils = require('../utils');
  
  var _singlePlayer = require('../singlePlayer');
  
  var _singlePlayer2 = _interopRequireDefault(_singlePlayer);
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
  
  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 IOS = typeof navigator !== 'undefined' && /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
  var AUDIO_EXTENSIONS = /\.(m4a|mp4a|mpga|mp2|mp2a|mp3|m2a|m3a|wav|weba|aac|oga|spx)($|\?)/i;
  var VIDEO_EXTENSIONS = /\.(mp4|og[gv]|webm|mov|m4v)($|\?)/i;
  var HLS_EXTENSIONS = /\.(m3u8)($|\?)/i;
  var HLS_SDK_URL = 'https://cdnjs.cloudflare.com/ajax/libs/hls.js/0.8.9/hls.min.js';
  var HLS_GLOBAL = 'Hls';
  var DASH_EXTENSIONS = /\.(mpd)($|\?)/i;
  var DASH_SDK_URL = 'https://cdnjs.cloudflare.com/ajax/libs/dashjs/2.6.5/dash.all.min.js';
  var DASH_GLOBAL = 'dashjs';
  var MATCH_DROPBOX_URL = /www\.dropbox\.com\/.+/;
  
  function canPlay(url) {
    if (url instanceof Array) {
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;
  
      try {
        for (var _iterator = url[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var item = _step.value;
  
          if (typeof item === 'string' && canPlay(item)) {
            return true;
          }
          if (canPlay(item.src)) {
            return true;
          }
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator['return']) {
            _iterator['return']();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }
  
      return false;
    }
    return AUDIO_EXTENSIONS.test(url) || VIDEO_EXTENSIONS.test(url) || HLS_EXTENSIONS.test(url) || DASH_EXTENSIONS.test(url);
  }
  
  var FilePlayer = exports.FilePlayer = function (_Component) {
    _inherits(FilePlayer, _Component);
  
    function FilePlayer() {
      var _ref;
  
      var _temp, _this, _ret;
  
      _classCallCheck(this, FilePlayer);
  
      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
  
      return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = FilePlayer.__proto__ || Object.getPrototypeOf(FilePlayer)).call.apply(_ref, [this].concat(args))), _this), _this.onSeek = function (e) {
        _this.props.onSeek(e.target.currentTime);
      }, _this.renderSourceElement = function (source, index) {
        if (typeof source === 'string') {
          return _react2['default'].createElement('source', { key: index, src: source });
        }
        return _react2['default'].createElement('source', _extends({ key: index }, source));
      }, _this.renderTrack = function (track, index) {
        return _react2['default'].createElement('track', _extends({ key: index }, track));
      }, _this.ref = function (player) {
        _this.player = player;
      }, _temp), _possibleConstructorReturn(_this, _ret);
    }
  
    _createClass(FilePlayer, [{
      key: 'componentDidMount',
      value: function componentDidMount() {
        this.addListeners();
        if (IOS) {
          this.player.load();
        }
      }
    }, {
      key: 'componentWillReceiveProps',
      value: function componentWillReceiveProps(nextProps) {
        if (this.shouldUseAudio(this.props) !== this.shouldUseAudio(nextProps)) {
          this.removeListeners();
        }
      }
    }, {
      key: 'componentDidUpdate',
      value: function componentDidUpdate(prevProps) {
        if (this.shouldUseAudio(this.props) !== this.shouldUseAudio(prevProps)) {
          this.addListeners();
        }
      }
    }, {
      key: 'componentWillUnmount',
      value: function componentWillUnmount() {
        this.removeListeners();
      }
    }, {
      key: 'addListeners',
      value: function addListeners() {
        var _props = this.props,
            onReady = _props.onReady,
            onPlay = _props.onPlay,
            onPause = _props.onPause,
            onEnded = _props.onEnded,
            onError = _props.onError,
            playsinline = _props.playsinline;
  
        this.player.addEventListener('canplay', onReady);
        this.player.addEventListener('play', onPlay);
        this.player.addEventListener('pause', onPause);
        this.player.addEventListener('seeked', this.onSeek);
        this.player.addEventListener('ended', onEnded);
        this.player.addEventListener('error', onError);
        if (playsinline) {
          this.player.setAttribute('playsinline', '');
          this.player.setAttribute('webkit-playsinline', '');
        }
      }
    }, {
      key: 'removeListeners',
      value: function removeListeners() {
        var _props2 = this.props,
            onReady = _props2.onReady,
            onPlay = _props2.onPlay,
            onPause = _props2.onPause,
            onEnded = _props2.onEnded,
            onError = _props2.onError;
  
        this.player.removeEventListener('canplay', onReady);
        this.player.removeEventListener('play', onPlay);
        this.player.removeEventListener('pause', onPause);
        this.player.removeEventListener('seeked', this.onSeek);
        this.player.removeEventListener('ended', onEnded);
        this.player.removeEventListener('error', onError);
      }
    }, {
      key: 'shouldUseAudio',
      value: function shouldUseAudio(props) {
        if (props.config.file.forceVideo) {
          return false;
        }
        if (props.config.file.attributes.poster) {
          return false; // Use <video> so that poster is shown
        }
        return AUDIO_EXTENSIONS.test(props.url) || props.config.file.forceAudio;
      }
    }, {
      key: 'shouldUseHLS',
      value: function shouldUseHLS(url) {
        return HLS_EXTENSIONS.test(url) && !IOS || this.props.config.file.forceHLS;
      }
    }, {
      key: 'shouldUseDASH',
      value: function shouldUseDASH(url) {
        return DASH_EXTENSIONS.test(url) || this.props.config.file.forceDASH;
      }
    }, {
      key: 'load',
      value: function load(url) {
        var _this2 = this;
  
        if (this.shouldUseHLS(url)) {
          (0, _utils.getSDK)(HLS_SDK_URL, HLS_GLOBAL).then(function (Hls) {
            _this2.hls = new Hls(_this2.props.config.file.hlsOptions);
            _this2.hls.on(Hls.Events.ERROR, function (e, data) {
              _this2.props.onError(e, data, _this2.hls, Hls);
            });
            _this2.hls.loadSource(url);
            _this2.hls.attachMedia(_this2.player);
          });
        }
        if (this.shouldUseDASH(url)) {
          (0, _utils.getSDK)(DASH_SDK_URL, DASH_GLOBAL).then(function (dashjs) {
            _this2.dash = dashjs.MediaPlayer().create();
            _this2.dash.initialize(_this2.player, url, _this2.props.playing);
            _this2.dash.getDebug().setLogToBrowserConsole(false);
          });
        }
      }
    }, {
      key: 'play',
      value: function play() {
        var promise = this.player.play();
        if (promise) {
          promise['catch'](this.props.onError);
        }
      }
    }, {
      key: 'pause',
      value: function pause() {
        this.player.pause();
      }
    }, {
      key: 'stop',
      value: function stop() {
        this.player.removeAttribute('src');
        if (this.hls) {
          this.hls.destroy();
        }
        if (this.dash) {
          this.dash.reset();
        }
      }
    }, {
      key: 'seekTo',
      value: function seekTo(seconds) {
        this.player.currentTime = seconds;
      }
    }, {
      key: 'setVolume',
      value: function setVolume(fraction) {
        this.player.volume = fraction;
      }
    }, {
      key: 'setPlaybackRate',
      value: function setPlaybackRate(rate) {
        this.player.playbackRate = rate;
      }
    }, {
      key: 'getDuration',
      value: function getDuration() {
        return this.player.duration;
      }
    }, {
      key: 'getCurrentTime',
      value: function getCurrentTime() {
        return this.player.currentTime;
      }
    }, {
      key: 'getSecondsLoaded',
      value: function getSecondsLoaded() {
        var buffered = this.player.buffered;
  
        if (buffered.length === 0) {
          return 0;
        }
        var end = buffered.end(buffered.length - 1);
        var duration = this.getDuration();
        if (end > duration) {
          return duration;
        }
        return end;
      }
    }, {
      key: 'getSource',
      value: function getSource(url) {
        var useHLS = this.shouldUseHLS(url);
        var useDASH = this.shouldUseDASH(url);
        if (url instanceof Array || useHLS || useDASH) {
          return undefined;
        }
        if (MATCH_DROPBOX_URL.test(url)) {
          return url.replace('www.dropbox.com', 'dl.dropboxusercontent.com');
        }
        return url;
      }
    }, {
      key: 'render',
      value: function render() {
        var _props3 = this.props,
            url = _props3.url,
            loop = _props3.loop,
            controls = _props3.controls,
            config = _props3.config,
            width = _props3.width,
            height = _props3.height;
  
        var useAudio = this.shouldUseAudio(this.props);
        var Element = useAudio ? 'audio' : 'video';
        var style = {
          width: !width || width === 'auto' ? width : '100%',
          height: !height || height === 'auto' ? height : '100%'
        };
        return _react2['default'].createElement(
          Element,
          _extends({
            ref: this.ref,
            src: this.getSource(url),
            style: style,
            preload: 'auto',
            controls: controls,
            loop: loop
          }, config.file.attributes),
          url instanceof Array && url.map(this.renderSourceElement),
          config.file.tracks.map(this.renderTrack)
        );
      }
    }]);
  
    return FilePlayer;
  }(_react.Component);
  
  FilePlayer.displayName = 'FilePlayer';
  FilePlayer.canPlay = canPlay;
  exports['default'] = (0, _singlePlayer2['default'])(FilePlayer);