'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.disallowSoftDelete = exports.disallowDelete = exports.disallowCreate = exports.canAdmin = exports.canCreateNew = exports.canSoftDelete = exports.canRelate = exports.canList = exports.canDelete = exports.canUpdate = exports.canRead = exports.canCreate = exports.getPermissions = undefined;

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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

var _get2 = _interopRequireDefault(_get);

var _set = require('lodash/set');

var _set2 = _interopRequireDefault(_set);

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

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

var getPermissions = exports.getPermissions = function getPermissions(config, accountPermsData) {
  var perms = {};

  var accountPerms = (0, _get2.default)(accountPermsData, ['ns2:account_permission', 'permission']);

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

    var _canAdmin = false;
    var _canCreateNew = false;

    accountPerms.forEach(function (permission) {
      var actionGroup = permission.actionGroup,
          resourceName = permission.resourceName;


      var resourceNameParts = resourceName.split('/');

      var servicePath = void 0;
      var transitionName = void 0;

      if (resourceNameParts.length === 1) {
        servicePath = resourceNameParts[0];
      } else if (resourceNameParts.length === 2) {
        servicePath = resourceName;
      } else if (resourceNameParts.length === 5 && resourceNameParts[3] === 'workflow') {
        servicePath = resourceNameParts[1];
        transitionName = resourceNameParts[4];
      }

      if (servicePath) {
        var recordTypeConfig = (0, _configHelpers.getRecordTypeConfigByServicePath)(config, servicePath);

        if (recordTypeConfig) {
          var name = recordTypeConfig.name;


          if (transitionName) {
            (0, _set2.default)(perms, [name, 'transition', transitionName], actionGroup);
          } else {
            (0, _set2.default)(perms, [name, 'data'], actionGroup);

            var serviceType = (0, _get2.default)(recordTypeConfig, ['serviceConfig', 'serviceType']);

            // Track if any object, authority, or procedure record can be created. This is used to
            // determine if the Create New navigation item should be shown.

            if (actionGroup.indexOf('C') >= 0 && (serviceType === 'object' || serviceType === 'authority' || serviceType === 'procedure')) {
              _canCreateNew = true;
            }

            // Track if any vocabulary or security record can be created or updated. This is used
            // to determine if the Admin navigation item should be shown.

            if (actionGroup.indexOf('L') >= 0 && (serviceType === 'security' || name === 'vocabulary')) {
              _canAdmin = true;
            }
          }
        }
      }
    });

    perms.canCreateNew = _canCreateNew;
    perms.canAdmin = _canAdmin;
  }

  return _immutable2.default.fromJS(perms);
};

var can = function can(recordType, permissions, actionCode) {
  if (!permissions) {
    return false;
  }
  var actions = permissions.getIn([recordType, 'data']);

  return !!actions && actions.indexOf(actionCode) >= 0;
};

var canTransition = function canTransition(recordType, permissions, transitionName) {
  if (!permissions) {
    return false;
  }

  var actions = permissions.getIn([recordType, 'transition', transitionName]);

  // A transition from the UI is always an update (PUT).

  return !!actions && actions.indexOf('U') >= 0;
};

var canCreate = exports.canCreate = function canCreate(recordType, permissions) {
  return can(recordType, permissions, 'C');
};
var canRead = exports.canRead = function canRead(recordType, permissions) {
  return can(recordType, permissions, 'R');
};
var canUpdate = exports.canUpdate = function canUpdate(recordType, permissions) {
  return can(recordType, permissions, 'U');
};
var canDelete = exports.canDelete = function canDelete(recordType, permissions) {
  return can(recordType, permissions, 'D');
};
var canList = exports.canList = function canList(recordType, permissions) {
  return can(recordType, permissions, 'L');
};

// The UI requires that a record be editable to create a relation with it. (This is not enforced
// in the services layer).
var canRelate = exports.canRelate = function canRelate(recordType, permissions) {
  return can(recordType, permissions, 'U');
};

var canSoftDelete = exports.canSoftDelete = function canSoftDelete(recordType, permissions) {
  return canTransition(recordType, permissions, 'delete');
};

var canCreateNew = exports.canCreateNew = function canCreateNew(permissions) {
  return permissions && !!permissions.get('canCreateNew');
};

var canAdmin = exports.canAdmin = function canAdmin(permissions) {
  return permissions && !!permissions.get('canAdmin');
};

var disallow = function disallow(recordType, permissions, actionCode) {
  if (can(recordType, permissions, actionCode)) {
    var dataPerms = permissions.getIn([recordType, 'data']);

    return permissions.setIn([recordType, 'data'], dataPerms.replace(actionCode, ''));
  }

  return permissions;
};

var disallowTransition = function disallowTransition(recordType, permissions, transitionName) {
  if (canTransition(recordType, permissions, transitionName)) {
    var transitionPerms = permissions.getIn([recordType, 'transition', transitionName]);

    return permissions.setIn([recordType, 'transition', transitionName], transitionPerms.replace('U', ''));
  }

  return permissions;
};

var disallowCreate = exports.disallowCreate = function disallowCreate(recordType, permissions) {
  return disallow(recordType, permissions, 'C');
};
var disallowDelete = exports.disallowDelete = function disallowDelete(recordType, permissions) {
  return disallow(recordType, permissions, 'D');
};

var disallowSoftDelete = exports.disallowSoftDelete = function disallowSoftDelete(recordType, permissions) {
  return disallowTransition(recordType, permissions, 'delete');
};