"use strict";

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

var _qs = _interopRequireDefault(require("qs"));

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

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

var _actionCodes = require("../constants/actionCodes");

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

const setQuickSearchKeyword = value => ({
  type: _actionCodes.SET_QUICK_SEARCH_KEYWORD,
  payload: value
});

exports.setQuickSearchKeyword = setQuickSearchKeyword;

const initiateSearch = push => (dispatch, getState) => {
  const keyword = (0, _reducers.getQuickSearchKeyword)(getState());
  const kw = keyword ? keyword.trim() : '';
  const recordType = (0, _reducers.getQuickSearchRecordType)(getState());
  const vocabulary = (0, _reducers.getQuickSearchVocabulary)(getState(), recordType);
  let pathname;
  let search;
  const 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) {
      const query = {
        kw
      };

      const queryString = _qs.default.stringify(query);

      search = `?${queryString}`;
    }
  }

  push({
    pathname,
    search
  });
};

exports.initiateSearch = initiateSearch;