import store from '@/store'
import { createHashHistory } from 'history'

import wx from 'weixin-js-sdk'
import { baseUrlTemp } from './http'

const history = createHashHistory()

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

export default history

let homeFlag = '课堂'

// 2个不同模块的首页判断
export const homeJudgeFu = () => {
  if (window.location.href.includes('exhi')) homeFlag = '展览'
}

// 去首页
export const toHomeFu = () => {
  history.replace(homeFlag === '课堂' ? '/' : '/exhi')
}

// 判断是手机端还是pc端
export const isMobileFu = () => {
  if (
    window.navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    )
  ) {
    return true
  } else return false
}

// 在微信小程序中点击下载
export const wxDownFu = (url: string) => {
  wx.miniProgram.navigateTo({
    url: `/pages/download/index?url=${encodeURI(baseUrlTemp + url)}`
  })

  // wx.miniProgram.postMessage({
  //   data: { type: 'download', url: baseUrlTemp + url }
  // })
}
