"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() { 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; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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 _this$props = this.props,
          {
      /* eslint-disable no-unused-vars */
      onSearchDescriptorChange,
      search
    } = _this$props,
          remainingProps = _objectWithoutProperties(_this$props, ["onSearchDescriptorChange", "search"]);

    return _react.default.createElement(_SearchResultTableContainer.default, remainingProps);
  }

}

exports.default = WatchedSearchResultTable;
WatchedSearchResultTable.propTypes = propTypes;