"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 _SearchResultTableContainer = _interopRequireDefault(require("../../containers/search/SearchResultTableContainer"));
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 propTypes = {
  searchDescriptor: _propTypes.default.instanceOf(_immutable.default.Map),
  searchError: _propTypes.default.instanceOf(_immutable.default.Map),
  searchResult: _propTypes.default.instanceOf(_immutable.default.Map),
  search: _propTypes.default.func,
  onSearchDescriptorChange: _propTypes.default.func
};

/**
 * A wrapper around SearchResultTable that notifies when the search descriptor is changed, and
 * re-executes the search if the search results are cleared.
 */
class WatchedSearchResultTable extends _react.Component {
  componentDidUpdate(prevProps) {
    const {
      searchDescriptor: prevSearchDescriptor,
      searchResult: prevSearchResult
    } = prevProps;
    const {
      search,
      searchDescriptor,
      searchError,
      searchResult,
      onSearchDescriptorChange
    } = this.props;
    if (onSearchDescriptorChange && !_immutable.default.is(prevSearchDescriptor, searchDescriptor)) {
      onSearchDescriptorChange(searchDescriptor);
    }
    if (search && typeof searchResult === 'undefined' && prevSearchResult && !searchError) {
      search();
    }
  }
  render() {
    const {
      onSearchDescriptorChange,
      search,
      ...remainingProps
    } = this.props;
    return /*#__PURE__*/_react.default.createElement(_SearchResultTableContainer.default, remainingProps);
  }
}
exports.default = WatchedSearchResultTable;
WatchedSearchResultTable.propTypes = propTypes;