import { slicedToArray as _slicedToArray, toConsumableArray as _toConsumableArray } from '../../../_virtual/_rollupPluginBabelHelpers.js';
import { isSelectProcess } from './calendarRangeHelper.js';
import { YEAR_RENDER_COUNT, MONTHS, I18N } from './constants.js';

var getDaysInMonth = function getDaysInMonth(monthIndex, year) {
  return new Date(year, monthIndex + 1, 0).getDate();
};
var getOffsetDayInWeek = function getOffsetDayInWeek(monthIndex, year) {
  return (new Date(year, monthIndex).getDay() || 7) - 1;
};
var getStartYear = function getStartYear(year) {
  return Math.floor(year / YEAR_RENDER_COUNT) * YEAR_RENDER_COUNT;
};
var getNextDate = function getNextDate(currentYear, currentMonth) {
  return currentMonth + 1 === MONTHS.length ? [currentYear + 1, 0] : [currentYear, currentMonth + 1];
};
var getPrevDate = function getPrevDate(currentYear, currentMonth) {
  return currentMonth - 1 < 0 ? [Math.abs(currentYear - 1) || 0, 11] : [currentYear, currentMonth - 1];
};
var getDateFromValue = function getDateFromValue(date) {
  var state = date || new Date();
  return {
    day: date !== undefined ? state.getDate() : 0,
    monthIndex: state.getMonth(),
    year: Math.abs(state.getFullYear())
  };
};
var getDateFromNow = function getDateFromNow() {
  var nowDate = new Date();
  return {
    day: nowDate.getDate(),
    monthIndex: nowDate.getMonth(),
    year: nowDate.getFullYear()
  };
};
var IsCurrentDay = function IsCurrentDay(date, currentDay) {
  var _getDateFromNow = getDateFromNow(),
    day = _getDateFromNow.day,
    currentMonthIndex = _getDateFromNow.monthIndex,
    currentYear = _getDateFromNow.year;
  return day === currentDay && date.monthIndex === currentMonthIndex && date.year === currentYear;
};
var isSelectedDay = function isSelectedDay(date, currentDay, value) {
  if (!value) {
    return false;
  }
  var _getDateFromValue = getDateFromValue(value),
    day = _getDateFromValue.day,
    monthIndex = _getDateFromValue.monthIndex,
    year = _getDateFromValue.year;
  return day === currentDay && date.monthIndex === monthIndex && date.year === year;
};
var isCurrentMonth = function isCurrentMonth(date, monthIndex) {
  var _getDateFromNow2 = getDateFromNow(),
    currentMonthIndex = _getDateFromNow2.monthIndex,
    currentYear = _getDateFromNow2.year;
  return monthIndex === currentMonthIndex && date.year === currentYear;
};
var isCurrentQuarter = function isCurrentQuarter(date, monthIndex) {
  var _getDateFromNow3 = getDateFromNow(),
    currentMonthIndex = _getDateFromNow3.monthIndex,
    currentYear = _getDateFromNow3.year;
  var currentQuarterCondition = currentMonthIndex - monthIndex <= 3 && currentMonthIndex - monthIndex > 0;
  return currentQuarterCondition && date.year === currentYear;
};
var isSelectedMonth = function isSelectedMonth(date, currentMonth, value) {
  if (!value) {
    return false;
  }
  var _getDateFromValue2 = getDateFromValue(value),
    monthIndex = _getDateFromValue2.monthIndex,
    year = _getDateFromValue2.year;
  return currentMonth === monthIndex && date.year === year;
};
var isCurrentYear = function isCurrentYear(year) {
  var _getDateFromNow4 = getDateFromNow(),
    currentYear = _getDateFromNow4.year;
  return year === currentYear;
};
var isSelectedYear = function isSelectedYear(yearValue, value) {
  if (!value) {
    return false;
  }
  var _getDateFromValue3 = getDateFromValue(value),
    year = _getDateFromValue3.year;
  return yearValue === year;
};

/**
 * Метод для получения набора неповторяющихся дат.
 */
var getPropsMap = function getPropsMap(props) {
  return props.reduce(function (acc, prop) {
    var _getDateFromValue4 = getDateFromValue(prop.date),
      year = _getDateFromValue4.year,
      monthIndex = _getDateFromValue4.monthIndex,
      day = _getDateFromValue4.day;
    var key = "".concat(year, "-").concat(monthIndex, "-").concat(day);
    var propList = acc.get(key) || [];
    propList.push(prop);
    return acc.set(key, propList);
  }, new Map());
};

/**
 * Метод проверяет, можно ли выбрать день.
 */
var canSelectDate = function canSelectDate(_ref, value, disabledList) {
  var year = _ref.year,
    monthIndex = _ref.monthIndex,
    day = _ref.day;
  if (!isSelectProcess(value)) {
    return true;
  }
  var hoverDate = new Date(year, monthIndex, day);
  var _value = _slicedToArray(value, 1),
    startDate = _value[0];
  if (!(disabledList !== null && disabledList !== void 0 && disabledList.length)) {
    return true;
  }
  var offDisabledRange = disabledList.some(function (_ref2) {
    var date = _ref2.date;
    return startDate < date && date < hoverDate || startDate > date && date > hoverDate;
  });
  return !offDisabledRange;
};

/**
 * Метод для получения двумерного массива и возвращения выбранного элемента.
 */
var getMatrix = function getMatrix(items) {
  var rowSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 7;
  var newItems = _toConsumableArray(items);
  var selected;
  var result = newItems.reduce(function (acc, item, index) {
    if (index % rowSize === 0) {
      acc.push([]);
    }
    acc[acc.length - 1].push(item);
    if (item.isSelected) {
      selected = [acc.length - 1, index % rowSize];
    }
    return acc;
  }, []);
  return [result, selected];
};
var getCalendarType = function getCalendarType(type, locale) {
  switch (type) {
    case 'Months':
    case 'Quarters':
      return I18N.year[locale];
    case 'Years':
      return I18N.period[locale];
    default:
      return I18N.month[locale];
  }
};

export { IsCurrentDay, canSelectDate, getCalendarType, getDateFromNow, getDateFromValue, getDaysInMonth, getMatrix, getNextDate, getOffsetDayInWeek, getPrevDate, getPropsMap, getStartYear, isCurrentMonth, isCurrentQuarter, isCurrentYear, isSelectedDay, isSelectedMonth, isSelectedYear };
