'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': 'usedByPanel.title',
    'defaultMessage': 'Used By'
  }
});

const getSearchDescriptor = (recordType, vocabulary, csid, updatedTimestamp) => _immutable2.default.fromJS({
  recordType,
  vocabulary,
  csid,
  subresource: 'refs',
  searchQuery: {
    p: 0,
    size: 5
  },
  seqID: updatedTimestamp
});

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

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

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

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


    const searchDescriptor = getSearchDescriptor(recordType, vocabulary, csid, (0, _recordDataHelpers.getUpdatedTimestamp)(recordData));

    this.state = {
      searchDescriptor
    };
  }

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


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

    const nextCsid = nextProps.csid,
          nextRecordData = nextProps.recordData,
          nextRecordType = nextProps.recordType,
          nextVocabulary = nextProps.vocabulary;


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

    if (nextCsid !== csid || nextRecordType !== recordType || nextVocabulary !== vocabulary || nextUpdatedTimestamp !== updatedTimestamp) {
      let newSearchDescriptor;

      if (nextCsid === csid && nextRecordType === recordType && nextVocabulary === vocabulary) {
        // Only the updated timestamp changed, so just update the seq id of the search.

        newSearchDescriptor = this.state.searchDescriptor.set('seqID', nextUpdatedTimestamp);
      } else {
        newSearchDescriptor = getSearchDescriptor(nextRecordType, nextVocabulary, nextCsid, nextUpdatedTimestamp);
      }

      this.setState({
        searchDescriptor: newSearchDescriptor
      });
    }
  }

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

  render() {
    var _props3 = this.props;
    const color = _props3.color,
          config = _props3.config,
          csid = _props3.csid,
          history = _props3.history,
          recordType = _props3.recordType,
          vocabulary = _props3.vocabulary;
    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,
      color: color,
      columnSetName: 'narrow',
      config: config,
      csid: csid,
      history: history,
      listType: 'refDoc',
      name: 'usedByPanel',
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      vocabulary: vocabulary,
      title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }
}

exports.default = UsedByPanel;
UsedByPanel.propTypes = propTypes;