"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() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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": "usedByPanel.title",
    "defaultMessage": "Used By"
  }
});

const getSearchDescriptor = (recordType, vocabulary, csid, updatedTimestamp) => _immutable.default.fromJS({
  recordType,
  vocabulary,
  csid,
  subresource: 'refs',
  searchQuery: {
    p: 0,
    size: 5
  },
  seqID: updatedTimestamp
});

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 UsedByPanel extends _react.Component {
  constructor(props) {
    super(props);
    this.handleSearchDescriptorChange = this.handleSearchDescriptorChange.bind(this);
    const {
      csid,
      recordData,
      recordType,
      vocabulary
    } = this.props;
    const searchDescriptor = getSearchDescriptor(recordType, vocabulary, csid, (0, _recordDataHelpers.getUpdatedTimestamp)(recordData));
    this.state = {
      searchDescriptor
    };
  }

  componentWillReceiveProps(nextProps) {
    const {
      csid,
      recordData,
      recordType,
      vocabulary
    } = this.props;
    const updatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(recordData);
    const {
      csid: nextCsid,
      recordData: nextRecordData,
      recordType: nextRecordType,
      vocabulary: nextVocabulary
    } = nextProps;
    const nextUpdatedTimestamp = (0, _recordDataHelpers.getUpdatedTimestamp)(nextRecordData);

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

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

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

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

    if (!searchDescriptor.get('seqID')) {
      // 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: "refDoc",
      name: "usedByPanel",
      searchDescriptor: searchDescriptor,
      recordType: recordType,
      vocabulary: vocabulary,
      title: _react.default.createElement(_reactIntl.FormattedMessage, messages.title),
      onSearchDescriptorChange: this.handleSearchDescriptorChange
    });
  }

}

exports.default = UsedByPanel;
UsedByPanel.propTypes = propTypes;