"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = MediaSnapshotPanel;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _immutable = _interopRequireDefault(require("immutable"));
var _reactIntl = require("react-intl");
var _permissionHelpers = require("../../helpers/permissionHelpers");
var _recordDataHelpers = require("../../helpers/recordDataHelpers");
var _MediaViewerPanelContainer = _interopRequireDefault(require("../../containers/media/MediaViewerPanelContainer"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "mediaSnapshotPanel.title",
    "defaultMessage": "Media"
  }
});
const getSearchDescriptor = props => {
  const {
    csid,
    mediaRecordType,
    recordData,
    recordRelationUpdatedTimestamp,
    recordType,
    sort
  } = props;

  // Update the media snapshot when relations have changed.

  let updatedTimestamp = recordRelationUpdatedTimestamp;
  if (recordType === mediaRecordType) {
    // For a media record, the media snapshot needs to be updated when either the record data
    // changes (because the blobCsid may have changed), or relations have changed.

    const recordUpdatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(recordData);
    updatedTimestamp = recordRelationUpdatedTimestamp > recordUpdatedTimestamp ? recordRelationUpdatedTimestamp : recordUpdatedTimestamp;
  }
  return _immutable.default.fromJS({
    recordType: mediaRecordType,
    searchQuery: {
      sort,
      rel: csid,
      p: 0,
      size: 2500
    },
    seqID: updatedTimestamp
  });
};
const propTypes = {
  color: _propTypes.default.string,
  config: _propTypes.default.shape({
    listTypes: _propTypes.default.object
  }),
  csid: _propTypes.default.string,
  mediaRecordType: _propTypes.default.string,
  mediaRecordBlobCsidField: _propTypes.default.string,
  name: _propTypes.default.string,
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  recordData: _propTypes.default.instanceOf(_immutable.default.Map),
  // These uses aren't detected by eslint.
  /* eslint-disable react/no-unused-prop-types */
  sort: _propTypes.default.string,
  recordRelationUpdatedTimestamp: _propTypes.default.string,
  /* eslint-enable react/no-unused-prop-types */
  recordType: _propTypes.default.string,
  titleMessage: _propTypes.default.shape({
    id: _propTypes.default.string,
    defaultMessage: _propTypes.default.string
  })
};
const defaultProps = {
  mediaRecordType: 'media',
  mediaRecordBlobCsidField: 'ns2:media_common/blobCsid',
  name: 'mediaSnapshotPanel',
  sort: 'title',
  titleMessage: messages.title
};
function MediaSnapshotPanel(props) {
  const {
    color,
    config,
    csid,
    mediaRecordType,
    mediaRecordBlobCsidField,
    name,
    perms,
    recordData,
    recordType,
    titleMessage
  } = props;
  const 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)(mediaRecordType, perms) || !(0, _recordDataHelpers.getUpdatedTimestamp)(recordData)) {
    return null;
  }
  let ownBlobCsid;
  if (recordType === mediaRecordType) {
    // For media records, pass in the record's own blobCsid. Other types only have related media
    // blobs.

    ownBlobCsid = recordData.getIn(['document', ...mediaRecordBlobCsidField.split('/')]);
  }
  return /*#__PURE__*/_react.default.createElement(_MediaViewerPanelContainer.default, {
    collapsed: true,
    color: color,
    config: config,
    csid: csid,
    name: name,
    ownBlobCsid: ownBlobCsid,
    searchDescriptor: searchDescriptor,
    recordType: recordType,
    title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, titleMessage)
  });
}
MediaSnapshotPanel.propTypes = propTypes;
MediaSnapshotPanel.defaultProps = defaultProps;