import dayjs from 'dayjs';

const isSameOrBefore = require('dayjs/plugin/isSameOrBefore');
dayjs.extend(isSameOrBefore);
export const DateTool = {
  DateFormat: {
    YYYY: 'YYYY',
    YYYY_MM: 'YYYY-MM',
    YYYY_MM_DD: 'YYYY-MM-DD',
    YYYY_MM_DD_HH_mm: 'YYYY-MM-DD HH:mm',
    YYYY_MM_DD_HH_mm_ss: 'YYYY-MM-DD HH:mm:ss',
    YYYY_MM_DD_HH_mm_ss_SSS: 'YYYY-MM-DD HH:mm:ss.SSS',
    YYYYMMDD: 'YYYYMMDD',
    YYYYMMDDHHmmss: 'YYYYMMDDHHmmss',
    YYYY_MM_DD_00_00_00: 'YYYY-MM-DD 00:00:00',
    YYYY_MM_DD_23_59_59: 'YYYY-MM-DD 23:59:59',
  },
  /**
   * 获取当天时间刻度
   * @param timeSplit 单位分钟
   */
  getTime: function (timeSplit = 5) {
    const now = dayjs();
    let zero = dayjs().startOf('date').add(1, 'hour');
    const result = [];
    while (zero.isSameOrBefore(now)) {
      result.push(zero.format('HH:mm'));
      zero = zero.add(timeSplit, 'minute');
    }
    return result;
  },
  now: (format = DateTool.DateFormat.YYYY_MM_DD) => {
    return dayjs().format(format);
  },
  /**
   * 获取一段时间的日期
   * @param start 开始时间
   * @param end 结束时间
   * @param format
   */
  getDayOfRange: function (start, end, format = this.DateFormat.YYYY_MM_DD) {
    const dates = [];
    let current = dayjs(start);
    while (current.isSameOrBefore(end)) {
      dates.push(current.format(format));
      current = current.add(1, 'day');
    }
    return dates;
  },

  getMonthsOfYearAgo: () => {
    const month = [];
    const currentDate = dayjs();
    const oneYearAgo = currentDate.subtract(1, 'year');
    let currentMonth = oneYearAgo.startOf('month');
    while (currentMonth.isBefore(currentDate.endOf('month'))) {
      month.push(currentMonth.format(DateTool.DateFormat.YYYY_MM));
      currentMonth = currentMonth.add(1, 'month').startOf('month');
    }
    return month;
  },

};
