"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = ContentViewer;

var _react = _interopRequireDefault(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _immutable = _interopRequireDefault(require("immutable"));

var _reactIntl = require("react-intl");

var _get = _interopRequireDefault(require("lodash/get"));

var _warning = _interopRequireDefault(require("warning"));

var _ImageContainer = _interopRequireDefault(require("../../containers/media/ImageContainer"));

var _blobHelpers = require("../../helpers/blobHelpers");

var _contentHelpers = require("../../helpers/contentHelpers");

var _ContentViewer = _interopRequireDefault(require("../../../styles/cspace-ui/ContentViewer.css"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const messages = (0, _reactIntl.defineMessages)({
  previewTitle: {
    "id": "contentViewer.previewTitle",
    "defaultMessage": "File preview"
  },
  error: {
    "id": "contentViewer.error",
    "defaultMessage": "Preview not available"
  },
  pending: {
    "id": "contentViewer.pending",
    "defaultMessage": "File preview"
  }
});
const contextTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  csid: _propTypes.default.string,
  intl: _reactIntl.intlShape,
  recordData: _propTypes.default.instanceOf(_immutable.default.Map)
};

function ContentViewer(props, context) {
  const {
    config,
    recordType,
    vocabulary,
    csid,
    intl,
    recordData
  } = context;
  const content = (0, _get.default)(config, ['recordTypes', recordType, 'content']);
  process.env.NODE_ENV !== "production" ? (0, _warning.default)(content, `No content descriptor found for the record type ${recordType}. The content viewer will not be rendered.`) : void 0;

  if (!content) {
    return null;
  }

  const popupSubresource = (0, _get.default)(content, ['popup', 'subresource']);
  const previewSubresource = (0, _get.default)(content, ['preview', 'subresource']);
  const popupUrl = (0, _blobHelpers.getImageViewerPath)(config, (0, _contentHelpers.getContentPath)(config, recordType, vocabulary, csid, popupSubresource, recordData));
  const previewUrl = (0, _contentHelpers.getContentPath)(config, recordType, vocabulary, csid, previewSubresource, recordData);

  if (!previewUrl) {
    return null;
  }

  const previewTitle = intl.formatMessage(messages.previewTitle);
  const errorMessage = intl.formatMessage(messages.error);
  const pendingMessage = intl.formatMessage(messages.pending);
  return /*#__PURE__*/_react.default.createElement("a", {
    className: _ContentViewer.default.common,
    href: popupUrl,
    target: _blobHelpers.VIEWER_WINDOW_NAME
  }, /*#__PURE__*/_react.default.createElement(_ImageContainer.default, {
    src: previewUrl,
    alt: previewTitle,
    retry: true,
    errorMessage: errorMessage,
    pendingMessage: pendingMessage
  }));
}

ContentViewer.contextTypes = contextTypes;