declare global { // 设置全局属性
  interface Window { // window对象属性
    pendingETAG: boolean; // 加入对象
  }
}
export function getVersion() {
  // main.js 和 拦截器中加入
  if (('pendingETAG' in window && window.pendingETAG) || import.meta.env.DEV) return
  const htmlFileUrl = `${location.origin}${location.pathname}`
  window.pendingETAG = true
  setTimeout(() => {
    window.pendingETAG = false
  }, 20000)
  fetch(htmlFileUrl, {
    method: 'HEAD',
    cache: 'no-cache'
  }).then(res => {
    const version:string | undefined = res.headers.get('etag') as string || undefined
    if (!version) throw new Error('can not get version')
    const previousVersion = localStorage.getItem('version')
    if (!previousVersion) return localStorage.setItem('version', version)
    if (version !== previousVersion) {
      const isConfirm = confirm('检测到存在可用更新，是否更新？')
      if (isConfirm) {
        localStorage.setItem('version', version)
        location.reload()
      }
    }
  })
}
export function getVersionFR() {
  // 或者 路由守卫afterEach 中加入
  if (import.meta.env.DEV) return
  const htmlFileUrl = `${location.origin}${location.pathname}`
  fetch(htmlFileUrl, {
    method: 'HEAD',
    cache: 'no-cache'
  }).then(res => {
    const version:string | undefined = res.headers.get('etag') as string || undefined
    if (!version) throw new Error('can not get version')
    const previousVersion = localStorage.getItem('version')
    if (!previousVersion) return localStorage.setItem('version', version)
    if (version !== previousVersion) {
      localStorage.setItem('version', version)
      location.reload()
    }
  })
}
