'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

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

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

var _SearchPanel2 = _interopRequireDefault(_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 = ownProps.name,
        recordType = ownProps.recordType,
        providedSearchDescriptor = ownProps.searchDescriptor;


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

exports.default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_SearchPanel2.default);