'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.login = exports.readAccountPerms = exports.resetLogin = exports.LOGIN_REJECTED = exports.LOGIN_FULFILLED = exports.LOGIN_STARTED = exports.RESET_LOGIN = exports.ACCOUNT_PERMS_READ_REJECTED = exports.ACCOUNT_PERMS_READ_FULFILLED = exports.AUTH_RENEW_REJECTED = exports.AUTH_RENEW_FULFILLED = undefined;

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

var _cspace2 = _interopRequireDefault(_cspace);

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

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

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

var AUTH_RENEW_FULFILLED = exports.AUTH_RENEW_FULFILLED = 'AUTH_RENEW_FULFILLED';
var AUTH_RENEW_REJECTED = exports.AUTH_RENEW_REJECTED = 'AUTH_RENEW_REJECTED';
var ACCOUNT_PERMS_READ_FULFILLED = exports.ACCOUNT_PERMS_READ_FULFILLED = 'ACCOUNT_PERMS_READ_FULFILLED';
var ACCOUNT_PERMS_READ_REJECTED = exports.ACCOUNT_PERMS_READ_REJECTED = 'ACCOUNT_PERMS_READ_REJECTED';
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 renewAuth = function renewAuth(username, password) {
  return function (dispatch) {
    dispatch((0, _cspace.createSession)(username, password));

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

      throw error;
    });
  };
};

var readAccountPerms = exports.readAccountPerms = function readAccountPerms(config) {
  return function (dispatch, getState) {
    var username = (0, _reducers.getUserUsername)(getState());

    if (!username) {
      return Promise.resolve();
    }

    return (0, _cspace2.default)().read('accounts/0/accountperms').then(function (response) {
      return dispatch({
        type: ACCOUNT_PERMS_READ_FULFILLED,
        payload: response,
        meta: {
          config: config
        }
      });
    }).catch(function (error) {
      dispatch({
        type: ACCOUNT_PERMS_READ_REJECTED,
        payload: error
      });

      throw error;
    });
  };
};

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

    return dispatch(renewAuth(username, password)).then(function () {
      return dispatch(readAccountPerms(config));
    }).then(function () {
      return dispatch((0, _prefs.loadPrefs)());
    }).then(function () {
      return dispatch({
        type: LOGIN_FULFILLED,
        meta: {
          username: username
        }
      });
    }).catch(function (error) {
      return dispatch({
        type: LOGIN_REJECTED,
        payload: error,
        meta: {
          username: username
        }
      });
    });
  };
};