import { getCurrentInstance, onUnmounted } from 'vue'

// 防抖（仅在最新一次请求的计时结束后执行）
export function useDebounce(fn, delay = 500, immediate = false) {
  let timer = null
  let instance = getCurrentInstance() // 获取当前组件实例

  const debounced = (...args) => {
    // 接收所有传入的参数
    if (timer) clearTimeout(timer)

    if (immediate && !timer) {
      // 立即执行
      fn.apply(this, args)
    }

    timer = setTimeout(() => {
      if (!immediate) fn.apply(this, args) // 将参数透传给原始函数
      timer = null // 清理timer引用
    }, delay)
  }

  // 取消防抖执行
  debounced.cancel = () => {
    clearTimeout(timer)
    timer = null
  }

  // 立即执行
  debounced.flush = (...args) => {
    clearTimeout(timer)
    timer = null
    fn.apply(this, args)
  }

  if (instance) {
    // 自动注册卸载钩子（仅组件内生效）
    onUnmounted(() => {
      debounced.cancel()
    })
  }

  return debounced
}

// 节流（执行第一次请求，直到计时器结束，才可以重新请求）
export function useThrottle(fn, delay = 1000) {
  let lastCall = 0

  const throttled = (...args) => {
    const now = Date.now()
    if (now - lastCall < delay) return
    lastCall = now
    fn(...args) // 将参数透传给原始函数
  }

  return throttled
}
