import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _regeneratorRuntime from "@babel/runtime/regenerator";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(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; })(); }
import React from 'react';
import Folder24Icon from '@atlaskit/icon-file-type/glyph/folder/24';
import { downloadUrl } from '@atlaskit/media-common';
import { MediaTypeIcon } from '@atlaskit/media-ui/media-type-icon';
import { ArchiveSidebarFolderWrapper, ArchiveSidebarFileEntryWrapper, SidebarItemWrapper } from './styleWrappers';
import { getMediaTypeFromFilename, isMacPrivateFile, rejectAfter } from '../../utils';
import { CustomButtonItem } from './custom-button-item';
import { messages } from '@atlaskit/media-ui';
import { ArchiveDownloadButton } from './archive-download-button';
import { injectIntl } from 'react-intl-next';
var ArchiveSidebarFolderEntryBase = /*#__PURE__*/function (_React$Component) {
  function ArchiveSidebarFolderEntryBase() {
    var _this;
    _classCallCheck(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));
    _defineProperty(_this, "state", {
      isAbuseModalOpen: false
    });
    _defineProperty(_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.createElement(ArchiveSidebarFileEntryWrapper, {
        key: entry.name,
        index: entry.name
      }, /*#__PURE__*/React.createElement(SidebarItemWrapper, null, /*#__PURE__*/React.createElement(CustomButtonItem, {
        iconBefore: _this.renderEntryIcon(entry),
        onClick: onClick
      }, _this.formatName(root, entry.name))), entry.isDirectory ? null : _this.renderDownloadButton(entry, root, mediaClient.config.enforceDataSecurityPolicy));
    });
    _defineProperty(_this, "renderEntryIcon", function (entry) {
      if (entry.isDirectory) {
        return /*#__PURE__*/React.createElement(Folder24Icon, {
          label: "Folder"
        });
      }
      var mediaType = getMediaTypeFromFilename(entry.name);
      return /*#__PURE__*/React.createElement(MediaTypeIcon, {
        type: mediaType
      });
    });
    _defineProperty(_this, "renderDownloadButton", function (entry, root, enforceDataSecurityPolicy) {
      var _this$props2 = _this.props,
        shouldRenderAbuseModal = _this$props2.shouldRenderAbuseModal,
        intl = _this$props2.intl;
      var tooltip = enforceDataSecurityPolicy ? intl.formatMessage(messages.download_disabled_security_policy) : undefined;
      var downloadFn = function downloadFn() {
        return _this.downloadZipEntry(entry, root);
      };
      return /*#__PURE__*/React.createElement(ArchiveDownloadButton, {
        downloadFn: downloadFn,
        shouldRenderAbuseModal: shouldRenderAbuseModal,
        isDisabled: enforceDataSecurityPolicy,
        tooltip: tooltip
      });
    });
    _defineProperty(_this, "downloadZipEntry", /*#__PURE__*/function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(entry, root) {
        var blob, name;
        return _regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              _context.next = 3;
              return rejectAfter(function () {
                return entry.blob();
              });
            case 3:
              blob = _context.sent;
              name = _this.formatName(root, entry.name);
              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;
  }
  _inherits(ArchiveSidebarFolderEntryBase, _React$Component);
  return _createClass(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) || 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.createElement(ArchiveSidebarFolderWrapper, null, this.renderSidebarContent(root, entries));
    }
  }]);
}(React.Component);
export var ArchiveSidebarFolderEntry = injectIntl(ArchiveSidebarFolderEntryBase);