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

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

var addIDGenerators = exports.addIDGenerators = function addIDGenerators(idGenerators) {
  return {
    type: ADD_ID_GENERATORS,
    payload: idGenerators
  };
};

var readIDGenerator = exports.readIDGenerator = function readIDGenerator(idGeneratorName) {
  return function (dispatch, getState) {
    var idGeneratorCsid = null;

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

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

    if (!idGeneratorCsid) {
      return null;
    }

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

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

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

var createID = exports.createID = function createID(recordTypeConfig, idGeneratorName, csid, path) {
  return function (dispatch, getState) {
    var idGeneratorCsid = null;

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

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

    if (!idGeneratorCsid) {
      return null;
    }

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

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

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