"use strict";

var _responses = require("./responses");

var _event = require("./event");

var _privileges = require("./privileges");

var _repetition = _interopRequireDefault(require("./repetition"));

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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

const pluginSockets = require.main.require('./src/socket.io/plugins');

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

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

const can = {
  posts: privileges.posts.can,
  topics: privileges.topics.can,
  categories: privileges.categories.can
};

const tidFromPid = (pid, cb) => posts.getPostField(pid, 'tid', cb);

const topicIsDeleted = (tid, cb) => topics.getTopicField(tid, 'deleted', cb);

pluginSockets.calendar = {};

pluginSockets.calendar.canPostEvent = async ({
  uid
}, {
  pid,
  tid,
  cid,
  isMain
}) => {
  const neither = {
    canPost: false,
    canPostMandatory: false
  };

  if (!isMain && (await (0, _settings.getSetting)('mainPostOnly'))) {
    return neither;
  }

  if (!(pid || tid || cid)) {
    return neither;
  }

  let promises;

  if (pid) {
    promises = [can.posts(_privileges.privilegeNames.canPost, pid, uid), can.posts(_privileges.privilegeNames.canMandatoryPost, pid, uid)];
  }

  if (tid) {
    promises = [can.topics(_privileges.privilegeNames.canPost, tid, uid), can.topics(_privileges.privilegeNames.canMandatoryPost, tid, uid)];
  }

  if (cid) {
    promises = [can.categories(_privileges.privilegeNames.canPost, cid, uid), can.categories(_privileges.privilegeNames.canMandatoryPost, cid, uid)];
  }

  const [canPost, canPostMandatory] = await Promise.all(promises);
  return {
    canPost,
    canPostMandatory
  };
};

pluginSockets.calendar.getResponses = async ({
  uid
}, {
  pid,
  day
}) => await (0, _responses.getAll)({
  uid,
  pid,
  day
});

pluginSockets.calendar.submitResponse = async ({
  uid
}, {
  pid,
  value,
  day
} = {}) => await (0, _responses.submitResponse)({
  uid,
  pid,
  value,
  day
});

pluginSockets.calendar.getUserResponse = async ({
  uid
}, {
  pid,
  day
}) => await (0, _responses.getUserResponse)({
  uid,
  pid,
  day
});

pluginSockets.calendar.getEventsByDate = async ({
  uid
}, {
  startDate,
  endDate
}) => {
  const events = await (0, _event.getEventsByDate)(startDate, endDate);
  const filtered = await (0, _privileges.filterByPid)(events, uid);
  const occurences = filtered.reduce((prev, event) => {
    if (event.repeats && event.repeats.every) {
      return [...prev, ...(0, _repetition.default)(event, startDate, endDate)];
    }

    return [...prev, event];
  }, []);
  const withResponses = await Promise.all(occurences.map(async event => {
    const {
      pid,
      day
    } = event;
    const [response, topicDeleted, escaped] = await Promise.all([(0, _responses.getUserResponse)({
      pid,
      uid,
      day
    }).catch(() => null), tidFromPid(pid).then(topicIsDeleted), (0, _event.escapeEvent)(event)]);
    return _objectSpread({}, escaped, {
      responses: {
        [uid]: response
      },
      topicDeleted: !!parseInt(topicDeleted, 10)
    });
  }));
  return withResponses;
};
//# sourceMappingURL=sockets.js.map