import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
dayjs.extend(duration);

export function formatTimer(createTime, updateTime) {
  const minute = 1000 * 60;
  const hour = minute * 60;
  const day = hour * 24;
  const week = day * 7;
  const month = day * 30;
  const time1 = dayjs().valueOf(); //当前的时间戳
  let time2 = null;

  if (!updateTime) {
    time2 = dayjs(createTime).valueOf(); //指定时间的时间戳
  } else {
    time2 = dayjs(updateTime).valueOf();
  }
  const time = time1 - time2;

  let result = null;
  if (time < 0) {
    return "刚刚";
  } else if (time / month >= 1) {
    result = parseInt((time / month).toString()) + "个月前";
  } else if (time / week >= 1) {
    result = parseInt((time / week).toString()) + "周前";
  } else if (time / day >= 1) {
    result = parseInt((time / day).toString()) + "天前";
  } else if (time / hour >= 1) {
    result = parseInt((time / hour).toString()) + "小时前";
  } else if (time / minute >= 1) {
    result = parseInt((time / minute).toString()) + "分钟前";
  } else {
    result = "刚刚";
  }
  return result;
}

export function formatHSM(seconds) {
  let timeStr = dayjs.duration(seconds, "seconds");
  let formatStr = "HH时mm分ss秒";
  if (timeStr.$d.years > 0) {
    formatStr = "YY年MM月DD天HH时mm分ss秒";
  } else if (timeStr.$d.months > 0) {
    formatStr = "MM月DD天HH时mm分ss秒";
  } else if (timeStr.$d.days > 0) {
    formatStr = "DD天HH时mm分ss秒";
  }
  return timeStr.format(formatStr);
}
