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

const getSearchDescriptor = props => {
  const recordType = props.recordType,
        vocabulary = props.vocabulary,
        csid = props.csid,
        recordData = props.recordData;


  return _immutable2.default.fromJS({
    recordType,
    vocabulary,
    csid,
    subresource: 'terms',
    searchQuery: {
      p: 0,
      size: 5
    },
    seqID: (0, _recordDataHelpers.getUpdatedTimestamp)(recordData)
  });
};

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 TermsUsedPanel extends _react.Component {
  constructor(props) {
    super(props);

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

    this.state = {
      searchDescriptor: getSearchDescriptor(props)
    };
  }

  componentWillReceiveProps(nextProps) {
    const searchDescriptor = this.state.searchDescriptor;

    let nextSearchDescriptor = getSearchDescriptor(nextProps);

    if (!_immutable2.default.is(searchDescriptor, nextSearchDescriptor)) {
      if (searchDescriptor.get('csid') === nextSearchDescriptor.get('csid') && searchDescriptor.get('recordType') === nextSearchDescriptor.get('recordType') && searchDescriptor.get('vocabulary') === nextSearchDescriptor.get('vocabulary')) {
        // The record type, vocabulary, and csid didn't change, so carry over the page number, size,
        // and sort from the current search descriptor.

        const searchQuery = searchDescriptor.get('searchQuery');

        const nextSearchQuery = nextSearchDescriptor.get('searchQuery').set('p', searchQuery.get('p')).set('size', searchQuery.get('size')).set('sort', searchQuery.get('sort'));

        nextSearchDescriptor = nextSearchDescriptor.set('searchQuery', nextSearchQuery);
      }

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

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

  render() {
    var _props = this.props;
    const color = _props.color,
          config = _props.config,
          csid = _props.csid,
          history = _props.history,
          recordType = _props.recordType,
          vocabulary = _props.vocabulary,
          recordData = _props.recordData;
    const searchDescriptor = this.state.searchDescriptor;


    if (!(0, _recordDataHelpers.getUpdatedTimestamp)(recordData)) {
      // 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: 'authRef',
      name: 'termsUsedPanel',
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      vocabulary: vocabulary,
      title: _react2.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }
}

exports.default = TermsUsedPanel;
TermsUsedPanel.propTypes = propTypes;