'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.login = exports.resetLogin = exports.redirectLogin = exports.LOGIN_REJECTED = exports.LOGIN_FULFILLED = exports.LOGIN_STARTED = exports.RESET_LOGIN = exports.LOGIN_REDIRECTED = undefined;

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

var _cspace2 = _interopRequireDefault(_cspace);

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

var LOGIN_REDIRECTED = exports.LOGIN_REDIRECTED = 'LOGIN_REDIRECTED';
var RESET_LOGIN = exports.RESET_LOGIN = 'RESET_LOGIN';
var LOGIN_STARTED = exports.LOGIN_STARTED = 'LOGIN_STARTED';
var LOGIN_FULFILLED = exports.LOGIN_FULFILLED = 'LOGIN_FULFILLED';
var LOGIN_REJECTED = exports.LOGIN_REJECTED = 'LOGIN_REJECTED';

var redirectLogin = exports.redirectLogin = function redirectLogin(replace, attemptedUrl) {
  replace('/login');

  return {
    type: LOGIN_REDIRECTED,
    meta: {
      attemptedUrl: attemptedUrl
    }
  };
};

var resetLogin = exports.resetLogin = function resetLogin() {
  return {
    type: RESET_LOGIN
  };
};

var login = exports.login = function login(username, password) {
  return function (dispatch) {
    dispatch({
      type: LOGIN_STARTED,
      meta: {
        username: username
      }
    });

    (0, _cspace.createSession)(username, password);

    return (0, _cspace2.default)().login().then(function (response) {
      return dispatch({
        type: LOGIN_FULFILLED,
        payload: response,
        meta: {
          username: username
        }
      });
    }).catch(function (error) {
      return dispatch({
        type: LOGIN_REJECTED,
        payload: error,
        meta: {
          username: username
        }
      });
    });
  };
};