"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ArchiveSidebarFolderEntry = 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 _ = _interopRequireDefault(require("@atlaskit/icon-file-type/glyph/folder/24"));
var _mediaCommon = require("@atlaskit/media-common");
var _mediaTypeIcon = require("@atlaskit/media-ui/media-type-icon");
var _styleWrappers = require("./styleWrappers");
var _utils = require("../../utils");
var _customButtonItem = require("./custom-button-item");
var _mediaUi = require("@atlaskit/media-ui");
var _archiveDownloadButton = require("./archive-download-button");
var _reactIntlNext = require("react-intl-next");
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 ArchiveSidebarFolderEntryBase = /*#__PURE__*/function (_React$Component) {
  function ArchiveSidebarFolderEntryBase() {
    var _this;
    (0, _classCallCheck2.default)(this, ArchiveSidebarFolderEntryBase);
    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
    _this = _callSuper(this, ArchiveSidebarFolderEntryBase, [].concat(args));
    (0, _defineProperty2.default)(_this, "state", {
      isAbuseModalOpen: false
    });
    (0, _defineProperty2.default)(_this, "renderEntry", function (entry) {
      var _this$props = _this.props,
        root = _this$props.root,
        onEntrySelected = _this$props.onEntrySelected,
        mediaClient = _this$props.mediaClient;
      var onClick = function onClick() {
        return onEntrySelected(entry);
      };
      return /*#__PURE__*/_react.default.createElement(_styleWrappers.ArchiveSidebarFileEntryWrapper, {
        key: entry.name,
        index: entry.name
      }, /*#__PURE__*/_react.default.createElement(_styleWrappers.SidebarItemWrapper, null, /*#__PURE__*/_react.default.createElement(_customButtonItem.CustomButtonItem, {
        iconBefore: _this.renderEntryIcon(entry),
        onClick: onClick
      }, _this.formatName(root, entry.name))), entry.isDirectory ? null : _this.renderDownloadButton(entry, root, mediaClient.config.enforceDataSecurityPolicy));
    });
    (0, _defineProperty2.default)(_this, "renderEntryIcon", function (entry) {
      if (entry.isDirectory) {
        return /*#__PURE__*/_react.default.createElement(_.default, {
          label: "Folder"
        });
      }
      var mediaType = (0, _utils.getMediaTypeFromFilename)(entry.name);
      return /*#__PURE__*/_react.default.createElement(_mediaTypeIcon.MediaTypeIcon, {
        type: mediaType
      });
    });
    (0, _defineProperty2.default)(_this, "renderDownloadButton", function (entry, root, enforceDataSecurityPolicy) {
      var _this$props2 = _this.props,
        shouldRenderAbuseModal = _this$props2.shouldRenderAbuseModal,
        intl = _this$props2.intl;
      var tooltip = enforceDataSecurityPolicy ? intl.formatMessage(_mediaUi.messages.download_disabled_security_policy) : undefined;
      var downloadFn = function downloadFn() {
        return _this.downloadZipEntry(entry, root);
      };
      return /*#__PURE__*/_react.default.createElement(_archiveDownloadButton.ArchiveDownloadButton, {
        downloadFn: downloadFn,
        shouldRenderAbuseModal: shouldRenderAbuseModal,
        isDisabled: enforceDataSecurityPolicy,
        tooltip: tooltip
      });
    });
    (0, _defineProperty2.default)(_this, "downloadZipEntry", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(entry, root) {
        var blob, name;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return (0, _utils.rejectAfter)(function () {
                return entry.blob();
              });
            case 3:
              blob = _context.sent;
              name = _this.formatName(root, entry.name);
              (0, _mediaCommon.downloadUrl)(URL.createObjectURL(blob), {
                name: name
              });
              _context.next = 11;
              break;
            case 8:
              _context.prev = 8;
              _context.t0 = _context["catch"](0);
              _this.props.onError(_context.t0, entry);
            case 11:
            case "end":
              return _context.stop();
          }
        }, _callee, null, [[0, 8]]);
      }));
      return function (_x, _x2) {
        return _ref.apply(this, arguments);
      };
    }());
    return _this;
  }
  (0, _inherits2.default)(ArchiveSidebarFolderEntryBase, _React$Component);
  return (0, _createClass2.default)(ArchiveSidebarFolderEntryBase, [{
    key: "formatName",
    value: function formatName(root, name) {
      return name.replace(root, '');
    }
  }, {
    key: "renderSidebarContent",
    value: function renderSidebarContent(root, entries) {
      var navItems = new Map();
      for (var _i = 0, _Object$values = Object.values(entries); _i < _Object$values.length; _i++) {
        var value = _Object$values[_i];
        var name = value.name;
        if (!name.startsWith(root) || (0, _utils.isMacPrivateFile)(name)) {
          continue;
        }
        var paths = name.replace(root, '').split('/').filter(Boolean);
        if (paths.length > 1) {
          if (!navItems.has(paths[0])) {
            navItems.set(paths[0], {
              name: "".concat(root).concat(paths[0], "/"),
              isDirectory: true
            });
          }
        } else if (paths.length === 1) {
          navItems.set(paths[0], value);
        }
      }
      return Array.from(navItems.values()).map(this.renderEntry);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
        root = _this$props3.root,
        entries = _this$props3.entries;
      return /*#__PURE__*/_react.default.createElement(_styleWrappers.ArchiveSidebarFolderWrapper, null, this.renderSidebarContent(root, entries));
    }
  }]);
}(_react.default.Component);
var ArchiveSidebarFolderEntry = exports.ArchiveSidebarFolderEntry = (0, _reactIntlNext.injectIntl)(ArchiveSidebarFolderEntryBase);