"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.resetPassword = exports.requestPasswordReset = exports.readAccountPerms = exports.checkForRoleUses = void 0;

var _get = _interopRequireDefault(require("lodash/get"));

var _cspace = _interopRequireDefault(require("./cspace"));

var _actionCodes = require("../constants/actionCodes");

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

/* global btoa */
const checkForRoleUses = csid => () => (0, _cspace.default)().read("authorization/roles/".concat(csid, "/accountroles")).then(response => {
  const account = (0, _get.default)(response, ['data', 'ns2:account_role', 'account']);
  return !!account;
});

exports.checkForRoleUses = checkForRoleUses;

const readAccountPerms = config => dispatch => (0, _cspace.default)().read('accounts/0/accountperms').then(response => dispatch({
  type: _actionCodes.ACCOUNT_PERMS_READ_FULFILLED,
  payload: response,
  meta: {
    config
  }
})).catch(error => {
  dispatch({
    type: _actionCodes.ACCOUNT_PERMS_READ_REJECTED,
    payload: error
  });
  return Promise.reject(error);
});

exports.readAccountPerms = readAccountPerms;

const requestPasswordReset = (email, tenantId) => () => {
  const config = {
    params: {
      email,
      tid: tenantId
    }
  };
  return (0, _cspace.default)().create('accounts/requestpasswordreset', config);
};

exports.requestPasswordReset = requestPasswordReset;

const resetPassword = (password, token) => () => {
  const config = {
    data: {
      'ns2:passwordreset': {
        '@xmlns:ns2': 'http://collectionspace.org/services/authentication',
        token,
        password: btoa(password)
      }
    }
  };
  return (0, _cspace.default)().create('accounts/processpasswordreset', config);
};

exports.resetPassword = resetPassword;
var _default = {};
exports.default = _default;