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

var getSearchDescriptor = function getSearchDescriptor(props) {
  var recordType = props.recordType,
      vocabulary = props.vocabulary,
      csid = props.csid,
      recordData = props.recordData;


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

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 TermsUsedPanel = function (_Component) {
  _inherits(TermsUsedPanel, _Component);

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

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

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

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

  _createClass(TermsUsedPanel, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var searchDescriptor = this.state.searchDescriptor;

      var 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.

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

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

  return TermsUsedPanel;
}(_react.Component);

exports.default = TermsUsedPanel;


TermsUsedPanel.propTypes = propTypes;