'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.clearMatchedTerms = exports.findMatchingTerms = exports.addTerm = exports.CLEAR_PARTIAL_TERM_SEARCH_RESULTS = exports.PARTIAL_TERM_SEARCH_REJECTED = exports.PARTIAL_TERM_SEARCH_FULFILLED = exports.PARTIAL_TERM_SEARCH_STARTED = exports.ADD_TERM_REJECTED = exports.ADD_TERM_FULFILLED = exports.ADD_TERM_STARTED = undefined;

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _cspace = require('./cspace');

var _cspace2 = _interopRequireDefault(_cspace);

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

var ADD_TERM_STARTED = exports.ADD_TERM_STARTED = 'ADD_TERM_STARTED';
var ADD_TERM_FULFILLED = exports.ADD_TERM_FULFILLED = 'ADD_TERM_FULFILLED';
var ADD_TERM_REJECTED = exports.ADD_TERM_REJECTED = 'ADD_TERM_REJECTED';
var PARTIAL_TERM_SEARCH_STARTED = exports.PARTIAL_TERM_SEARCH_STARTED = 'PARTIAL_TERM_SEARCH_STARTED';
var PARTIAL_TERM_SEARCH_FULFILLED = exports.PARTIAL_TERM_SEARCH_FULFILLED = 'PARTIAL_TERM_SEARCH_FULFILLED';
var PARTIAL_TERM_SEARCH_REJECTED = exports.PARTIAL_TERM_SEARCH_REJECTED = 'PARTIAL_TERM_SEARCH_REJECTED';
var CLEAR_PARTIAL_TERM_SEARCH_RESULTS = exports.CLEAR_PARTIAL_TERM_SEARCH_RESULTS = 'CLEAR_PARTIAL_TERM_SEARCH_RESULTS';

var addTerm = exports.addTerm = function addTerm(recordTypeConfig, vocabulary, displayName) {
  return function (dispatch) {
    var recordType = (0, _get2.default)(recordTypeConfig, 'name');
    var serviceConfig = (0, _get2.default)(recordTypeConfig, 'serviceConfig');
    var servicePath = (0, _get2.default)(serviceConfig, 'servicePath');

    var vocabularyServicePath = vocabulary ? (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary, 'serviceConfig', 'servicePath']) : undefined;

    dispatch({
      type: ADD_TERM_STARTED,
      meta: {
        displayName: displayName,
        recordType: recordType,
        vocabulary: vocabulary
      }
    });

    var config = {
      data: serviceConfig.quickAddData({
        displayName: displayName
      })
    };

    var itemPath = vocabularyServicePath ? '/' + vocabularyServicePath + '/items' : '';

    return (0, _cspace2.default)().create('' + servicePath + itemPath, config).then(function (response) {
      var location = response.headers.location;

      // Workaround for DRYD-178
      // Should be able to just read response.headers.location, but it might contain the wrong
      // protocol, so parse out the path after cspace-services, and use that.

      var path = location.split('cspace-services/')[1];

      return (0, _cspace2.default)().read(path);
    }).then(function (response) {
      return dispatch({
        type: ADD_TERM_FULFILLED,
        payload: response,
        meta: {
          displayName: displayName,
          recordType: recordType,
          vocabulary: vocabulary
        }
      });
    }).catch(function (error) {
      return dispatch({
        type: ADD_TERM_REJECTED,
        payload: error,
        meta: {
          displayName: displayName,
          recordType: recordType,
          vocabulary: vocabulary
        }
      });
    });
  };
};

var findMatchingTerms = exports.findMatchingTerms = function findMatchingTerms(recordTypeConfig, vocabulary, partialTerm) {
  return function (dispatch) {
    var recordType = (0, _get2.default)(recordTypeConfig, 'name');
    var servicePath = (0, _get2.default)(recordTypeConfig, ['serviceConfig', 'servicePath']);

    var vocabularyServicePath = vocabulary ? (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary, 'serviceConfig', 'servicePath']) : undefined;

    // Disable this warning, because shared vocabs are not defined in core, but are configured as
    // sources in autocompletes.
    // eslint-disable-next-line max-len
    // warning(vocabularyServicePath, `No service path found for the vocabulary ${vocabulary} in record type ${recordType}. Partial term search will not include this vocabulary.`);

    if (recordTypeConfig.vocabularies && !vocabularyServicePath) {
      return null;
    }

    dispatch({
      type: PARTIAL_TERM_SEARCH_STARTED,
      meta: {
        partialTerm: partialTerm,
        recordType: recordType,
        vocabulary: vocabulary
      }
    });

    var config = {
      params: {
        pt: partialTerm,
        wf_deleted: 'false'
      }
    };

    var itemPath = vocabularyServicePath ? '/' + vocabularyServicePath + '/items' : '';

    return (0, _cspace2.default)().read('' + servicePath + itemPath, config).then(function (response) {
      return dispatch({
        type: PARTIAL_TERM_SEARCH_FULFILLED,
        payload: response,
        meta: {
          partialTerm: partialTerm,
          recordType: recordType,
          vocabulary: vocabulary
        }
      });
    }).catch(function (error) {
      return dispatch({
        type: PARTIAL_TERM_SEARCH_REJECTED,
        payload: error,
        meta: {
          partialTerm: partialTerm,
          recordType: recordType,
          vocabulary: vocabulary
        }
      });
    });
  };
};

var clearMatchedTerms = exports.clearMatchedTerms = function clearMatchedTerms() {
  return {
    type: CLEAR_PARTIAL_TERM_SEARCH_RESULTS
  };
};