'use strict';

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

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

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

var SET_KEYWORD_SEARCH_KEYWORD = exports.SET_KEYWORD_SEARCH_KEYWORD = 'SET_KEYWORD_SEARCH_KEYWORD';

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

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

    var pathname = void 0;

    var query = {};

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

      pathname = '/record/' + recordType + '/' + kw;
    } else {
      var vocabularyPath = vocabulary ? '/' + vocabulary : '';

      pathname = '/list/' + recordType + vocabularyPath;

      if (kw) {
        query.kw = kw;
      }
    }

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