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

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

const addTerm = exports.addTerm = (recordTypeConfig, vocabulary, displayName, partialTerm) => dispatch => {
  const recordType = (0, _get2.default)(recordTypeConfig, 'name');
  const serviceConfig = (0, _get2.default)(recordTypeConfig, 'serviceConfig');
  const servicePath = (0, _get2.default)(serviceConfig, 'servicePath');

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

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

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

  const itemPath = vocabularyServicePath ? `/${vocabularyServicePath}/items` : '';

  return (0, _cspace2.default)().create(`${servicePath}${itemPath}`, config).then(response => {
    const 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.

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

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

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

  const 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,
      recordType,
      vocabulary
    }
  });

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

  const itemPath = vocabularyServicePath ? `/${vocabularyServicePath}/items` : '';

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

const clearMatchedTerms = exports.clearMatchedTerms = () => ({
  type: CLEAR_PARTIAL_TERM_SEARCH_RESULTS
});