"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 = _interopRequireWildcard(require("react"));
var _mediaClient = require("@atlaskit/media-client");
var _styleWrappers = require("../../styleWrappers");
var _archiveSidebar = require("./archive-sidebar");
var _archive = require("./archive");
var _loading = require("../../loading");
var _errors = require("../../errors");
var _styleWrappers2 = require("./styleWrappers");
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 _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 ArchiveSidebarRenderer = exports.default = /*#__PURE__*/function (_Component) {
  function ArchiveSidebarRenderer() {
    var _this;
    (0, _classCallCheck2.default)(this, ArchiveSidebarRenderer);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ArchiveSidebarRenderer, [].concat(args));
    (0, _defineProperty2.default)(_this, "state", {
      entries: {},
      status: 'loading'
    });
    return _this;
  }
  (0, _inherits2.default)(ArchiveSidebarRenderer, _Component);
  return (0, _createClass2.default)(ArchiveSidebarRenderer, [{
    key: "componentDidMount",
    value: function () {
      var _componentDidMount = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
        var _this$props, selectedFileState, mediaClient, collectionName, onError, onSuccess, archive, entries;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _this$props = this.props, selectedFileState = _this$props.selectedFileState, mediaClient = _this$props.mediaClient, collectionName = _this$props.collectionName, onError = _this$props.onError, onSuccess = _this$props.onSuccess;
              _context.prev = 1;
              _context.next = 4;
              return (0, _archive.getArchiveEntriesFromFileState)(selectedFileState, mediaClient, collectionName);
            case 4:
              archive = _context.sent;
              entries = archive.entries;
              this.setState({
                entries: entries,
                status: 'loaded'
              });
              onSuccess();
              _context.next = 14;
              break;
            case 10:
              _context.prev = 10;
              _context.t0 = _context["catch"](1);
              this.setState({
                status: 'loaded'
              });
              onError(new _errors.ArchiveViewerError('archiveviewer-read-binary', _context.t0 instanceof Error ? _context.t0 : undefined));
            case 14:
            case "end":
              return _context.stop();
          }
        }, _callee, this, [[1, 10]]);
      }));
      function componentDidMount() {
        return _componentDidMount.apply(this, arguments);
      }
      return componentDidMount;
    }()
  }, {
    key: "render",
    value: function render() {
      var _this$state = this.state,
        entries = _this$state.entries,
        status = _this$state.status;
      var _this$props2 = this.props,
        mediaClient = _this$props2.mediaClient,
        onHeaderClicked = _this$props2.onHeaderClicked,
        isArchiveEntryLoading = _this$props2.isArchiveEntryLoading,
        onSelectedArchiveEntryChange = _this$props2.onSelectedArchiveEntryChange,
        onError = _this$props2.onError,
        selectedFileState = _this$props2.selectedFileState;
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, status === 'loading' && /*#__PURE__*/_react.default.createElement(_styleWrappers2.ArchiveSideBar, null, /*#__PURE__*/_react.default.createElement(_styleWrappers.SpinnerWrapper, null, /*#__PURE__*/_react.default.createElement(_loading.Spinner, null))) || /*#__PURE__*/_react.default.createElement(_archiveSidebar.ArchiveSidebar, {
        entries: entries,
        onEntrySelected: onSelectedArchiveEntryChange,
        onHeaderClicked: onHeaderClicked,
        mediaClient: mediaClient,
        isArchiveEntryLoading: isArchiveEntryLoading,
        onError: onError,
        shouldRenderAbuseModal: !(0, _mediaClient.isErrorFileState)(selectedFileState) && !!selectedFileState.abuseClassification
      }));
    }
  }]);
}(_react.Component);