'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getError = exports.getResponse = exports.getUsername = exports.isPending = undefined;

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _login = require('../actions/login');

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

exports.default = function () {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _immutable2.default.Map();
  var action = arguments[1];

  switch (action.type) {
    case _login.RESET_LOGIN:
      return state.clear();
    case _login.LOGIN_STARTED:
      return state.set('isPending', true).set('username', action.meta.username).delete('response').delete('error');
    case _login.LOGIN_FULFILLED:
      return state.delete('isPending').set('username', action.meta.username).set('response', _immutable2.default.fromJS(action.payload)).delete('error');
    case _login.LOGIN_REJECTED:
      return state.delete('isPending').set('username', action.meta.username).delete('response').set('error', _immutable2.default.fromJS(action.payload));
    case _cspace.CSPACE_CONFIGURED:
      return state.set('username', action.payload.username);
    default:
      return state;
  }
};

var isPending = exports.isPending = function isPending(state) {
  return state.get('isPending');
};
var getUsername = exports.getUsername = function getUsername(state) {
  return state.get('username');
};
var getResponse = exports.getResponse = function getResponse(state) {
  return state.get('response');
};
var getError = exports.getError = function getError(state) {
  return state.get('error');
};