'use strict';

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

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 _permissionHelpers = require('../../helpers/permissionHelpers');

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(props) {
  var csid = props.csid,
      recordRelationUpdatedTimestamp = props.recordRelationUpdatedTimestamp;


  return _immutable2.default.fromJS({
    recordType: 'media',
    searchQuery: {
      rel: csid,
      p: 0,
      size: 2500,
      sort: 'title'
    },
    seqID: recordRelationUpdatedTimestamp
  });
};

var propTypes = {
  color: _propTypes2.default.string,
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordData: _propTypes2.default.instanceOf(_immutable2.default.Map),
  // This use isn't detected by eslint.
  /* eslint-disable react/no-unused-prop-types */
  recordRelationUpdatedTimestamp: _propTypes2.default.string,
  /* eslint-enable react/no-unused-prop-types */
  recordType: _propTypes2.default.string
};

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


  var searchDescriptor = getSearchDescriptor(props);

  // Don't render if list permissions on media are not present.
  // Don't render until after the record has loaded.

  if (!(0, _permissionHelpers.canList)('media', perms) || !(0, _recordDataHelpers.getUpdatedTimestamp)(recordData)) {
    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, {
    collapsed: true,
    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;