"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.privilegeNames = exports.canRespond = exports.filterByPid = exports.privilegesListHuman = exports.privilegesGroupsList = exports.privilegesList = exports.filterUidsByPid = exports.canPostMandatoryEvent = exports.canPostEvent = exports.canViewPost = void 0;

var _settings = require("./settings");

const privileges = require.main.require('./src/privileges');

const {
  getCidByPid
} = require.main.require('./src/posts');

const privilegesPostCan = privileges.posts.can;
const privilegesTopicCan = privileges.topics.can;
const filterUidsByCid = privileges.categories.filterUids;
const filterPids = privileges.posts.filter;
const privilegeNames = {
  canPost: 'plugin_calendar:event:post',
  canMandatoryPost: 'plugin_calendar:event:mandatory'
};
exports.privilegeNames = privilegeNames;

const canViewPost = (pid, uid) => privilegesPostCan('read', pid, uid);

exports.canViewPost = canViewPost;

const canPostEvent = (tid, uid) => privilegesTopicCan(privilegeNames.canPost, tid, uid);

exports.canPostEvent = canPostEvent;

const canPostMandatoryEvent = (tid, uid) => privilegesTopicCan(privilegeNames.canMandatoryPost, tid, uid);

exports.canPostMandatoryEvent = canPostMandatoryEvent;

const canRespond = (pid, uid) => (0, _settings.getSetting)('respondIfCanReply').then(respondIfCanReply => {
  if (respondIfCanReply) {
    return privilegesPostCan('topics:reply', pid, uid);
  }

  return canViewPost(pid, uid);
});

exports.canRespond = canRespond;

const filterUidsByPid = (uids, pid) => getCidByPid(pid).then(cid => filterUidsByCid('read', cid, uids));

exports.filterUidsByPid = filterUidsByPid;

const filterByPid = (events, uid) => filterPids('read', events.map(e => e.pid), uid).then(filtered => events.filter(e => filtered.includes(e.pid)));

exports.filterByPid = filterByPid;

const privilegesList = (list, callback) => callback(null, [...list, ...Object.values(privilegeNames)]);

exports.privilegesList = privilegesList;

const privilegesGroupsList = (list, callback) => callback(null, [...list, ...Object.values(privilegeNames).map(name => `groups:${name}`)]);

exports.privilegesGroupsList = privilegesGroupsList;

const privilegesListHuman = (list, callback) => callback(null, [...list, {
  name: 'Post events'
}, {
  name: 'Post mandatory events'
}]);

exports.privilegesListHuman = privilegesListHuman;
//# sourceMappingURL=privileges.js.map