"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Header = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireWildcard(require("react"));
var _mediaClient = require("@atlaskit/media-client");
var _mediaUi = require("@atlaskit/media-ui");
var _codeViewer = require("@atlaskit/media-ui/codeViewer");
var _reactIntlNext = require("react-intl-next");
var _domain = require("./domain");
var _styleWrappers = require("./styleWrappers");
var _download = require("./download");
var _mediaClientReact = require("@atlaskit/media-client-react");
var _mimeTypeIcon = require("@atlaskit/media-ui/mime-type-icon");
var _util = require("./viewers/codeViewer/util");
var _errors = require("./errors");
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); }
var Header = exports.Header = function Header(_ref) {
  var _ref$isArchiveSideBar = _ref.isArchiveSideBarVisible,
    isArchiveSideBarVisible = _ref$isArchiveSideBar === void 0 ? false : _ref$isArchiveSideBar,
    extensions = _ref.extensions,
    isSidebarVisible = _ref.isSidebarVisible,
    onSidebarButtonClick = _ref.onSidebarButtonClick,
    identifier = _ref.identifier,
    onSetArchiveSideBarVisible = _ref.onSetArchiveSideBarVisible,
    traceContext = _ref.traceContext;
  // States
  var _useState = (0, _react.useState)(_domain.Outcome.pending()),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    item = _useState2[0],
    setItem = _useState2[1];

  // Refs and Hooks
  var mediaClient = (0, _mediaClientReact.useMediaClient)();
  var _ref2 = identifier,
    id = _ref2.id,
    collectionName = _ref2.collectionName,
    occurrenceKey = _ref2.occurrenceKey;
  var _useFileState = (0, _mediaClientReact.useFileState)(id, {
      collectionName: collectionName,
      occurrenceKey: occurrenceKey
    }),
    fileState = _useFileState.fileState;
  var onSetArchiveSideBarVisibleRef = (0, _react.useRef)(onSetArchiveSideBarVisible);
  onSetArchiveSideBarVisibleRef.current = onSetArchiveSideBarVisible;

  // previous values
  (0, _react.useEffect)(function () {
    if ((0, _mediaClient.isExternalImageIdentifier)(identifier)) {
      var _identifier$name = identifier.name,
        name = _identifier$name === void 0 ? identifier.dataURI : _identifier$name;

      // Simulate a processing file state to render right metadata
      var _fileState = {
        status: 'processing',
        id: name,
        mediaType: 'image',
        mimeType: 'image/',
        name: name,
        representations: {},
        size: 0
      };
      setItem(_domain.Outcome.successful(_fileState));
      return;
    }
    if (!fileState) {
      return;
    }
    if (fileState.status !== 'error') {
      var _onSetArchiveSideBarV;
      (_onSetArchiveSideBarV = onSetArchiveSideBarVisibleRef.current) === null || _onSetArchiveSideBarV === void 0 || _onSetArchiveSideBarV.call(onSetArchiveSideBarVisibleRef, !(0, _mediaClient.isErrorFileState)(fileState) && fileState.mediaType === 'archive');
      setItem(_domain.Outcome.successful(fileState));
    } else {
      setItem(_domain.Outcome.failed(new _errors.MediaViewerError('header-fetch-metadata', (0, _mediaClient.toCommonMediaClientError)(fileState))));
    }
  }, [fileState, identifier]);
  var renderFileTypeText = function renderFileTypeText(item) {
    // render appropriate header if its a code/email item and the feature flag is enabled
    if ((0, _codeViewer.isCodeViewerItem)(item.name, item.mimeType)) {
      // gather language and extension
      // i.e test.py would have a language of 'python' and an extension of 'py'
      var language = (0, _codeViewer.getLanguageType)(item.name, item.mimeType);
      var ext = (0, _codeViewer.getExtension)(item.name);

      // specific cases for if we want a certain word translated in other languages
      switch (ext) {
        case 'msg':
          return /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, _mediaUi.messages.email);
        case 'txt':
          return /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, _mediaUi.messages.text);
      }

      // no need for translations in other languages
      return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, (0, _util.getFormat)(language || 'unknown', ext));
    }
    var mediaType = item.mediaType;
    var mediaTypeTranslationMap = {
      doc: _mediaUi.messages.document,
      audio: _mediaUi.messages.audio,
      video: _mediaUi.messages.video,
      image: _mediaUi.messages.image,
      archive: _mediaUi.messages.archive,
      unknown: _mediaUi.messages.unknown
    };
    var message = mediaTypeTranslationMap[mediaType || 'unknown'];

    // Defaulting to unknown again since backend has more mediaTypes than the current supported ones
    return /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, message || _mediaUi.messages.unknown);
  };
  return /*#__PURE__*/_react.default.createElement(_styleWrappers.Header, {
    isArchiveSideBarVisible: isArchiveSideBarVisible
    // eslint-disable-next-line @atlaskit/ui-styling-standard/no-classname-prop -- Ignored via go/DSP-18766
    ,
    className: _mediaUi.hideControlsClassName
  }, /*#__PURE__*/_react.default.createElement(_styleWrappers.LeftHeader, null, item.match({
    successful: function successful(item) {
      return !(0, _mediaClient.isErrorFileState)(item) && /*#__PURE__*/_react.default.createElement(_styleWrappers.MetadataWrapper, null, /*#__PURE__*/_react.default.createElement(_styleWrappers.MetadataIconWrapper, null, /*#__PURE__*/_react.default.createElement(_mimeTypeIcon.MimeTypeIcon, {
        testId: 'media-viewer-file-type-icon',
        mediaType: item.mediaType,
        mimeType: item.mimeType,
        name: item.name
      })), /*#__PURE__*/_react.default.createElement(_styleWrappers.MedatadataTextWrapper, null, /*#__PURE__*/_react.default.createElement(_styleWrappers.MetadataFileName, {
        "data-testid": "media-viewer-file-name"
      }, item.name || /*#__PURE__*/_react.default.createElement(_reactIntlNext.FormattedMessage, _mediaUi.messages.unknown)), /*#__PURE__*/_react.default.createElement(_styleWrappers.MetadataSubText, {
        "data-testid": "media-viewer-file-metadata-text"
      }, /*#__PURE__*/_react.default.createElement(_styleWrappers.FormattedMessageWrapper, null, renderFileTypeText(item)), item.size ? ' · ' + (0, _mediaUi.toHumanReadableMediaSize)(item.size) : '')));
    },
    pending: function pending() {
      return null;
    },
    failed: function failed() {
      return null;
    }
  })), /*#__PURE__*/_react.default.createElement(_styleWrappers.RightHeader, null, (extensions === null || extensions === void 0 ? void 0 : extensions.sidebar) && /*#__PURE__*/_react.default.createElement(_mediaUi.MediaButton, {
    isSelected: isSidebarVisible,
    testId: "media-viewer-sidebar-button",
    onClick: onSidebarButtonClick,
    iconBefore: extensions.sidebar.icon
  }), item.match({
    pending: function pending() {
      return _download.DisabledToolbarDownloadButton;
    },
    failed: function failed() {
      return _download.DisabledToolbarDownloadButton;
    },
    successful: function successful(item) {
      return /*#__PURE__*/_react.default.createElement(_download.ToolbarDownloadButton, {
        state: item,
        identifier: identifier,
        mediaClient: mediaClient,
        traceContext: traceContext
      });
    }
  })));
};
var _default = exports.default = (0, _reactIntlNext.injectIntl)(Header);