'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _recordDataHelpers = require('../../helpers/recordDataHelpers');

var _MediaViewerPanelContainer = require('../../containers/media/MediaViewerPanelContainer');

var _MediaViewerPanelContainer2 = _interopRequireDefault(_MediaViewerPanelContainer);

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

var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'mediaSnapshotPanel.title',
    'defaultMessage': 'Media'
  }
});

// TODO: Make sort param configurable.

var getSearchDescriptor = function getSearchDescriptor(relatedCsid, updatedTimestamp) {
  return {
    recordType: 'media',
    searchQuery: {
      rel: relatedCsid,
      p: 0,
      size: 2500,
      sort: 'title'
    },
    seqID: updatedTimestamp
  };
};

var propTypes = {
  color: _react.PropTypes.string,
  config: _react.PropTypes.object,
  csid: _react.PropTypes.string,
  recordData: _react.PropTypes.instanceOf(_immutable2.default.Map),
  recordType: _react.PropTypes.string
};

function MediaSnapshotPanel(props) {
  var color = props.color,
      config = props.config,
      csid = props.csid,
      recordData = props.recordData,
      recordType = props.recordType;


  var searchDescriptor = getSearchDescriptor(csid, (0, _recordDataHelpers.getUpdatedTimestamp)(recordData));

  if (!searchDescriptor.seqID) {
    // Don't render until after the record has loaded.

    return null;
  }

  var ownBlobCsid = void 0;

  if (recordType === 'media') {
    // For media records, pass in the record's own blobCsid. Other types only have related media
    // blobs.

    ownBlobCsid = recordData.getIn(['document', 'ns2:media_common', 'blobCsid']);
  }

  return _react2.default.createElement(_MediaViewerPanelContainer2.default, {
    color: color,
    columnSetName: 'narrow',
    config: config,
    csid: csid,
    name: 'mediaSnapshotPanel',
    ownBlobCsid: ownBlobCsid,
    searchDescriptor: searchDescriptor,
    recordType: recordType,
    title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title)
  });
}

MediaSnapshotPanel.propTypes = propTypes;