"use strict";

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

var _moment = _interopRequireDefault(require("moment"));

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 dayMS = 24 * 60 * 60 * 1000; // get occurences of `event` over the interval from `start` to `end`

function getOccurencesOfRepetition(event, start, end) {
  const startDate = new Date(event.startDate);
  const {
    endDate,
    every
  } = event.repeats;

  if (every.day) {
    every.numOfDays = [1];
  } else if (every.week) {
    every.daysOfWeek = [startDate.getDay()];
  } else if (every.month) {
    every.daysOfMonth = [startDate.getDate()];
  }

  const duration = event.endDate - event.startDate;
  let dates = [];

  if (every.numOfDays) {
    dates = Array.prototype.concat.apply([], every.numOfDays.map(num => {
      // first occurrence after start
      const current = new Date(startDate.valueOf());

      if (current < start) {
        const s = new Date(start);
        current.setFullYear(s.getFullYear());
        current.setMonth(s.getMonth());
        current.setDate(s.getDate());

        if (current < start) {
          current.setDate(current.getDate() + 1);
        }

        const mod = (current - startDate) % num * dayMS;

        if (mod !== 0) {
          current.setDate((current.getDate() + num - mod) / dayMS);
        }
      }

      const out = [];

      while (current < end && (!endDate || current < endDate)) {
        out.push(current.valueOf());
        current.setDate(current.getDate() + num);
      }

      return out;
    }));
  } else if (every.daysOfWeek) {
    dates = Array.prototype.concat.apply([], every.daysOfWeek.map(day => {
      // first occurrence after start
      const current = new Date(startDate.valueOf());

      if (current < start) {
        const s = new Date(start);
        current.setFullYear(s.getFullYear());
        current.setMonth(s.getMonth());
        current.setDate(s.getDate());

        if (current < start) {
          current.setDate(current.getDate() + 1);
        }
      }

      if (current.getDay() !== day) {
        let add = day - current.getDay();

        if (add < 0) {
          add += 7;
        }

        current.setDate(current.getDate() + add);
      }

      const out = [];

      while (current < end && (!endDate || current < endDate)) {
        out.push(current.valueOf());
        current.setDate(current.getDate() + 7);
      }

      return out;
    }));
  } else if (every.daysOfMonth) {
    dates = Array.prototype.concat.apply([], every.daysOfMonth.map(date => {
      // first occurrence after start
      const current = new Date(startDate.valueOf());

      if (current < start) {
        const s = new Date(start);
        current.setFullYear(s.getFullYear());
        current.setMonth(s.getMonth());

        if (current < start) {
          current.setMonth(current.getMonth() + 1);
        }
      }

      current.setDate(date);
      const out = [];

      while (current < end && (!endDate || current < endDate)) {
        out.push(current.valueOf());
        current.setMonth(current.getMonth() + 1);
      }

      return out;
    }));
  } else if (every.year) {
    // first occurrence after start
    const current = new Date(startDate.valueOf());

    if (current < start) {
      const s = new Date(start);
      current.setFullYear(s.getFullYear());

      if (current < start) {
        current.setFullYear(current.getFullYear() + 1);
      }
    }

    const out = [];

    while (current < end && (!endDate || current < endDate)) {
      out.push(current.valueOf());
      current.setFullYear(current.getFullYear() + 1);
    }

    dates = out;
  }

  const occurences = dates.map(date => _objectSpread({}, event, {
    startDate: date,
    endDate: date + duration,
    day: _moment.default.utc(date).format('YYYY-MM-DD')
  }));
  return occurences;
}
//# sourceMappingURL=repetition.js.map