"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _immutable = _interopRequireDefault(require("immutable"));

var _reactIntl = require("react-intl");

var _recordDataHelpers = require("../../helpers/recordDataHelpers");

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

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "termsUsedPanel.title",
    "defaultMessage": "Terms Used"
  }
});

const getSearchDescriptor = props => {
  const {
    recordType,
    vocabulary,
    csid,
    recordData
  } = props;
  return _immutable.default.fromJS({
    recordType,
    vocabulary,
    csid,
    subresource: 'terms',
    searchQuery: {
      p: 0,
      size: 5
    },
    seqID: (0, _recordDataHelpers.getUpdatedTimestamp)(recordData)
  });
};

const propTypes = {
  color: _propTypes.default.string,
  config: _propTypes.default.object,
  csid: _propTypes.default.string,
  history: _propTypes.default.object,
  recordData: _propTypes.default.instanceOf(_immutable.default.Map),
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.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 (!_immutable.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() {
    const {
      color,
      config,
      csid,
      history,
      recordType,
      vocabulary,
      recordData
    } = this.props;
    const {
      searchDescriptor
    } = this.state;

    if (!(0, _recordDataHelpers.getUpdatedTimestamp)(recordData)) {
      // Don't render until after the record has loaded.
      return null;
    }

    return _react.default.createElement(_SearchPanelContainer.default, {
      collapsed: true,
      color: color,
      columnSetName: "narrow",
      config: config,
      csid: csid,
      history: history,
      listType: "authRef",
      name: "termsUsedPanel",
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      vocabulary: vocabulary,
      title: _react.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }

}

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