"use strict";

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

var _reactRedux = require("react-redux");

var _SearchPanel = _interopRequireDefault(require("../../components/search/SearchPanel"));

var _search = require("../../actions/search");

var _prefs = require("../../actions/prefs");

var _reducers = require("../../reducers");

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

const mapStateToProps = (state, ownProps) => {
  const {
    name,
    recordType,
    searchDescriptor: providedSearchDescriptor
  } = ownProps;
  const preferredPageSize = (0, _reducers.getSearchPanelPageSize)(state, recordType, name);
  const providedSearchQuery = providedSearchDescriptor.get('searchQuery');
  let searchDescriptor;

  if (preferredPageSize && preferredPageSize !== providedSearchQuery.get('size') && !providedSearchQuery.get('p')) {
    // A preferred page size exists. Override the provided page size.
    searchDescriptor = providedSearchDescriptor.set('searchQuery', providedSearchQuery.set('size', preferredPageSize));
  } else {
    searchDescriptor = providedSearchDescriptor;
  }

  return {
    searchDescriptor,
    searchResult: (0, _reducers.getSearchResult)(state, name, searchDescriptor)
  };
};

const mapDispatchToProps = {
  search: _search.search,
  setPreferredPageSize: _prefs.setSearchPanelPageSize
};

var _default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_SearchPanel.default);

exports.default = _default;