import { toConsumableArray as _toConsumableArray, slicedToArray as _slicedToArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { useMemo } from 'react';
import { CalendarState } from '../store/types.js';
import { getDaysInMonth, getOffsetDayInWeek, getMatrix, IsCurrentDay, isSelectedDay, getPrevDate, getNextDate } from '../utils/calendarGridHelper.js';
import { getDatesWithModifications } from '../utils/getDateWithModification.js';
import { isDateInRange } from '../utils/calendarRangeHelper.js';

/**
 * Метод возвращающий массив дней в предыдущем месяце.
 */
var getDaysInPrevMonth = function getDaysInPrevMonth(date, offsetDayInWeek, value) {
  var _getPrevDate = getPrevDate(date.year, date.monthIndex),
    _getPrevDate2 = _slicedToArray(_getPrevDate, 2),
    prevYear = _getPrevDate2[0],
    prevMonth = _getPrevDate2[1];
  var daysInPrevMonth = getDaysInMonth(prevMonth, prevYear);
  return Array.from(Array(offsetDayInWeek), function (_, i) {
    return {
      isCurrent: false,
      isSelected: false,
      isDayInCurrentMonth: false,
      inRange: Array.isArray(value) ? isDateInRange(prevYear, prevMonth, daysInPrevMonth - (offsetDayInWeek - i) + 1, value) : false,
      date: {
        day: daysInPrevMonth - (offsetDayInWeek - i) + 1,
        monthIndex: prevMonth,
        year: prevYear
      }
    };
  });
};

/**
 * Метод возвращающий массив дней в текущем месяце.
 */
var getDaysInCurrentMonth = function getDaysInCurrentMonth(date, daysInMonth, value) {
  return Array.from(Array(daysInMonth), function (_, i) {
    return {
      isCurrent: IsCurrentDay(date, i + 1),
      isSelected: Array.isArray(value) ? Boolean(value.find(function (v) {
        return isSelectedDay(date, i + 1, v);
      })) : isSelectedDay(date, i + 1, value),
      isDayInCurrentMonth: true,
      inRange: Array.isArray(value) ? isDateInRange(date.year, date.monthIndex, i + 1, value) : false,
      date: {
        day: i + 1,
        monthIndex: date.monthIndex,
        year: date.year
      }
    };
  });
};

/**
 * Метод возвращающий массив дней в следующем месяце.
 */
var getDaysInNextMonth = function getDaysInNextMonth(date, daysInMonth, offsetDayInWeek, value) {
  var _getNextDate = getNextDate(date.year, date.monthIndex),
    _getNextDate2 = _slicedToArray(_getNextDate, 2),
    nextYear = _getNextDate2[0],
    nextMonthIndex = _getNextDate2[1];
  var visibleDayCount = 42;
  var restDaysInCalendar = visibleDayCount - (daysInMonth + offsetDayInWeek);
  return Array.from(Array(restDaysInCalendar), function (_, i) {
    return {
      isCurrent: false,
      isSelected: false,
      isDayInCurrentMonth: false,
      inRange: Array.isArray(value) ? isDateInRange(nextYear, nextMonthIndex, i + 1, value) : false,
      date: {
        day: i + 1,
        monthIndex: nextMonthIndex,
        year: nextYear
      }
    };
  });
};

/**
 * Хук для получения списка дней.
 */
var useDays = function useDays(_ref) {
  var date = _ref.date,
    value = _ref.value,
    eventList = _ref.eventList,
    disabledList = _ref.disabledList,
    min = _ref.min,
    max = _ref.max,
    includeEdgeDates = _ref.includeEdgeDates;
  return useMemo(function () {
    var monthIndex = date.monthIndex,
      year = date.year;
    var daysInMonth = getDaysInMonth(monthIndex, year);
    var offsetDayInWeek = getOffsetDayInWeek(monthIndex, year);
    var days = [].concat(_toConsumableArray(getDaysInPrevMonth(date, offsetDayInWeek, value)), _toConsumableArray(getDaysInCurrentMonth(date, daysInMonth, value)), _toConsumableArray(getDaysInNextMonth(date, daysInMonth, offsetDayInWeek, value)));
    if (eventList !== null && eventList !== void 0 && eventList.length || disabledList !== null && disabledList !== void 0 && disabledList.length || max || min) {
      var modifiedDays = getDatesWithModifications({
        dates: days,
        type: CalendarState.Days,
        includeEdgeDates: includeEdgeDates,
        min: min,
        max: max,
        eventList: eventList,
        disabledList: disabledList
      });
      return getMatrix(modifiedDays);
    }
    return getMatrix(days);
  }, [date, value, eventList, disabledList, max, min, includeEdgeDates]);
};

export { useDays };
