'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.initiateSearch = exports.setQuickSearchKeyword = exports.SET_QUICK_SEARCH_KEYWORD = undefined;

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

var _csidHelpers = require('../helpers/csidHelpers');

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

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

var SET_QUICK_SEARCH_KEYWORD = exports.SET_QUICK_SEARCH_KEYWORD = 'SET_QUICK_SEARCH_KEYWORD';

var setQuickSearchKeyword = exports.setQuickSearchKeyword = function setQuickSearchKeyword(value) {
  return {
    type: SET_QUICK_SEARCH_KEYWORD,
    payload: value
  };
};

var initiateSearch = exports.initiateSearch = function initiateSearch(push) {
  return function (dispatch, getState) {
    var keyword = (0, _reducers.getQuickSearchKeyword)(getState());
    var kw = keyword ? keyword.trim() : '';
    var recordType = (0, _reducers.getQuickSearchRecordType)(getState());
    var vocabulary = (0, _reducers.getQuickSearchVocabulary)(getState(), recordType);

    var pathname = void 0;
    var search = void 0;

    var vocabularyPath = vocabulary ? '/' + vocabulary : '';

    if ((0, _csidHelpers.isCsid)(kw)) {
      // Go straight to the record with the csid.

      pathname = '/record/' + recordType + vocabularyPath + '/' + kw;
    } else {
      pathname = '/list/' + recordType + vocabularyPath;

      if (kw) {
        var query = {
          kw: kw
        };

        var queryString = _qs2.default.stringify(query);

        search = '?' + queryString;
      }
    }

    push({
      pathname: pathname,
      search: search
    });
  };
};