"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useCalendarDays = useCalendarDays;
var _react = require("react");
var _setTimeToNoon = require("../util/setTimeToNoon");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var DAYS_BY_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

/**
  groupByWeeks: {
   year,
   month,
   weeks: [
      {weekNum: N,
      wekDays: [
        {inMonth: true, day: N, mode: ''}
        or
        {inMonth: false}
        ]
      },...
      ]
  }
 */

function _groupByWeeks(year, month, weekStartsOn) {
  if (year == undefined || month == undefined) {
    return undefined;
  }
  var firstDay = new Date(year, month, 1);
  var startingDay = weekStartsOn > 1 ? firstDay.getDay() - weekStartsOn + 7 : weekStartsOn === 1 ? firstDay.getDay() === 0 ? 6 : firstDay.getDay() - 1 : firstDay.getDay();
  var monthLength = DAYS_BY_MONTH[month];
  if (month == 1) {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
      monthLength = 29;
    }
  }
  var isInMonth = (monthDay, weekIndex, weekDay) => {
    if (monthDay <= monthLength && (weekIndex > 0 || weekDay >= startingDay)) {
      return true;
    }
    return false;
  };
  var getWeekNumber = monthDay => {
    var date = new Date(year, month, monthDay - 1, 12, 0, 0, 0);
    var target = new Date(date.valueOf());
    var dayNr = (date.getDay() + 6) % 7;
    target.setDate(target.getDate() - dayNr + 3);
    var firstThursday = target.valueOf();
    target.setMonth(0, 1);
    if (target.getDay() !== 4) {
      target.setMonth(0, 1 + (4 - target.getDay() + 7) % 7);
    }
    return 1 + Math.ceil((firstThursday - target) / 604800000);
  };
  var allWeeks = [];
  var monthDay = 1;
  for (var weekIndex = 0; weekIndex < 9; weekIndex++) {
    var weekDays = [];
    for (var weekDay = 0; weekDay <= 6; weekDay++) {
      if (isInMonth(monthDay, weekIndex, weekDay)) {
        weekDays.push({
          inMonth: true,
          day: monthDay
        });
        monthDay += 1;
      } else {
        weekDays.push({
          inMonth: false
        });
      }
    }
    var weekNum = getWeekNumber(monthDay);
    allWeeks.push({
      weekDays,
      weekNum
    });
    if (monthDay > monthLength) {
      break;
    }
  }
  return {
    year,
    month,
    weeks: allWeeks
  };
}

/**
  calendarDays: [
  {weekNum: N,
   wekDays: [
     {inMonth: true, day: N, mode: ''}
     or
     {inMonth: false}
    ]
  },...
  ]
 */

function _makeCalendarDays(groupByWeeks, selectedDate, minDate, maxDate) {
  if (groupByWeeks == undefined) {
    return [];
  }
  var getDayMode = day => {
    var date = (0, _setTimeToNoon.setTimeToNoon)(new Date(groupByWeeks.year, groupByWeeks.month, day, 12, 0, 0, 0)).toISOString();
    var beforeMinDate = minDate != undefined ? Date.parse(date) < Date.parse((0, _setTimeToNoon.setTimeToNoon)(new Date(minDate))) : false;
    var afterMaxDate = maxDate != undefined ? Date.parse(date) > Date.parse((0, _setTimeToNoon.setTimeToNoon)(new Date(maxDate))) : false;
    var currentDate = (0, _setTimeToNoon.setTimeToNoon)(new Date());
    var nSelectedDate = (0, _setTimeToNoon.setTimeToNoon)(new Date(selectedDate));
    if (beforeMinDate || afterMaxDate) {
      return 'muted';
    } else if (Date.parse(date) === Date.parse(nSelectedDate)) {
      return 'selected';
    } else if (Date.parse(date) === Date.parse(currentDate)) {
      return 'current';
    } else {
      return 'normal';
    }
  };
  var calendarDays = [];
  groupByWeeks.weeks.map(week => {
    var weekNum = week.weekNum;
    var weekDays = week.weekDays.map(weekDay => {
      return _objectSpread(_objectSpread({}, weekDay), {}, {
        mode: weekDay.inMonth ? getDayMode(weekDay.day) : undefined
      });
    });
    calendarDays.push({
      weekNum,
      weekDays
    });
  });
  return calendarDays;
}
function useCalendarDays(displayDate, selectedDate, minDate, maxDate, weekStartsOn) {
  var [year, setYear] = (0, _react.useState)(undefined);
  var [month, setMonth] = (0, _react.useState)(undefined);
  var [groupByWeeks, setGroupByWeeks] = (0, _react.useState)(undefined);
  var [calendarDays, setCalendarDays] = (0, _react.useState)([]);
  (0, _react.useEffect)(() => {
    if (displayDate) {
      setYear(displayDate.getFullYear());
      setMonth(displayDate.getMonth());
    }
  }, [displayDate]);
  (0, _react.useEffect)(() => {
    setGroupByWeeks(_groupByWeeks(year, month, weekStartsOn));
  }, [year, month, weekStartsOn]);
  (0, _react.useEffect)(() => {
    setCalendarDays(_makeCalendarDays(groupByWeeks, selectedDate, minDate, maxDate));
  }, [groupByWeeks, selectedDate, minDate, maxDate]);
  return calendarDays;
}