'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 _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 _ContentViewer = require('../../../styles/cspace-ui/ContentViewer.css');

var _ContentViewer2 = _interopRequireDefault(_ContentViewer);

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

var getContentPath = function getContentPath(config, recordType, vocabulary, csid, subresource) {
  if (!csid) {
    return null;
  }

  var recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);
  var vocabularyConfig = (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary]);
  var subresourceConfig = (0, _get2.default)(config, ['subresources', subresource]);

  var recordServicePath = (0, _get2.default)(recordTypeConfig, ['serviceConfig', 'servicePath']);
  var vocabularyServicePath = (0, _get2.default)(vocabularyConfig, ['serviceConfig', 'servicePath']);

  var pathParts = [recordServicePath];

  if (vocabularyServicePath) {
    pathParts.push(vocabularyServicePath);
    pathParts.push('items');
  }

  pathParts.push(csid);

  if (subresourceConfig) {
    var subresourceServicePath = (0, _get2.default)(subresourceConfig, ['serviceConfig', 'servicePath']);

    if (subresourceServicePath) {
      pathParts.push(subresourceServicePath);
    }
  }

  pathParts.push('content');

  var path = pathParts.join('/');

  return path;
};

var 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'
  }
});

var contextTypes = {
  config: _propTypes2.default.object,
  recordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  csid: _propTypes2.default.string,
  intl: _reactIntl.intlShape
};

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


  var 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;
  }

  var fullSubresource = (0, _get2.default)(content, ['full', 'subresource']);
  var previewSubresource = (0, _get2.default)(content, ['preview', 'subresource']);

  var fullUrl = (0, _blobHelpers.getImageViewerPath)(config, getContentPath(config, recordType, vocabulary, csid, fullSubresource));

  var previewUrl = getContentPath(config, recordType, vocabulary, csid, previewSubresource);

  if (!previewUrl) {
    return null;
  }

  var previewTitle = intl.formatMessage(messages.previewTitle);
  var errorMessage = intl.formatMessage(messages.error);
  var pendingMessage = intl.formatMessage(messages.pending);

  return _react2.default.createElement(
    'a',
    { className: _ContentViewer2.default.common, href: fullUrl, target: _blobHelpers.VIEWER_WINDOW_NAME },
    _react2.default.createElement(_ImageContainer2.default, {
      src: previewUrl,
      alt: previewTitle,
      retry: true,
      errorMessage: errorMessage,
      pendingMessage: pendingMessage
    })
  );
}

ContentViewer.contextTypes = contextTypes;