/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-08-20 13:53:10
 * @LastEditTime: 2021-09-10 09:20:03
 * @FilePath: \my-remax-ui\components\calendar\util.ts
 */
export var compareMonth = function compareMonth(date1, date2) {
  var year1 = date1.getFullYear();
  var year2 = date2.getFullYear();

  if (year1 === year2) {
    var month1 = date1.getMonth();
    var month2 = date2.getMonth();
    return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
  }

  return year1 > year2 ? 1 : -1;
};
export var compareDay = function compareDay(day1, day2) {
  if (!(day1 instanceof Date)) {
    day1 = new Date(day1);
  }

  if (!(day2 instanceof Date)) {
    day2 = new Date(day2);
  }

  var compareMonthResult = compareMonth(day1, day2);

  if (compareMonthResult === 0) {
    var date1 = day1.getDate();
    var date2 = day2.getDate();
    return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
  }

  return compareMonthResult;
};
export var getDayByOffset = function getDayByOffset(date, offset) {
  date = new Date(date);
  date.setDate(date.getDate() + offset);
  return date;
};
export var getPrevDay = function getPrevDay(date) {
  return getDayByOffset(date, -1);
};
export var getToday = function getToday() {
  var today = new Date();
  today.setHours(0, 0, 0, 0);
  return today;
};
export var copyDates = function copyDates(dates) {
  if (Array.isArray(dates)) {
    return dates.map(function (date) {
      if (date === null) {
        return date;
      }

      return new Date(date);
    });
  }

  return new Date(dates);
};
export var getMonthEndDay = function getMonthEndDay(year, month) {
  return 32 - new Date(year, month - 1, 32).getDate();
};
export var calcDateNum = function calcDateNum(date) {
  var day1 = new Date(date[0]).getTime();
  var day2 = new Date(date[1]).getTime();
  return (day2 - day1) / (1000 * 60 * 60 * 24) + 1;
};
export var getNextDay = function getNextDay(date) {
  return getDayByOffset(date, 1);
};
export var getMonths = function getMonths(minDate, maxDate) {
  var months = [];
  var cursor = new Date(minDate);
  cursor.setDate(1);

  do {
    months.push(cursor.getTime());
    cursor.setMonth(cursor.getMonth() + 1);
  } while (compareMonth(cursor, maxDate) !== 1);

  return months;
};
export function formatMonthTitle(date) {
  return "".concat(date.getFullYear(), " ").concat(date.getMonth() + 1);
}