'use strict';

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

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

var _merge = require('lodash/merge');

var _merge2 = _interopRequireDefault(_merge);

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

var mapStateToProps = function mapStateToProps(state, ownProps) {
  var name = ownProps.name,
      recordType = ownProps.recordType,
      providedSearchDescriptor = ownProps.searchDescriptor;


  var preferredPageSize = (0, _reducers.getSearchPanelPageSize)(state, recordType, name);

  var searchDescriptor = void 0;

  if (preferredPageSize && preferredPageSize !== providedSearchDescriptor.searchQuery.size && !providedSearchDescriptor.searchQuery.p) {
    // A preferred page size exists. Override the provded page size.

    searchDescriptor = (0, _merge2.default)({}, providedSearchDescriptor, {
      searchQuery: {
        size: preferredPageSize
      }
    });
  } else {
    searchDescriptor = providedSearchDescriptor;
  }

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

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

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