"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.VideoViewer = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _mediaClient = require("@atlaskit/media-client");
var _mediaUi = require("@atlaskit/media-ui");
var _domain = require("../domain");
var _errors = require("../errors");
var _styleWrappers = require("../styleWrappers");
var _isIE = require("../utils/isIE");
var _baseViewer = require("./base-viewer");
var _getObjectUrlFromFileState = require("../utils/getObjectUrlFromFileState");
var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
var sdArtifact = 'video_640.mp4';
var hdArtifact = 'video_1280.mp4';
var localStorageKeyName = 'mv_video_player_quality';
var VideoViewer = exports.VideoViewer = /*#__PURE__*/function (_BaseViewer) {
  function VideoViewer() {
    var _this;
    (0, _classCallCheck2.default)(this, VideoViewer);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, VideoViewer, [].concat(args));
    (0, _defineProperty2.default)(_this, "onHDChange", function () {
      var isHDActive = !_this.state.isHDActive;
      var preferredQuality = isHDActive ? 'hd' : 'sd';
      localStorage.setItem(localStorageKeyName, preferredQuality);
      _this.setState({
        isHDActive: isHDActive
      });
      _this.init(isHDActive);
    });
    (0, _defineProperty2.default)(_this, "onFirstPlay", function () {
      var _this$props = _this.props,
        item = _this$props.item,
        onCanPlay = _this$props.onCanPlay;
      _mediaClient.globalMediaEventEmitter.emit('media-viewed', {
        fileId: item.id,
        viewingLevel: 'full'
      });
      onCanPlay && onCanPlay();
    });
    (0, _defineProperty2.default)(_this, "onPlay", function () {
      if ((0, _platformFeatureFlags.fg)('platform_media_resume_video_on_token_expiry')) {
        _this.init();
      }
    });
    (0, _defineProperty2.default)(_this, "onTimeChanged", function () {
      if ((0, _platformFeatureFlags.fg)('platform_media_resume_video_on_token_expiry')) {
        _this.init();
      }
    });
    (0, _defineProperty2.default)(_this, "onError", function () {
      var onError = _this.props.onError;
      onError && onError(new _errors.MediaViewerError('videoviewer-playback'));
    });
    return _this;
  }
  (0, _inherits2.default)(VideoViewer, _BaseViewer);
  return (0, _createClass2.default)(VideoViewer, [{
    key: "initialState",
    get: function get() {
      var item = this.props.item;
      var preferredQuality = (0, _platformFeatureFlags.fg)('platform_media_disable_video_640p_artifact_usage') ? 'hd' : localStorage.getItem(localStorageKeyName);
      return {
        content: _domain.Outcome.pending(),
        isHDActive: isHDAvailable(item) && preferredQuality !== 'sd'
      };
    }
  }, {
    key: "renderSuccessful",
    value: function renderSuccessful(content) {
      var isHDActive = this.state.isHDActive;
      var _this$props2 = this.props,
        item = _this$props2.item,
        showControls = _this$props2.showControls,
        previewCount = _this$props2.previewCount,
        identifier = _this$props2.identifier;
      var useCustomVideoPlayer = !(0, _isIE.isIE)();
      var isAutoPlay = previewCount === 0;
      var hdAvailable = isHDAvailable(item);
      var hdActiveOverride = (0, _platformFeatureFlags.fg)('platform_media_disable_video_640p_artifact_usage') ? hdAvailable : isHDActive;
      return useCustomVideoPlayer ? /*#__PURE__*/_react.default.createElement(_styleWrappers.CustomVideoPlayerWrapper, {
        "data-testid": "media-viewer-video-content"
      }, (0, _mediaClient.isFileIdentifier)(identifier) && (0, _platformFeatureFlags.fg)('platform_media_video_captions') ? /*#__PURE__*/_react.default.createElement(_mediaUi.MediaPlayer, {
        identifier: identifier,
        type: "video",
        isAutoPlay: isAutoPlay,
        onHDToggleClick: this.onHDChange,
        showControls: showControls,
        src: content,
        isHDActive: hdActiveOverride,
        isHDAvailable: hdAvailable,
        isShortcutEnabled: true,
        onFirstPlay: this.onFirstPlay,
        onError: this.onError,
        onPlay: this.onPlay,
        onTimeChanged: this.onTimeChanged,
        lastWatchTimeConfig: {
          contentId: item.id
        }
      }) : /*#__PURE__*/_react.default.createElement(_mediaUi.CustomMediaPlayer, {
        type: "video",
        isAutoPlay: isAutoPlay,
        onHDToggleClick: this.onHDChange,
        showControls: showControls,
        src: content,
        fileId: item.id,
        isHDActive: hdActiveOverride,
        isHDAvailable: hdAvailable,
        isShortcutEnabled: true,
        onFirstPlay: this.onFirstPlay,
        onError: this.onError,
        onPlay: this.onPlay,
        onTimeChanged: this.onTimeChanged,
        lastWatchTimeConfig: {
          contentId: item.id
        }
      })) : /*#__PURE__*/_react.default.createElement(_styleWrappers.Video, {
        autoPlay: isAutoPlay,
        controls: true,
        src: content
      });
    }
  }, {
    key: "init",
    value: function () {
      var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var isHDActive,
          _this$props3,
          mediaClient,
          item,
          collectionName,
          contentUrl,
          hdAvailable,
          hdActiveOverride,
          preferHd,
          _args = arguments;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              isHDActive = _args.length > 0 && _args[0] !== undefined ? _args[0] : this.state.isHDActive;
              _this$props3 = this.props, mediaClient = _this$props3.mediaClient, item = _this$props3.item, collectionName = _this$props3.collectionName;
              _context.prev = 2;
              if (!(item.status === 'processed')) {
                _context.next = 14;
                break;
              }
              hdAvailable = isHDAvailable(item);
              hdActiveOverride = (0, _platformFeatureFlags.fg)('platform_media_disable_video_640p_artifact_usage') ? hdAvailable : isHDActive;
              preferHd = hdActiveOverride && hdAvailable;
              _context.next = 9;
              return mediaClient.file.getArtifactURL(item.artifacts, preferHd ? hdArtifact : sdArtifact, collectionName);
            case 9:
              contentUrl = _context.sent;
              if (contentUrl) {
                _context.next = 12;
                break;
              }
              throw new _errors.MediaViewerError("videoviewer-missing-artefact");
            case 12:
              _context.next = 20;
              break;
            case 14:
              _context.next = 16;
              return (0, _getObjectUrlFromFileState.getObjectUrlFromFileState)(item);
            case 16:
              contentUrl = _context.sent;
              if (contentUrl) {
                _context.next = 20;
                break;
              }
              this.setState({
                content: _domain.Outcome.pending()
              });
              return _context.abrupt("return");
            case 20:
              this.setState({
                content: _domain.Outcome.successful(contentUrl)
              });
              _context.next = 26;
              break;
            case 23:
              _context.prev = 23;
              _context.t0 = _context["catch"](2);
              this.setState({
                content: _domain.Outcome.failed(new _errors.MediaViewerError('videoviewer-fetch-url', _context.t0 instanceof Error ? _context.t0 : undefined))
              });
            case 26:
            case "end":
              return _context.stop();
          }
        }, _callee, this, [[2, 23]]);
      }));
      function init() {
        return _init.apply(this, arguments);
      }
      return init;
    }()
  }, {
    key: "release",
    value: function release() {}
  }]);
}(_baseViewer.BaseViewer);
function isHDAvailable(file) {
  if (file.status !== 'processed') {
    return false;
  }
  return !!(0, _mediaClient.getArtifactUrl)(file.artifacts, hdArtifact);
}