// 格式化时间戳为 "刚刚, 几分钟前"
export function formatTimestamp(times) {
  const now = new Date().getTime();
  const date = new Date(times);
  const diff = now - date.getTime();

  if (diff > 0) {
    // 刚刚
    if (diff < 60000) {
      return '刚刚';
    }

    // n分钟前
    if (diff < 3600000) {
      return Math.floor(diff / 60000) + '分钟前';
    }

    // n小时前
    if (diff < 86400000) {
      return Math.floor(diff / 3600000) + '小时前';
    }

    // 昨天
    if (diff < 172800000) {
      return '昨天';
    }

    // 前天
    if (diff < 259200000) {
      return '前天';
    }

    // 2天前 4 * 24 * 60 * 60 * 1000
    if (diff < 345600000) {
      return '2天前';
    }

    // 3天前 5 * 24 * 60 * 60 * 1000
    if (diff < 432000000) {
      return '3天前';
    }
  }

  // n月n日
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');

  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  // const seconds = String(date.getSeconds()).padStart(2, '0');
  return month + '-' + day + ' ' + hours + ':' + minutes;
}


// 格式化时间 '2025-09-19  星期三'
export function getWeekStr(time) {
  if (!time) return '';

  const date = new Date(time);
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');

  const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  const weekday = weekdays[date.getDay()];

  return `${year}-${month}-${day} ${weekday}`;


}

//  获取时间 start end
export function getDate(type) {
  const date = new Date();

  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();

  if (type === 'start') {
    year = year - 10;
  } else if (type === 'end') {
    year = year + 10;
  }
  month = month > 9 ? month : '0' + month;;
  day = day > 9 ? day : '0' + day;

  return `${year}-${month}-${day}`;
}

// 日期格式化
export function parseTime(time, pattern) {
  if (arguments.length === 0 || !time) {
    return null
  }
  const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    } else if (typeof time === 'string') {
      time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}


/**
 * 获取常用时间范围
 * @param {string} rangeType - 范围类型: '3days' | '7days' | '1month' | '3months'
 * @returns {Object} { startDate, endDate } 返回开始和结束日期，格式为 'YYYY-MM-DD'
 */
export function getDateRange(rangeType) {
  const endDate = new Date(); // 结束日期默认为今天
  // 方案一：使用原生JavaScript
  const startDate = new Date();
  switch (rangeType) {
    case '3days':
      startDate.setDate(endDate.getDate() - 2); // 近3天，含今天
      break;
    case '7days':
      startDate.setDate(endDate.getDate() - 6); // 近7天，含今天
      break;
    case '1month':
      startDate.setMonth(endDate.getMonth() - 1); // 近1个月
      break;
    case '3months':
      startDate.setMonth(endDate.getMonth() - 3); // 近3个月
      break;
    default:
      throw new Error('不支持的rangeType');
  }
  // 格式化日期为 'YYYY-MM-DD'
  const format = (date) => date.toISOString().split('T')[0];
  return {
    startDate: format(startDate),
    endDate: format(endDate)
  };
}
// 获取rangeType名称
export function getDateRangeName(rangeType){
  let str = "";
  switch (rangeType) {
    case '3days':
      str = "近3天"; // 近3天，含今天
      break;
    case '7days':
      str = "近7天";// 近7天，含今天
      break;
    case '1month':
      str = "近1月";// 近1月
      break;
    case '3months':
      str = "近3月"; // 近3月
      break;
    default:
      str="请选择";
      // throw new Error('不支持的rangeType');
  }
  return str;
}
