// import Vue from 'vue'
import mitt from 'mitt'

/**
 * @description: 日期格式化
 * @param {*} timestamp 时间戳
 * @param {*} type 日期格式化类型
 * @param {*} hideYear 是否显示年 为true时 本年不显示 其他年份显示
 * @return {*}
 */

export function dateFormatter (timestamp, type, hideYear = false) {
  const date = new Date(timestamp)
  const year = date.getFullYear()
  const month = date.getMonth() + 1 // 月份从 0 开始,需要加 1
  const month_0 = (`0${date.getMonth() + 1}`).slice(-2) // 月份从 0 开始,需要加 1,并补零
  const day = date.getDate()
  const day_0 = (`0${date.getDate()}`).slice(-2) // 补零
  const hours = (`0${date.getHours()}`).slice(-2) // 补零
  const minutes = (`0${date.getMinutes()}`).slice(-2) // 补零
  const seconds = (`0${date.getSeconds()}`).slice(-2) // 补零

  const currentYear = new Date().getFullYear()
  const currentMonth = new Date().getMonth() + 1
  const currentDay = new Date().getDate()
  // 当天 不显示 年月日
  const isCurrentDay = year === currentYear && month === currentMonth && day === currentDay

  // 默认格式 2023-11-04 13:23:02
  return `${isCurrentDay ? '' : (`${year}-${month_0}-${day_0} `)}${hours}:${minutes}:${seconds}`
}

/**
 * @description: 节流
 * @param {*} fun
 * @param {*} delay
 * @return {*}
 */
export function _throttle (fn, delay = 1000) {
  let lastTime, timer
  return function () {

    const _this = this

    const args = arguments
    const nowTime = Date.now()
    if (lastTime && nowTime - lastTime < delay) {
      if (timer) clearTimeout(timer)
      timer = setTimeout(() => {
        lastTime = nowTime
        fn.apply(_this, args)
      }, delay)
    } else {
      lastTime = nowTime
      fn.apply(_this, args)
    }
  }
}