'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var messages = (0, _reactIntl.defineMessages)({
  title: {
    'id': 'usedByPanel.title',
    'defaultMessage': 'Used By'
  }
});

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

var 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
};

var UsedByPanel = function (_Component) {
  _inherits(UsedByPanel, _Component);

  function UsedByPanel(props) {
    _classCallCheck(this, UsedByPanel);

    var _this = _possibleConstructorReturn(this, (UsedByPanel.__proto__ || Object.getPrototypeOf(UsedByPanel)).call(this, props));

    _this.handleSearchDescriptorChange = _this.handleSearchDescriptorChange.bind(_this);

    var _this$props = _this.props,
        csid = _this$props.csid,
        recordData = _this$props.recordData,
        recordType = _this$props.recordType,
        vocabulary = _this$props.vocabulary;


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

    _this.state = {
      searchDescriptor: searchDescriptor
    };
    return _this;
  }

  _createClass(UsedByPanel, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var _props = this.props,
          csid = _props.csid,
          recordData = _props.recordData,
          recordType = _props.recordType,
          vocabulary = _props.vocabulary;


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

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


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

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

        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
        });
      }
    }
  }, {
    key: 'handleSearchDescriptorChange',
    value: function handleSearchDescriptorChange(searchDescriptor) {
      this.setState({
        searchDescriptor: searchDescriptor
      });
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          color = _props2.color,
          config = _props2.config,
          csid = _props2.csid,
          history = _props2.history,
          recordType = _props2.recordType,
          vocabulary = _props2.vocabulary;
      var 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
      });
    }
  }]);

  return UsedByPanel;
}(_react.Component);

exports.default = UsedByPanel;


UsedByPanel.propTypes = propTypes;