"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 _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "termsUsedPanel.title",
    "defaultMessage": "Terms Used"
  }
});
const getSearchDescriptor = props => {
  const {
    config,
    recordType,
    vocabulary,
    csid,
    recordData
  } = props;
  return _immutable.default.fromJS({
    recordType,
    vocabulary,
    csid,
    subresource: 'terms',
    searchQuery: {
      p: 0,
      size: config.defaultSearchPanelSize || 5
    },
    seqID: (0, _recordDataHelpers.getUpdatedTimestamp)(recordData)
  });
};
const propTypes = {
  color: _propTypes.default.string,
  config: _propTypes.default.shape({
    listTypes: _propTypes.default.object
  }),
  csid: _propTypes.default.string,
  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)
    };
  }

  // eslint-disable-next-line camelcase
  UNSAFE_componentWillReceiveProps(nextProps) {
    const {
      searchDescriptor
    } = this.state;
    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,
      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 /*#__PURE__*/_react.default.createElement(_SearchPanelContainer.default, {
      collapsed: true,
      color: color,
      columnSetName: "narrow",
      config: config,
      csid: csid,
      listType: "authRef",
      name: "termsUsedPanel",
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      vocabulary: vocabulary,
      title: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }
}
exports.default = TermsUsedPanel;
TermsUsedPanel.propTypes = propTypes;