import dayjs from "dayjs";

const INVALID_DATE = "N/A";

/** 格式化日期时间 */
export function formatDateTime(
  datetime: string | number | Date = "",
  template: string = "YYYY-MM-DD HH:mm:ss",
) {
  const day = dayjs(datetime);
  return day.isValid() ? day.format(template) : INVALID_DATE;
}

export function getSpecificDate(val: Date) {
  let endY = val.getFullYear();
  let endM = val.getMonth() + 1;
  let endD = val.getDate();
  let enddate =
    endY +
    "-" +
    (endM < 10 ? "0" + endM : endM) +
    "-" +
    (endD < 10 ? "0" + endD : endD);
  return enddate;
}
// 将时间转为年月日
export function formatDateToChinese(dateString: any) {
  const date = new Date(dateString);
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份从0开始，需要+1
  const day = date.getDate();

  return `${year}年${month}月${day}日`;

}

// 中文转英文函数
export function formatChineseToEnglish(chineseDate: any) {
  // 使用正则表达式提取年、月、日
  const match = chineseDate.match(/(\d{4})年(\d{1,2})月(\d{1,2})日/);
  console.log(match);

  if (!match) {
    throw new Error('日期格式不正确');
  }

  const year = parseInt(match[1]);
  const month = parseInt(match[2]) - 1; // 月份从0开始，需要-1
  const day = parseInt(match[3]);

  const date = new Date(year, month, day);

  // 星期名称数组
  const weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
  // 月份名称数组
  const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

  const weekday = weekdays[date.getDay()];
  const monthName = months[date.getMonth()];

  // 格式化日期部分，确保日为两位数
  const formattedDay = day < 10 ? '0' + day : day;

  return `${weekday} ${monthName} ${formattedDay} ${year} 00:00:00 GMT+0800`;
}
