'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _warning = require('warning');

var _warning2 = _interopRequireDefault(_warning);

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

var _ImageContainer2 = _interopRequireDefault(_ImageContainer);

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

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

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

var _ContentViewer2 = _interopRequireDefault(_ContentViewer);

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: _propTypes2.default.object,
  recordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  csid: _propTypes2.default.string,
  intl: _reactIntl.intlShape,
  recordData: _propTypes2.default.instanceOf(_immutable2.default.Map)
};

function ContentViewer(props, context) {
  const config = context.config,
        recordType = context.recordType,
        vocabulary = context.vocabulary,
        csid = context.csid,
        intl = context.intl,
        recordData = context.recordData;


  const content = (0, _get2.default)(config, ['recordTypes', recordType, 'content']);

  process.env.NODE_ENV !== 'production' ? (0, _warning2.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, _get2.default)(content, ['popup', 'subresource']);
  const previewSubresource = (0, _get2.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 _react2.default.createElement(
    'a',
    { className: _ContentViewer2.default.common, href: popupUrl, target: _blobHelpers.VIEWER_WINDOW_NAME },
    _react2.default.createElement(_ImageContainer2.default, {
      src: previewUrl,
      alt: previewTitle,
      retry: true,
      errorMessage: errorMessage,
      pendingMessage: pendingMessage
    })
  );
}

ContentViewer.contextTypes = contextTypes;