import { createHashHistory } from 'history'
import { infoPageIDSet } from './storage'
const history = createHashHistory()
export default history

// 权限失效，回登录页，带上回跳路径
export const loginOutFu = () => {
  const urlAll = window.location.href
  const urlArr = urlAll.split('/#/')

  if (!urlAll.includes('/login')) {
    if (urlArr[1]) {
      history.replace(`/login?back=${urlArr[1]}`)
    } else history.replace('/login')
  }
}

// 新窗口打开藏品详情页面
export const openLink = (path: string) => {
  // 清空藏品详情页的id存储
  infoPageIDSet(0)

  const urlAll = window.location.href
  const qian = urlAll.split('/#/')[0]
  window.open(`${qian}/#${path}`, '_blank')
}

// 监听路由
let routerLength = 0
history.listen((_: any, listener: any) => {
  if (listener === 'PUSH') {
    routerLength += 1
  } else if (listener === 'POP') {
    if (routerLength >= 1) routerLength -= 1
  }
})

// 点击返回
export const backPageFu = (path: string) => {
  if (routerLength) history.go(-1)
  else history.push(path)
}
