"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _util = require("util");

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

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

var _templates = _interopRequireDefault(require("./templates"));

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

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

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

const {
  filterCids
} = require.main.require('./src/privileges').categories;

const {
  getAllCategoryFields
} = require.main.require('./src/categories');
/* eslint-disable */


function shadeColor2(color, percent) {
  var f = parseInt(color.slice(1), 16),
      t = percent < 0 ? 0 : 255,
      p = percent < 0 ? percent * -1 : percent,
      R = f >> 16,
      G = f >> 8 & 0x00FF,
      B = f & 0x0000FF;
  return "#" + (0x1000000 + (Math.round((t - R) * p) + R) * 0x10000 + (Math.round((t - G) * p) + G) * 0x100 + (Math.round((t - B) * p) + B)).toString(16).slice(1);
}
/* eslint-enable */


var _default = (router, middleware) => {
  const renderAdmin = (req, res, next) => {
    (0, _settings.getSettings)().then(settings => {
      res.render('admin/plugins/calendar', {
        settings
      });
    }).catch(next);
  };

  router.get('/admin/plugins/calendar', middleware.admin.buildHeader, renderAdmin);
  router.get('/api/admin/plugins/calendar', renderAdmin);
  router.get('/api/admin/plugins/calendar/save', (req, res, next) => {
    Promise.resolve().then(() => (0, _settings.setSettings)(JSON.parse(req.query.settings))).then(() => {
      res.sendStatus(200);
    }).catch(next);
  });
  const renderPageCb = (0, _util.callbackify)(async ({
    uid,
    params
  }) => {
    const [cats, calendarViews] = await Promise.all([getAllCategoryFields(['cid', 'bgColor']), (0, _settings.getSetting)('calendarViews')]);
    const filtered = new Set((await filterCids('read', cats.map(c => c.cid), uid)));
    const colors = cats.filter(c => filtered.has(c.cid));
    const style = colors.map(({
      cid,
      bgColor
    }) => `
      .plugin-calendar-cal-event-category-${cid} {
      background-color: ${bgColor};
      border-color: ${shadeColor2(bgColor, -0.2)};
    }`);
    const {
      eventPid: pid,
      eventDay: day
    } = params;

    if (!pid || !(await (0, _privileges.canViewPost)(pid, uid))) {
      return {
        calendarEventsStyle: style.join('\n'),
        title: '[[calendar:calendar]]',
        eventJSON: 'null',
        calendarViews
      };
    }

    const raw = await (0, _event.getEvent)(pid);
    const [event, userResponse] = await Promise.all([(0, _event.escapeEvent)(raw), (0, _responses.getUserResponse)({
      pid,
      day,
      uid
    })]);
    event.day = day || null;

    if (event.repeats && event.day) {
      const {
        startDate,
        endDate
      } = event;
      const occurenceDate = new Date(day);
      const s = new Date(startDate);
      s.setUTCFullYear(occurenceDate.getUTCFullYear());
      s.setUTCDate(occurenceDate.getUTCDate());
      s.setUTCMonth(occurenceDate.getUTCMonth());
      event.startDate = s.valueOf();
      event.endDate = event.startDate + (endDate - startDate);
    }

    event.responses = {
      [uid]: userResponse
    };
    return {
      calendarEventsStyle: style.join('\n'),
      title: '[[calendar:calendar]]',
      eventData: event,
      eventJSON: JSON.stringify(event),
      eventHTML: await (0, _templates.default)({
        event,
        uid
      }),
      calendarViews
    };
  });

  const renderPage = (req, res, next) => {
    const cb = (err, data) => {
      if (err) {
        next(err);
        return;
      }

      res.render('calendar', data);
    };

    renderPageCb(req, res, cb);
  };

  router.get('/calendar/event/:eventPid/:eventDay', middleware.buildHeader, renderPage);
  router.get('/api/calendar/event/:eventPid/:eventDay', renderPage);
  router.get('/calendar/event/:eventPid', middleware.buildHeader, renderPage);
  router.get('/api/calendar/event/:eventPid', renderPage);
  router.get('/calendar', middleware.buildHeader, renderPage);
  router.get('/api/calendar', renderPage);
};

exports.default = _default;
//# sourceMappingURL=controllers.js.map