"use strict";

const express = require("express"),
  moment = require("moment-holiday");

require("../lib/pascha.js")(moment);

const router = express.Router();

/* Remove the western Easter dates, except for Easter itself */
[ 'Good Friday' ].forEach(function(holiday) {
  moment.modifyHolidays.remove(holiday);
});

router.get("/", function(req, res/*, next*/) {
  let holidays = [], skip = {};


  moment("1999-12-31", "YYYY-MM-DD").holidaysBetween("2001-01-01").forEach(function(holiday) {
    /* Dates with multiple holidays will return an array of items */
    let names = holiday.isHoliday();
    if (!Array.isArray(names)) {
      names = [ names ];
    }
    names.forEach(function(name) {
      if (name in skip) {
        return;
      }

      /* If this holiday already exists, remove it from the holidays list
       * as we only want single day events returned, and add to the 'skip'
       * list */
      let index = holidays.indexOf(name);
      if (index != -1)  {
        holidays.splice(index, 1);
        skip[name] = true;
      } else {
        holidays.push(name);
      }
    });
  });

  return res.status(200).send({
    holidays: holidays,
    next: moment().nextHoliday().isHoliday()
  });
});

module.exports = router;
