"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.clearMatchedTerms = exports.findMatchingTerms = exports.addTerm = void 0;

var _immutable = _interopRequireDefault(require("immutable"));

var _get = _interopRequireDefault(require("lodash/get"));

var _session = _interopRequireDefault(require("../helpers/session"));

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

var _recordDataHelpers = require("../helpers/recordDataHelpers");

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

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

const addTerm = (recordTypeConfig, vocabulary, displayName, partialTerm, clone) => (dispatch, getState) => {
  const recordType = (0, _get.default)(recordTypeConfig, 'name');
  const serviceConfig = (0, _get.default)(recordTypeConfig, 'serviceConfig');
  const servicePath = (0, _get.default)(serviceConfig, 'servicePath');
  const vocabularyServicePath = vocabulary ? (0, _get.default)(recordTypeConfig, ['vocabularies', vocabulary, 'serviceConfig', 'servicePath']) : undefined;
  dispatch({
    type: _actionCodes.ADD_TERM_STARTED,
    meta: {
      displayName,
      partialTerm,
      recordType,
      vocabulary
    }
  });

  let newRecordData = _immutable.default.Map();

  if (clone) {
    const cloneFromCsid = (0, _reducers.getRecordPagePrimaryCsid)(getState());

    if (cloneFromCsid) {
      const cloneFromData = (0, _reducers.getRecordData)(getState(), cloneFromCsid);

      if (cloneFromData) {
        newRecordData = (0, _recordDataHelpers.cloneRecordData)(recordTypeConfig, cloneFromCsid, cloneFromData);
      }
    }
  }

  newRecordData = newRecordData.mergeDeep(serviceConfig.quickAddData({
    displayName
  })); // TODO: This should probably go through the same code path as the record save action.

  const config = {
    data: (0, _recordDataHelpers.prepareForSending)(newRecordData, recordTypeConfig).toJS()
  };
  const itemPath = vocabularyServicePath ? `/${vocabularyServicePath}/items` : '';
  return (0, _session.default)().create(`${servicePath}${itemPath}`, config).then(response => {
    const {
      location
    } = response.headers; // 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, _session.default)().read(path);
  }).then(response => dispatch({
    type: _actionCodes.ADD_TERM_FULFILLED,
    payload: response,
    meta: {
      displayName,
      partialTerm,
      recordType,
      vocabulary
    }
  })).catch(error => dispatch({
    type: _actionCodes.ADD_TERM_REJECTED,
    payload: error,
    meta: {
      displayName,
      partialTerm,
      recordType,
      vocabulary
    }
  }));
};

exports.addTerm = addTerm;

const findMatchingTerms = (recordTypeConfig, vocabulary, partialTerm) => dispatch => {
  const recordType = (0, _get.default)(recordTypeConfig, 'name');
  const servicePath = (0, _get.default)(recordTypeConfig, ['serviceConfig', 'servicePath']);
  const vocabularyServicePath = vocabulary ? (0, _get.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: _actionCodes.PARTIAL_TERM_SEARCH_STARTED,
    meta: {
      partialTerm,
      recordType,
      vocabulary
    }
  });
  const config = {
    params: {
      pt: partialTerm,
      wf_deleted: 'false'
    }
  };
  const itemPath = vocabularyServicePath ? `/${vocabularyServicePath}/items` : '';
  return (0, _session.default)().read(`${servicePath}${itemPath}`, config).then(response => dispatch({
    type: _actionCodes.PARTIAL_TERM_SEARCH_FULFILLED,
    payload: response,
    meta: {
      partialTerm,
      recordType,
      vocabulary
    }
  })).catch(error => dispatch({
    type: _actionCodes.PARTIAL_TERM_SEARCH_REJECTED,
    payload: error,
    meta: {
      partialTerm,
      recordType,
      vocabulary
    }
  }));
};

exports.findMatchingTerms = findMatchingTerms;

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

exports.clearMatchedTerms = clearMatchedTerms;