'use strict';

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

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

var _cspace2 = _interopRequireDefault(_cspace);

var _prefs = require('./prefs');

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

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 resetLogin = exports.resetLogin = function resetLogin() {
  return {
    type: RESET_LOGIN
  };
};

var renewAuthToken = exports.renewAuthToken = function renewAuthToken(username, password) {
  return function (dispatch) {};
};

var readAccountInfo = exports.readAccountInfo = function readAccountInfo() {
  return function (dispatch) {};
};

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

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

    var session = (0, _cspace2.default)();

    return session.login().then(function () {
      return session.read('accounts/0/accountperms');
    }).then(function (response) {
      return dispatch({
        type: LOGIN_FULFILLED,
        payload: response,
        meta: {
          username: username
        }
      });
    }).then(function () {
      return dispatch((0, _prefs.loadPrefs)());
    }).catch(function (error) {
      return dispatch({
        type: LOGIN_REJECTED,
        payload: error,
        meta: {
          username: username
        }
      });
    });
  };
};