"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.readRoles = exports.readPerms = void 0;

var _session = _interopRequireDefault(require("../helpers/session"));

var _errorCodes = require("../constants/errorCodes");

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

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

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

const readPerms = config => (dispatch, getState) => {
  if ((0, _reducers.isAuthzPermsReadPending)(getState()) || (0, _reducers.getAuthzResourceNames)(getState())) {
    // We've already read the perms, or a request is already pending. Do nothing.
    return Promise.resolve();
  }

  const requestConfig = {
    params: {
      pgSz: 0,
      actGrp: 'CRUDL'
    }
  };
  dispatch({
    type: _actionCodes.PERMS_READ_STARTED
  });
  return (0, _session.default)().read('authorization/permissions', requestConfig).then(response => {
    dispatch({
      type: _actionCodes.PERMS_READ_FULFILLED,
      payload: response,
      meta: {
        config
      }
    });
  }).catch(error => {
    dispatch({
      type: _actionCodes.PERMS_READ_REJECTED,
      payload: {
        code: _errorCodes.ERR_API,
        error
      }
    });
  });
};

exports.readPerms = readPerms;

const readRoles = () => (dispatch, getState) => {
  if ((0, _reducers.isAuthzRolesReadPending)(getState()) || (0, _reducers.getAuthzRoles)(getState())) {
    // We've already read the perms, or a request is already pending. Do nothing.
    return Promise.resolve();
  }

  const requestConfig = {
    params: {
      pgSz: 0
    }
  };
  dispatch({
    type: _actionCodes.ROLES_READ_STARTED
  });
  return (0, _session.default)().read('authorization/roles', requestConfig).then(response => {
    dispatch({
      type: _actionCodes.ROLES_READ_FULFILLED,
      payload: response
    });
  }).catch(error => {
    dispatch({
      type: _actionCodes.ROLES_READ_REJECTED,
      payload: {
        code: _errorCodes.ERR_API,
        error
      }
    });
  });
};

exports.readRoles = readRoles;