'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 _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(authorityRecordTypeConfig, vocabularyName, displayName) {
  return function (dispatch) {
    var vocabularies = authorityRecordTypeConfig.vocabularies,
        authorityName = authorityRecordTypeConfig.name,
        authorityServiceConfig = authorityRecordTypeConfig.serviceConfig;
    var authorityServicePath = authorityServiceConfig.servicePath;
    var vocabularyServicePath = vocabularies[vocabularyName].serviceConfig.servicePath;


    dispatch({
      type: ADD_TERM_STARTED,
      meta: {
        displayName: displayName,
        authorityName: authorityName,
        vocabularyName: vocabularyName
      }
    });

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

    return (0, _cspace2.default)().create(authorityServicePath + '/' + vocabularyServicePath + '/items', config).then(function (response) {
      return (0, _cspace2.default)().read(response.headers.location);
    }).then(function (response) {
      return dispatch({
        type: ADD_TERM_FULFILLED,
        payload: response,
        meta: {
          displayName: displayName,
          authorityName: authorityName,
          vocabularyName: vocabularyName
        }
      });
    }).catch(function (error) {
      return dispatch({
        type: ADD_TERM_REJECTED,
        payload: error,
        meta: {
          displayName: displayName,
          authorityName: authorityName,
          vocabularyName: vocabularyName
        }
      });
    });
  };
};

var findMatchingTerms = exports.findMatchingTerms = function findMatchingTerms(authorityRecordTypeConfig, vocabularyName, partialTerm) {
  return function (dispatch) {
    var vocabularies = authorityRecordTypeConfig.vocabularies,
        authorityName = authorityRecordTypeConfig.name,
        authorityServiceConfig = authorityRecordTypeConfig.serviceConfig;
    var authorityServicePath = authorityServiceConfig.servicePath;
    var vocabularyServicePath = vocabularies[vocabularyName].serviceConfig.servicePath;


    dispatch({
      type: PARTIAL_TERM_SEARCH_STARTED,
      meta: {
        partialTerm: partialTerm,
        authorityName: authorityName,
        vocabularyName: vocabularyName
      }
    });

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

    return (0, _cspace2.default)().read(authorityServicePath + '/' + vocabularyServicePath + '/items', config).then(function (response) {
      return dispatch({
        type: PARTIAL_TERM_SEARCH_FULFILLED,
        payload: response,
        meta: {
          partialTerm: partialTerm,
          authorityName: authorityName,
          vocabularyName: vocabularyName
        }
      });
    }).catch(function (error) {
      return dispatch({
        type: PARTIAL_TERM_SEARCH_REJECTED,
        payload: error,
        meta: {
          partialTerm: partialTerm,
          authorityName: authorityName,
          vocabularyName: vocabularyName
        }
      });
    });
  };
};

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