"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isRolesReadPending = exports.isPermsReadPending = exports.getRoles = exports.getResourceNames = exports.default = void 0;
var _immutable = _interopRequireDefault(require("immutable"));
var _get = _interopRequireDefault(require("lodash/get"));
var _configHelpers = require("../helpers/configHelpers");
var _actionCodes = require("../constants/actionCodes");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const handlePermsReadFulfilled = (state, action) => {
  const {
    config
  } = action.meta;
  let perms = (0, _get.default)(action.payload.data, ['ns2:permissions_list', 'permission']);
  if (perms && !Array.isArray(perms)) {
    perms = [perms];
  }
  const resourceNames = perms.filter(perm => (0, _configHelpers.getRecordTypeConfigByServicePath)(config, perm.resourceName)).map(perm => perm.resourceName);
  return state.set('resourceNames', _immutable.default.List(resourceNames)).delete('isPermsReadPending');
};
const handleRolesReadFulfilled = (state, action) => {
  let roles = (0, _get.default)(action.payload.data, ['ns2:roles_list', 'role']);
  if (roles && !Array.isArray(roles)) {
    roles = [roles];
  }
  return state.set('roles', _immutable.default.fromJS(roles)).delete('isRolesReadPending');
};
const handleRecordCreated = (state, action) => {
  if (action.meta.recordTypeConfig.name === 'authrole') {
    return state.delete('roles');
  }
  return state;
};
const handleRecordDeleteFulfilled = (state, action) => {
  if (action.meta.recordTypeConfig.name === 'authrole') {
    return state.delete('roles');
  }
  return state;
};
var _default = function _default() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _immutable.default.Map();
  let action = arguments.length > 1 ? arguments[1] : undefined;
  switch (action.type) {
    case _actionCodes.PERMS_READ_STARTED:
      return state.set('isPermsReadPending', true);
    case _actionCodes.PERMS_READ_FULFILLED:
      return handlePermsReadFulfilled(state, action);
    case _actionCodes.PERMS_READ_REJECTED:
      return state.delete('isPermsReadPending');
    case _actionCodes.ROLES_READ_STARTED:
      return state.set('isRolesReadPending', true);
    case _actionCodes.ROLES_READ_FULFILLED:
      return handleRolesReadFulfilled(state, action);
    case _actionCodes.ROLES_READ_REJECTED:
      return state.delete('isRolesReadPending');
    case _actionCodes.RECORD_CREATED:
      return handleRecordCreated(state, action);
    case _actionCodes.RECORD_DELETE_FULFILLED:
      return handleRecordDeleteFulfilled(state, action);
    default:
      return state;
  }
};
exports.default = _default;
const isPermsReadPending = state => state.get('isPermsReadPending');
exports.isPermsReadPending = isPermsReadPending;
const getResourceNames = state => state.get('resourceNames');
exports.getResourceNames = getResourceNames;
const isRolesReadPending = state => state.get('isRolesReadPending');
exports.isRolesReadPending = isRolesReadPending;
const getRoles = state => state.get('roles');
exports.getRoles = getRoles;