"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getUserResponse = exports.getAll = exports.removeAll = exports.submitResponse = void 0;

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

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

const {
  setAdd,
  setsRemove,
  deleteAll,
  getSetsMembers,
  isSetMember
} = require.main.require('./src/database');

const {
  getUsersFields
} = require.main.require('./src/user');

const values = ['yes', 'maybe', 'no'];

const submitResponse = async ({
  pid,
  uid,
  value,
  day
}) => {
  if (!values.includes(value)) {
    throw Error('[[error:invalid-data]]');
  }

  if (!uid || !(await (0, _privileges.canRespond)(pid, uid))) {
    throw Error('[[error:no-privileges]]');
  }

  let toAddKey;
  let toRemoveKeys;

  if (day) {
    toAddKey = `${_event.listKey}:pid:${pid}:responses:day:${day}:${value}`;
    toRemoveKeys = values.filter(val => val !== value).map(val => `${_event.listKey}:pid:${pid}:responses:day:${day}:${val}`);
  } else {
    toAddKey = `${_event.listKey}:pid:${pid}:responses:${value}`;
    toRemoveKeys = values.filter(val => val !== value).map(val => `${_event.listKey}:pid:${pid}:responses:${val}`);
  }

  await Promise.all([setsRemove(toRemoveKeys, uid), setAdd(toAddKey, uid), setAdd(`${_event.listKey}:pid:${pid}:responses:lists`, toAddKey)]);
};

exports.submitResponse = submitResponse;

const removeAll = async pid => {
  const lists = await getSetsMembers(`${_event.listKey}:pid:${pid}:responses:lists`);
  const old = values.map(val => `${_event.listKey}:pid:${pid}:responses:${val}`);
  await deleteAll([`${_event.listKey}:pid:${pid}:responses:lists`, ...lists, ...old]);
};

exports.removeAll = removeAll;

const getAll = async ({
  pid,
  uid = 0,
  selection = values,
  day
} = {}) => {
  if (!(await (0, _privileges.canViewPost)(pid, uid))) {
    throw Error('[[error:no-privileges]]');
  }

  let keys;

  if (day) {
    keys = selection.map(val => `${_event.listKey}:pid:${pid}:responses:day:${day}:${val}`);
  } else {
    keys = selection.map(val => `${_event.listKey}:pid:${pid}:responses:${val}`);
  }

  const responseUids = await getSetsMembers(keys);
  const userFields = ['userslug', 'picture', 'username', 'icon:bgColor', 'icon:text'];
  const [yes, maybe, no] = await Promise.all(responseUids.map(uids => getUsersFields(uids, userFields)));
  return {
    yes,
    maybe,
    no
  };
};

exports.getAll = getAll;

const getUserResponse = async ({
  pid,
  uid,
  day
}) => {
  if (!uid || !(await (0, _privileges.canRespond)(pid, uid))) {
    throw Error('[[error:no-privileges]]');
  }

  let keys;

  if (day) {
    keys = values.map(val => `${_event.listKey}:pid:${pid}:responses:day:${day}:${val}`);
  } else {
    keys = values.map(val => `${_event.listKey}:pid:${pid}:responses:${val}`);
  }

  const arr = await Promise.all(keys.map(key => isSetMember(key, uid)));
  return values[arr.findIndex(val => !!val)];
};

exports.getUserResponse = getUserResponse;
//# sourceMappingURL=responses.js.map