'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getRoles = exports.isRolesReadPending = exports.getResourceNames = exports.isPermsReadPending = undefined;

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _configHelpers = require('../helpers/configHelpers');

var _authz = require('../actions/authz');

var _record = require('../actions/record');

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

var handlePermsReadFulfilled = function handlePermsReadFulfilled(state, action) {
  var config = action.meta.config;


  var perms = (0, _get2.default)(action.payload.data, ['ns2:permissions_list', 'permission']);

  if (perms && !Array.isArray(perms)) {
    perms = [perms];
  }

  var resourceNames = perms.filter(function (perm) {
    return (0, _configHelpers.getRecordTypeConfigByServicePath)(config, perm.resourceName);
  }).map(function (perm) {
    return perm.resourceName;
  });

  return state.set('resourceNames', _immutable2.default.List(resourceNames)).delete('isPermsReadPending');
};

var handleRolesReadFulfilled = function handleRolesReadFulfilled(state, action) {
  var roles = (0, _get2.default)(action.payload.data, ['ns2:roles_list', 'role']);

  if (roles && !Array.isArray(roles)) {
    roles = [roles];
  }

  return state.set('roles', _immutable2.default.fromJS(roles)).delete('isRolesReadPending');
};

var handleRecordCreated = function handleRecordCreated(state, action) {
  if (action.meta.recordTypeConfig.name === 'authrole') {
    return state.delete('roles');
  }

  return state;
};

var handleRecordDeleteFulfilled = function handleRecordDeleteFulfilled(state, action) {
  if (action.meta.recordTypeConfig.name === 'authrole') {
    return state.delete('roles');
  }

  return state;
};

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

  switch (action.type) {
    case _authz.PERMS_READ_STARTED:
      return state.set('isPermsReadPending', true);
    case _authz.PERMS_READ_FULFILLED:
      return handlePermsReadFulfilled(state, action);
    case _authz.PERMS_READ_REJECTED:
      return state.delete('isPermsReadPending');
    case _authz.ROLES_READ_STARTED:
      return state.set('isRolesReadPending', true);
    case _authz.ROLES_READ_FULFILLED:
      return handleRolesReadFulfilled(state, action);
    case _authz.ROLES_READ_REJECTED:
      return state.delete('isRolesReadPending');
    case _record.RECORD_CREATED:
      return handleRecordCreated(state, action);
    case _record.RECORD_DELETE_FULFILLED:
      return handleRecordDeleteFulfilled(state, action);
    default:
      return state;
  }
};

var isPermsReadPending = exports.isPermsReadPending = function isPermsReadPending(state) {
  return state.get('isPermsReadPending');
};
var getResourceNames = exports.getResourceNames = function getResourceNames(state) {
  return state.get('resourceNames');
};

var isRolesReadPending = exports.isRolesReadPending = function isRolesReadPending(state) {
  return state.get('isRolesReadPending');
};
var getRoles = exports.getRoles = function getRoles(state) {
  return state.get('roles');
};