import dayjs from 'dayjs';
import { CellStatusType } from './types';
export var MAX_MONTH = 11;
export var MIN_MONTH = 0;
export var MAX_DAY = 6;
export var MIN_DAY = 0;
export var NOW = new Date();
export var DEFAULT_MAX_DATE = new Date(NOW.getFullYear() + 20, 11, 31, 23, 59, 59, 999);
export var DEFAULT_MIN_DATE = new Date(2000, 0, 1, 0, 0, 0, 0);
export var getMaxMinDate = function getMaxMinDate(validRange) {
  var minDate = DEFAULT_MIN_DATE;
  var maxDate = DEFAULT_MAX_DATE;

  if (validRange && validRange.length > 0) {
    if (Object.prototype.toString.call(validRange[0]) === '[object Date]') {
      minDate = validRange[0];
    }

    if (Object.prototype.toString.call(validRange[1]) === '[object Date]') {
      maxDate = validRange[1];
    }
  }

  return [minDate, maxDate];
};
export var formatNumber = function formatNumber(value) {
  return value > 9 ? value : "0".concat(value);
};
export var getDate = function getDate(value, maxDate, minDate) {
  var v = value || new Date();

  if (v.getTime() > maxDate.getTime()) {
    v = cloneDate(maxDate);
  }

  if (v.getTime() < minDate.getTime()) {
    v = cloneDate(minDate);
  }

  return v;
};
export function cloneDate(d) {
  return new Date(d.getTime());
}
export var getDaysInMonth = function getDaysInMonth(date) {
  var resultDate = new Date(date.getFullYear(), date.getMonth(), 1);
  resultDate.setDate(1);
  resultDate.setMonth(resultDate.getMonth() + 1);
  resultDate.setDate(resultDate.getDate() - 1);
  return resultDate.getDate();
};
export function updateYear(d) {
  var isAdd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var maxDate = arguments.length > 2 ? arguments[2] : undefined;
  var minDate = arguments.length > 3 ? arguments[3] : undefined;
  var year = d.getFullYear();

  if (isAdd === true) {
    year = year + 1;
  } else {
    year = year - 1;
  }

  var newDate = new Date(year, d.getMonth(), 1);
  return getDate(newDate, maxDate, minDate);
}
export function updateWeek(d) {
  var isAdd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var maxDate = arguments.length > 2 ? arguments[2] : undefined;
  var minDate = arguments.length > 3 ? arguments[3] : undefined;
  var newD = dayjs(d)[isAdd ? 'add' : 'subtract'](1, 'week');
  return getDate(new Date(newD.valueOf()), maxDate, minDate);
}
export function updateMonth(d) {
  var isAdd = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var maxDate = arguments.length > 2 ? arguments[2] : undefined;
  var minDate = arguments.length > 3 ? arguments[3] : undefined;
  var month = d.getMonth();
  var year = d.getFullYear();

  if (isAdd === true) {
    month = month + 1;

    if (month > MAX_MONTH) {
      month = MIN_MONTH;
      year = year + 1;
    }
  } else {
    month = month - 1;

    if (month < MIN_MONTH) {
      month = MAX_MONTH;
      year = year - 1;
    }
  }

  var newDate = new Date(year, month, 1);
  var resultDate = getDate(new Date(newDate), maxDate, minDate);
  return resultDate;
}
export function getMonthList(visibleDate, locale) {
  var months = [];

  for (var month = MIN_MONTH; month <= MAX_MONTH; month += 1) {
    months.push({
      label: "".concat(formatNumber(month + 1)).concat(locale.month || ''),
      value: new Date(visibleDate.getFullYear(), month)
    });
  }

  return months;
}
export function getDayList(local) {
  var days = [];

  for (var day = MIN_DAY; day <= MAX_DAY; day += 1) {
    days.push({
      label: local.weekdays && local.weekdays[day],
      value: day
    });
  }

  return days;
}
export function getPreMonthDates(visibleDate, locale) {
  var days = [];
  var firstDay = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), 1);
  var visibleDay = firstDay.getDay();

  if (visibleDay === 0) {
    return days;
  }

  var lastMonthDays = 0;
  var year = visibleDate.getFullYear();
  var month = visibleDate.getMonth();

  if (visibleDate.getMonth() === 0) {
    year = year - 1;
    month = MAX_MONTH;
  } else {
    month = month - 1;
  }

  lastMonthDays = getDaysInMonth(new Date(year, month, 1));
  var beginDate = lastMonthDays - (visibleDay - 1);

  for (var i = 0; i < visibleDay; i++) {
    days.push({
      label: "".concat(beginDate + i),
      value: new Date(year, month, beginDate + i)
    });
  }

  return days;
}
export function getCurrentMonthDates(visibleDate, locale) {
  var days = [];
  var monthDays = getDaysInMonth(visibleDate);

  for (var i = 1; i <= monthDays; i++) {
    days.push({
      label: i.toString(),
      value: new Date(visibleDate.getFullYear(), visibleDate.getMonth(), i)
    });
  }

  return days;
}
export function getNextMonthDates(visibleDate, locale) {
  var days = [];
  var monthDays = getDaysInMonth(visibleDate);
  var lastDate = new Date(visibleDate.getFullYear(), visibleDate.getMonth(), monthDays);
  var lastDay = lastDate.getDay();
  var year = visibleDate.getFullYear();
  var month = visibleDate.getMonth();

  if (visibleDate.getMonth() === MAX_MONTH) {
    year = year + 1;
    month = MIN_MONTH;
  } else {
    month = month + 1;
  }

  for (var i = lastDay + 1; i <= MAX_DAY; i++) {
    days.push({
      label: "".concat(i - lastDay),
      value: new Date(year, month, i - lastDay)
    });
  }

  return days;
}
export function getCurrentMonthList(visibleDate, local) {
  var preDates = getPreMonthDates(visibleDate, local);
  var currentDates = getCurrentMonthDates(visibleDate, local);
  var nextDates = getNextMonthDates(visibleDate, local);
  return preDates.concat(currentDates).concat(nextDates);
}
export function isTheSameDay(date1, date2) {
  if (!date1 || !date2) {
    return false;
  }

  return dayjs(date1).isSame(date2, 'day');
}
export function isTheSameMonth(date1, date2) {
  if (!date1 || !date2) {
    return false;
  }

  return dayjs(date1).isSame(date2, 'month');
}
export function isTheSameYear(date1, date2) {
  if (!date1 || !date2) {
    return false;
  }

  return date1.getFullYear() === date2.getFullYear();
}
export function isDateDisabled(cellDate, disableFunc, maxDate, minDate, view) {
  if (cellDate.getTime() > maxDate.getTime()) {
    return true;
  }

  if (cellDate.getTime() < minDate.getTime()) {
    return true;
  }

  if (disableFunc) {
    return disableFunc(cellDate, view);
  }

  return false;
}
export function isDateInRange(cellDate, selectedData) {
  if (!cellDate || !selectedData) {
    return false;
  }

  if (!Array.isArray(selectedData)) {
    return false;
  }

  if (selectedData.length < 2) {
    return false;
  }

  if (cellDate.getTime() > selectedData[selectedData.length - 1].getTime()) {
    return false;
  }

  if (cellDate.getTime() < selectedData[0].getTime()) {
    return false;
  }

  return true;
}
export function isDateSelected(cellDate, selectedData, mode, type) {
  var isSelected = null;

  if (type === 'range') {
    if (!Array.isArray(selectedData)) {
      throw new Error('Type is range, but selected is not an array');
    }

    if (isTheSameDay(cellDate, selectedData[0])) {
      return CellStatusType.begin;
    }

    if (isTheSameDay(cellDate, selectedData[selectedData.length - 1])) {
      return CellStatusType.end;
    }

    return isSelected;
  }

  if (type === 'multi') {
    if (!Array.isArray(selectedData)) {
      throw new Error('Type is multi, but selected is not an array');
    }

    for (var i = 0; i < selectedData.length; i++) {
      if (isTheSameDay(cellDate, selectedData[i])) {
        return CellStatusType.selected;
      }
    }

    return isSelected;
  }

  if (Array.isArray(selectedData)) {
    throw new Error("Type is ".concat(mode, ", but selected is an array"));
  }

  if (mode === 'month' || mode === 'week') {
    if (isTheSameDay(cellDate, selectedData)) {
      isSelected = CellStatusType.selected;
    }
  }

  if (mode === 'year') {
    if (isTheSameMonth(cellDate, selectedData)) {
      isSelected = CellStatusType.selected;
    }
  }

  return isSelected;
}
export function getCellStatus(cellDate, selectedData, mode, type, disableFunc, maxDate, minDate, view) {
  var cellType = CellStatusType.normal;
  var today = isToday(cellDate);

  if (today) {
    cellType = CellStatusType.today;
  }

  var isInRange = type == 'range' && isDateInRange(cellDate, selectedData);

  if (isInRange) {
    cellType = CellStatusType.range;
  }

  var isSelected = isDateSelected(cellDate, selectedData, mode, type);

  if (isSelected) {
    cellType = isSelected;
  }

  var isDisable = isDateDisabled(cellDate, disableFunc, maxDate, minDate, view);

  if (isDisable) {
    cellType = CellStatusType.disabled;
  }

  return cellType;
}
export function isToday(cellDate) {
  if (!cellDate) {
    return false;
  }

  var cellYear = cellDate.getFullYear();
  var cellMonth = cellDate.getMonth();
  var cellDay = cellDate.getDate();
  var today = new Date();
  var todayYear = today.getFullYear();
  var todayMonth = today.getMonth();
  var todayDay = today.getDate();

  if (cellYear == todayYear && cellMonth == todayMonth && cellDay == todayDay) {
    return true;
  }

  return false;
}
export function isHaveDate(cellDate, selected) {
  if (!cellDate) {
    return -1;
  }

  if (!selected || selected.length < 1) {
    return -1;
  }

  for (var i = 0; i < selected.length; i++) {
    if (isTheSameDay(cellDate, selected[i])) {
      return i;
    }
  }

  return -1;
}
export function getFirstDayOfMonth(d) {
  return new Date(d.getFullYear(), d.getMonth(), 1);
}
export function getWeekArray(d, firstDayOfWeek) {
  var dayArray = [];
  var daysInMonth = getDaysInMonth(d);
  var weekArray = [];
  var week = [];

  for (var i = 1; i <= daysInMonth; i++) {
    dayArray.push(new Date(d.getFullYear(), d.getMonth(), i, d.getHours(), d.getMinutes()));
  }

  var addWeek = function addWeek(w) {
    var emptyDays = 7 - w.length;

    for (var _i = 0; _i < emptyDays; ++_i) {
      w[weekArray.length ? 'push' : 'unshift'](null);
    }

    weekArray.push(w);
  };

  dayArray.forEach(function (day) {
    if (week.length > 0 && day.getDay() === firstDayOfWeek) {
      addWeek(week);
      week = [];
    }

    week.push(day);

    if (dayArray.indexOf(day) === dayArray.length - 1) {
      addWeek(week);
    }
  });
  return weekArray;
}
export function getWeekDayArray(weekdays, firstDayOfWeek) {
  var beforeArray = [];
  var afterArray = [];

  for (var i = 0; i < weekdays.length; i++) {
    if (i < firstDayOfWeek) {
      afterArray.push(weekdays[i]);
    } else {
      beforeArray.push(weekdays[i]);
    }
  }

  return [].concat(beforeArray, afterArray);
}
export function getWeekDateArray(d, firstDayOfWeek) {
  var arr = [];

  for (var i = 0; i < 7; i++) {
    var weekDate = cloneDate(d);
    setWeek(weekDate, i, firstDayOfWeek);
    arr.push(weekDate);
  }

  return arr;
}
export function getMonthArray(d) {
  var length = 3;
  var months = [];
  var month = [];

  for (var i = 0; i < 12; i++) {
    month.push(new Date(d.getFullYear(), i, 1, d.getHours(), d.getMinutes()));

    if (month.length === length) {
      months.push(month);
      month = [];
    }
  }

  return months;
}
export function setWeek(d, day, firstDayOfWeek) {
  var week = d.getDay();
  d.setDate(d.getDate() + (day - week + firstDayOfWeek));
  return d;
}
export function cloneAsDate(d) {
  var clonedDate = cloneDate(d);
  clonedDate.setHours(0, 0, 0, 0);
  return clonedDate;
}
export function isBeforeDate(d1, d2) {
  var date1 = cloneAsDate(d1);
  var date2 = cloneAsDate(d2);
  return date1.getTime() < date2.getTime();
}
export function isAfterDate(d1, d2) {
  var date1 = cloneAsDate(d1);
  var date2 = cloneAsDate(d2);
  return date1.getTime() > date2.getTime();
}
export function isBetweenDates(dateToCheck, startDate, endDate) {
  return !isBeforeDate(dateToCheck, startDate) && !isAfterDate(dateToCheck, endDate);
}
export function isEqualDate(d1, d2) {
  return d1 && d2 && d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate();
}
export function normalizeValue(value, isSelectWeek, firstDayOfWeek) {
  if (!value) {
    return [];
  }

  if (!Array.isArray(value)) {
    if (isSelectWeek) {
      return [setWeek(cloneDate(value), 0, firstDayOfWeek), setWeek(cloneDate(value), 6, firstDayOfWeek)];
    }

    return [value];
  }

  return value;
}
export var getDefaultFormat = function getDefaultFormat(type, second, use12Hours) {
  var timeFormat = 'HH:mm';

  if (second) {
    timeFormat += ':ss';
  }

  if (use12Hours) {
    timeFormat = "".concat(timeFormat.replace('HH', 'hh'), " A");
  }

  return {
    date: 'YYYY-MM-DD',
    year: 'YYYY',
    month: 'YYYY-MM',
    datetime: ['YYYY-MM-DD', timeFormat].join(' '),
    time: timeFormat
  }[type];
};
export var getDateByValues = function getDateByValues(date, values, type, second, use12Hours) {
  var units = {
    date: 'year_month_date',
    year: 'year',
    month: 'year_month',
    datetime: "year_month_date_hour_minute".concat(second ? '_second' : ''),
    time: "hour_minute".concat(second ? '_second' : '')
  }[type].split('_');
  var d = cloneDate(date);

  if (units.includes('month')) {
    d.setDate(1);
  }

  units.forEach(function (unit, index) {
    var value = values[index];

    switch (unit) {
      case 'year':
        d.setFullYear(value);
        break;

      case 'month':
        d.setMonth(value);
        break;

      case 'date':
        var daysInMonth = getDaysInMonth(d);
        d.setDate(value > daysInMonth ? daysInMonth : value);
        break;

      case 'hour':
        if (use12Hours && values[values.length - 1] === 'am' && value > 11) {
          d.setHours(value - 12);
          break;
        } else if (use12Hours && values[values.length - 1] === 'pm' && value < 12) {
          d.setHours(value + 12);
          break;
        }

        d.setHours(value);
        break;

      case 'minute':
        d.setMinutes(value);
        break;

      case 'second':
        d.setSeconds(value);
        break;

      default:
        break;
    }
  });
  return d;
};
export var getValues = function getValues(value, type, second, use12Hours, maxDate, minDate) {
  value = getDate(value, maxDate, minDate);
  var date = dayjs(value);
  var values = [];
  var format = getDefaultFormat(type, second, use12Hours);
  var hour = date.hour();

  if (format.indexOf('YYYY') !== -1) {
    values.push(date.year());
  }

  if (format.indexOf('MM') !== -1) {
    values.push(date.month());
  }

  if (format.indexOf('DD') !== -1) {
    values.push(date.date());
  }

  if (format.indexOf('HH') !== -1 || format.indexOf('hh') !== -1) {
    values.push(hour);
  }

  if (format.indexOf('mm') !== -1) {
    values.push(date.minute());
  }

  if (format.indexOf('ss') !== -1) {
    values.push(date.second());
  }

  if (use12Hours) {
    values.push(hour > 11 ? 'pm' : 'am');
  }

  return values;
};
export var getYears = function getYears(maxDate, minDate) {
  var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  var years = [];
  var maxYears = maxDate.getFullYear();
  var minYears = minDate.getFullYear();

  for (var year = minYears; year <= maxYears; year += 1) {
    years.push({
      label: "".concat(year).concat(locale.year || ''),
      value: year
    });
  }

  return years;
};
export var getMonths = function getMonths(value, maxDate, minDate) {
  var locale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  var months = [];
  var minMonth = 0;
  var maxMonth = 11;

  if (value.getFullYear() === minDate.getFullYear()) {
    minMonth = minDate.getMonth();
  }

  if (value.getFullYear() === maxDate.getFullYear()) {
    maxMonth = maxDate.getMonth();
  }

  for (var month = minMonth; month <= maxMonth; month += 1) {
    months.push({
      label: "".concat(formatNumber(month + 1)).concat(locale.month || ''),
      value: month
    });
  }

  return months;
};
export var getDays = function getDays(value, maxDate, minDate) {
  var locale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  var days = [];
  var maxDay = getDaysInMonth(value);
  var minDay = 1;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth()) {
    maxDay = maxDate.getDate();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth()) {
    minDay = minDate.getDate();
  }

  for (var day = minDay; day <= maxDay; day += 1) {
    days.push({
      label: "".concat(formatNumber(day)).concat(locale.day || ''),
      value: day
    });
  }

  return days;
};
export var getHours = function getHours(value, maxDate, minDate) {
  var locale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  var use12Hours = arguments.length > 4 ? arguments[4] : undefined;
  var hours = [];
  var maxHour = 23;
  var minHour = 0;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth() && value.getDate() === maxDate.getDate()) {
    maxHour = maxDate.getHours();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth() && value.getDate() === minDate.getDate()) {
    minHour = minDate.getHours();
  }

  for (var hour = minHour; hour <= maxHour; hour += 1) {
    hours.push({
      label: "".concat(formatNumber(hour)).concat(locale.hour || ''),
      value: hour
    });
  }

  if (use12Hours) {
    var isPM = value.getHours() > 11;
    hours = hours.filter(function (item) {
      return isPM && item.value > 11 || !isPM && item.value < 12;
    }).map(function (item) {
      if (isPM) {
        var val = item.value - 12;
        return {
          label: "".concat(formatNumber(val)).concat(locale.hour || ''),
          value: item.value
        };
      }

      return item;
    });

    if (hours.length > 0 && (hours[0].value === 0 || hours[0].value === 12)) {
      var item = hours[0];
      hours.splice(0, 1);
      hours.push(Object.assign(Object.assign({}, item), {
        label: "12".concat(locale.hour || '')
      }));
    }
  }

  return hours;
};
export var getMinutes = function getMinutes(value, maxDate, minDate) {
  var locale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  var minutes = [];
  var maxMinute = 59;
  var minMinute = 0;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth() && value.getDate() === maxDate.getDate() && value.getHours() === maxDate.getHours()) {
    maxMinute = maxDate.getMinutes();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth() && value.getDate() === minDate.getDate() && value.getHours() === minDate.getHours()) {
    minMinute = minDate.getMinutes();
  }

  for (var minute = minMinute; minute <= maxMinute; minute += 1) {
    minutes.push({
      label: "".concat(formatNumber(minute)).concat(locale.minute || ''),
      value: minute
    });
  }

  return minutes;
};
export var getSeconds = function getSeconds(value, maxDate, minDate) {
  var locale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  var seconds = [];
  var maxSecond = 59;
  var minSecond = 0;

  if (value.getFullYear() === maxDate.getFullYear() && value.getMonth() === maxDate.getMonth() && value.getDate() === maxDate.getDate() && value.getHours() === maxDate.getHours() && value.getMinutes() === maxDate.getMinutes()) {
    maxSecond = maxDate.getSeconds();
  }

  if (value.getFullYear() === minDate.getFullYear() && value.getMonth() === minDate.getMonth() && value.getDate() === minDate.getDate() && value.getHours() === minDate.getHours() && value.getMinutes() === minDate.getMinutes()) {
    minSecond = minDate.getSeconds();
  }

  for (var second = minSecond; second <= maxSecond; second += 1) {
    seconds.push({
      label: "".concat(formatNumber(second)).concat(locale.second || ''),
      value: second
    });
  }

  return seconds;
};
export var getAMPM = function getAMPM(locale) {
  return ['am', 'pm'].map(function (key) {
    return {
      label: locale[key],
      value: key
    };
  });
};
export var getDataSource = function getDataSource(value, type, second, use12Hours, maxDate, minDate, locale) {
  value = getDate(value, maxDate, minDate);
  var format = getDefaultFormat(type, second, use12Hours);
  var data = [];

  if (format.indexOf('YYYY') !== -1) {
    data.push(getYears(maxDate, minDate, locale));
  }

  if (format.indexOf('MM') !== -1) {
    data.push(getMonths(value, maxDate, minDate, locale));
  }

  if (format.indexOf('DD') !== -1) {
    data.push(getDays(value, maxDate, minDate, locale));
  }

  if (format.indexOf('HH') !== -1 || format.indexOf('hh') !== -1) {
    data.push(getHours(value, maxDate, minDate, locale, use12Hours));
  }

  if (format.indexOf('mm') !== -1) {
    data.push(getMinutes(value, maxDate, minDate, locale));
  }

  if (format.indexOf('ss') !== -1) {
    data.push(getSeconds(value, maxDate, minDate, locale));
  }

  if (use12Hours) {
    data.push(getAMPM(locale));
  }

  return data;
};
export var getTargetDate = function getTargetDate(modeType, visibleDate, type, maxDate, minDate) {
  var newTime = visibleDate;

  if (type == 'pre') {
    if (modeType == 'week') {
      newTime = updateWeek(visibleDate, false, maxDate, minDate);
    }

    if (modeType == 'month') {
      newTime = updateMonth(visibleDate, false, maxDate, minDate);
    }

    if (modeType == 'year') {
      newTime = updateYear(visibleDate, false, maxDate, minDate);
    }
  }

  if (type == 'next') {
    if (modeType == 'week') {
      newTime = updateWeek(visibleDate, true, maxDate, minDate);
    }

    if (modeType == 'month') {
      newTime = updateMonth(visibleDate, true, maxDate, minDate);
    }

    if (modeType == 'year') {
      newTime = updateYear(visibleDate, true, maxDate, minDate);
    }
  }

  return newTime;
};
export var genContentHeight = function genContentHeight(modeType) {
  if (modeType == 'week') {
    return '220';
  }

  if (modeType == 'month') {
    return '580';
  }

  if (modeType == 'year') {
    return '700';
  }

  return '700';
};
export var genTotalPageDates = function genTotalPageDates(minDate, maxDate, mode, visibleDate, firstDay) {
  var dates = [];
  var lastPre = visibleDate;

  while (true) {
    lastPre = dates.length > 0 ? dates[dates.length - 1] : lastPre;
    var pre = getTargetDate(mode, lastPre, 'pre', maxDate, minDate);

    if (isTheSameDay(pre, lastPre)) {
      break;
    }

    if (mode == 'week') {
      var tempNext = getTargetDate(mode, pre, 'next', maxDate, minDate);

      if (!isTheSameDay(tempNext, lastPre)) {
        break;
      }
    }

    if (mode == 'month') {
      if (isTheSameMonth(pre, lastPre)) {
        break;
      }
    }

    if (mode == 'year') {
      if (isTheSameYear(pre, lastPre)) {
        break;
      }
    }

    dates.push(pre);
  }

  dates = dates.reverse();
  dates.push(visibleDate);
  var lastNext = visibleDate;

  while (true) {
    lastNext = dates.length > 0 ? dates[dates.length - 1] : lastNext;
    var next = getTargetDate(mode, lastNext, 'next', maxDate, minDate);

    if (isTheSameDay(next, lastNext)) {
      break;
    }

    if (mode == 'week') {
      var tempPre = getTargetDate(mode, next, 'pre', maxDate, minDate);

      if (!isTheSameDay(tempPre, lastNext)) {
        break;
      }
    }

    if (mode == 'month') {
      if (isTheSameMonth(next, lastNext)) {
        break;
      }
    }

    if (mode == 'year') {
      if (isTheSameYear(next, lastNext)) {
        break;
      }
    }

    dates.push(next);
  }

  return dates;
};
export var getSelectedIndex = function getSelectedIndex(dates, selectedDate) {
  if (!dates || dates.length < 1) {
    return 1;
  }

  for (var i = 0; i < dates.length; i++) {
    if (!dates[i]) {
      continue;
    }

    if (isTheSameDay(dates[i], selectedDate)) {
      return i;
    }
  }

  return 1;
};