'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createID = exports.readIDGenerator = exports.addIDGenerators = exports.CREATE_ID_REJECTED = exports.CREATE_ID_FULFILLED = exports.CREATE_ID_STARTED = exports.READ_ID_GENERATOR_REJECTED = exports.READ_ID_GENERATOR_FULFILLED = exports.READ_ID_GENERATOR_STARTED = exports.ADD_ID_GENERATORS = undefined;

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

var _cspace2 = _interopRequireDefault(_cspace);

var _record = require('./record');

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

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

const ADD_ID_GENERATORS = exports.ADD_ID_GENERATORS = 'ADD_ID_GENERATORS';
const READ_ID_GENERATOR_STARTED = exports.READ_ID_GENERATOR_STARTED = 'READ_ID_GENERATOR_STARTED';
const READ_ID_GENERATOR_FULFILLED = exports.READ_ID_GENERATOR_FULFILLED = 'READ_ID_GENERATOR_FULFILLED';
const READ_ID_GENERATOR_REJECTED = exports.READ_ID_GENERATOR_REJECTED = 'READ_ID_GENERATOR_REJECTED';
const CREATE_ID_STARTED = exports.CREATE_ID_STARTED = 'CREATE_ID_STARTED';
const CREATE_ID_FULFILLED = exports.CREATE_ID_FULFILLED = 'CREATE_ID_FULFILLED';
const CREATE_ID_REJECTED = exports.CREATE_ID_REJECTED = 'CREATE_ID_REJECTED';

const addIDGenerators = exports.addIDGenerators = idGenerators => ({
  type: ADD_ID_GENERATORS,
  payload: idGenerators
});

const readIDGenerator = exports.readIDGenerator = idGeneratorName => (dispatch, getState) => {
  let idGeneratorCsid = null;

  const idGenerator = (0, _reducers.getIDGenerator)(getState(), idGeneratorName);

  if (idGenerator) {
    idGeneratorCsid = idGenerator.get('csid');
  }

  if (!idGeneratorCsid) {
    return null;
  }

  dispatch({
    type: READ_ID_GENERATOR_STARTED,
    meta: {
      idGeneratorName
    }
  });

  const config = {
    params: {
      wf_deleted: false
    }
  };

  return (0, _cspace2.default)().read(`idgenerators/${idGeneratorCsid}`, config).then(response => dispatch({
    type: READ_ID_GENERATOR_FULFILLED,
    payload: response,
    meta: {
      idGeneratorName
    }
  })).catch(error => dispatch({
    type: READ_ID_GENERATOR_REJECTED,
    payload: error,
    meta: {
      idGeneratorName
    }
  }));
};

const createID = exports.createID = (recordTypeConfig, idGeneratorName, csid, path) => (dispatch, getState) => {
  let idGeneratorCsid = null;

  const idGenerator = (0, _reducers.getIDGenerator)(getState(), idGeneratorName);

  if (idGenerator) {
    idGeneratorCsid = idGenerator.get('csid');
  }

  if (!idGeneratorCsid) {
    return null;
  }

  dispatch({
    type: CREATE_ID_STARTED,
    meta: {
      recordTypeConfig,
      idGeneratorName,
      csid,
      path
    }
  });

  return (0, _cspace2.default)().create(`idgenerators/${idGeneratorCsid}/ids`).then(response => {
    dispatch({
      type: CREATE_ID_FULFILLED,
      payload: response,
      meta: {
        recordTypeConfig,
        idGeneratorName,
        csid,
        path
      }
    });

    return dispatch((0, _record.computeRecordData)(recordTypeConfig, csid)).then(() => dispatch((0, _record.validateRecordData)(recordTypeConfig, csid)));
  }).catch(error => dispatch({
    type: CREATE_ID_REJECTED,
    payload: error,
    meta: {
      recordTypeConfig,
      idGeneratorName,
      csid,
      path
    }
  }));
};