'use strict';

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

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

var _SearchPanelContainer = require('../../containers/search/SearchPanelContainer');

var _SearchPanelContainer2 = _interopRequireDefault(_SearchPanelContainer);

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

const messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'relatedMediaPanel.title',
    'defaultMessage': 'Related Media'
  }
});

const getSearchDescriptor = (relatedCsid, updatedTimestamp) => _immutable2.default.fromJS({
  recordType: 'media',
  searchQuery: {
    rel: relatedCsid,
    p: 0,
    size: 5
  },
  seqID: updatedTimestamp
});

const propTypes = {
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  recordData: _propTypes2.default.instanceOf(_immutable2.default.Map),
  recordType: _propTypes2.default.string
};

class RelatedMediaPanel extends _react.Component {
  constructor(props) {
    super(props);

    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);

    var _props = this.props;
    const csid = _props.csid,
          recordData = _props.recordData;


    this.state = {
      searchDescriptor: getSearchDescriptor(csid, (0, _recordDataHelpers.getUpdatedTimestamp)(recordData))
    };
  }

  componentWillReceiveProps(nextProps) {
    var _props2 = this.props;
    const csid = _props2.csid,
          recordData = _props2.recordData;


    const updatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(recordData);

    const nextCsid = nextProps.csid,
          nextRecordData = nextProps.recordData;


    const nextUpdatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(nextRecordData);

    if (nextCsid !== csid || nextUpdatedTimestamp !== updatedTimestamp) {
      this.setState({
        searchDescriptor: getSearchDescriptor(nextCsid, nextUpdatedTimestamp)
      });
    }
  }

  handleSearchDescriptorChange(searchDescriptor) {
    this.setState({
      searchDescriptor
    });
  }

  render() {
    var _props3 = this.props;
    const config = _props3.config,
          csid = _props3.csid,
          recordType = _props3.recordType;
    const searchDescriptor = this.state.searchDescriptor;


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

      return null;
    }

    return _react2.default.createElement(_SearchPanelContainer2.default, {
      collapsed: true,
      columnSetName: 'narrow',
      config: config,
      csid: csid,
      name: 'relatedMediaPanel',
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }
}

exports.default = RelatedMediaPanel;
RelatedMediaPanel.propTypes = propTypes;