"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.BaseViewer = void 0;
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 = _interopRequireWildcard(require("react"));
var _reactIntlNext = require("react-intl-next");
var _mediaUi = require("@atlaskit/media-ui");
var _deepEqual = _interopRequireDefault(require("deep-equal"));
var _mediaClient = require("@atlaskit/media-client");
var _errorMessage = _interopRequireDefault(require("../errorMessage"));
var _loading = require("../loading");
var _download = require("../download");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 BaseViewer = exports.BaseViewer = /*#__PURE__*/function (_React$Component) {
  function BaseViewer() {
    var _this;
    (0, _classCallCheck2.default)(this, BaseViewer);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, BaseViewer, [].concat(args));
    (0, _defineProperty2.default)(_this, "state", _this.getInitialState());
    (0, _defineProperty2.default)(_this, "mounted", false);
    (0, _defineProperty2.default)(_this, "onMediaDisplayed", function () {
      var item = _this.props.item;
      _mediaClient.globalMediaEventEmitter.emit('media-viewed', {
        fileId: item.id,
        viewingLevel: 'full'
      });
    });
    return _this;
  }
  (0, _inherits2.default)(BaseViewer, _React$Component);
  return (0, _createClass2.default)(BaseViewer, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.mounted = true;
      this.init();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.mounted = false;
      this.release();
    }
  }, {
    key: "safeSetState",
    value: function safeSetState(newState) {
      if (this.mounted) {
        this.setState(_objectSpread(_objectSpread({}, this.state), newState));
      }
    }

    // NOTE: We've moved parts of the logic to reset a component into this method
    // to optimise the performance. Resetting the state before the `componentDidUpdate`
    // lifecycle event allows us avoid one additional render cycle.
    // However, this lifecycle method might eventually be deprecated, so be careful
    // when working with it.
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      if (this.needsReset(nextProps, this.props)) {
        this.release();
        this.setState(this.initialState);
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      if (this.needsReset(prevProps, this.props)) {
        this.init();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;
      return this.state.content.match({
        pending: function pending() {
          return /*#__PURE__*/_react.default.createElement(_loading.Spinner, null);
        },
        successful: function successful(content) {
          return /*#__PURE__*/_react.default.createElement(_react.Fragment, null, _this2.renderSuccessful(content));
        },
        failed: function failed(error) {
          var item = _this2.props.item;
          return /*#__PURE__*/_react.default.createElement(_errorMessage.default, {
            fileId: item.id,
            fileState: item,
            error: error,
            supressAnalytics: true // item-viewer.tsx will send
          }, /*#__PURE__*/_react.default.createElement("p", null, /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, _mediaUi.messages.try_downloading_file)), _this2.renderDownloadButton(error));
        }
      });
    }

    // Accessing abstract getters in a constructor is not allowed
  }, {
    key: "getInitialState",
    value: function getInitialState() {
      return this.initialState;
    }
  }, {
    key: "renderDownloadButton",
    value: function renderDownloadButton(error) {
      var _this$props = this.props,
        item = _this$props.item,
        mediaClient = _this$props.mediaClient,
        collectionName = _this$props.collectionName,
        traceContext = _this$props.traceContext;
      return /*#__PURE__*/_react.default.createElement(_download.ErrorViewDownloadButton, {
        fileState: item,
        mediaClient: mediaClient,
        error: error,
        collectionName: collectionName,
        traceContext: traceContext
      });
    }
  }, {
    key: "needsReset",
    value: function needsReset(propsA, propsB) {
      return !(0, _deepEqual.default)(propsA.item, propsB.item) || propsA.collectionName !== propsB.collectionName;
    }
  }]);
}(_react.default.Component);