'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.resetPassword = exports.requestPasswordReset = exports.readAccountPerms = exports.SET_ACCOUNT_PERMS = exports.ACCOUNT_PERMS_READ_REJECTED = exports.ACCOUNT_PERMS_READ_FULFILLED = undefined;

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

var _cspace2 = _interopRequireDefault(_cspace);

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

var ACCOUNT_PERMS_READ_FULFILLED = exports.ACCOUNT_PERMS_READ_FULFILLED = 'ACCOUNT_PERMS_READ_FULFILLED'; /* global btoa */

var ACCOUNT_PERMS_READ_REJECTED = exports.ACCOUNT_PERMS_READ_REJECTED = 'ACCOUNT_PERMS_READ_REJECTED';
var SET_ACCOUNT_PERMS = exports.SET_ACCOUNT_PERMS = 'SET_ACCOUNT_PERMS';

var readAccountPerms = exports.readAccountPerms = function readAccountPerms(config) {
  return function (dispatch) {
    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
      });

      return Promise.reject(error);
    });
  };
};

var requestPasswordReset = exports.requestPasswordReset = function requestPasswordReset(email, tenantId) {
  return function () {
    var config = {
      params: {
        email: email,
        tid: tenantId
      }
    };

    return (0, _cspace2.default)().create('accounts/requestpasswordreset', config);
  };
};

var resetPassword = exports.resetPassword = function resetPassword(password, token) {
  return function () {
    var config = {
      data: {
        'ns2:passwordreset': {
          '@xmlns:ns2': 'http://collectionspace.org/services/authentication',
          token: token,
          password: btoa(password)
        }
      }
    };

    return (0, _cspace2.default)().create('accounts/processpasswordreset', config);
  };
};

exports.default = {};