import { createApp } from 'vue'
import App from './App.vue'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import router from '../router/index-student'
import vueFilter from '@/helpers/vueFilter'
import { postMessage, promisefiyPostMessage } from '@/helpers/native-message'

import 'normalize.css'

import '../styles/index.less'
import { state } from '@/state'
import { browser, setAuth } from '@/helpers/utils'

const app = createApp(App)

// import Vconsole from 'vconsole'
// const vconsole = new Vconsole()
postMessage(
  {
    api: 'getVersion'
  },
  (res: any) => {
    state.version = res.content.version
  }
)

// 判断是否是管乐团学生端，用来获取基础数据
if (browser().isOrchestraStudent) {
  // await promisefiyPostMessage({
  //   api: 'setCache',
  //   content: {
  //     key: 'h5-colexiu-token',
  //     value: ''
  //   }
  // })

  // 获取管乐团token
  promisefiyPostMessage({ api: 'getUserAccount' }).then((res: any) => {
    const content = res.content
    state.orchestraInfo.token = content.token
    state.orchestraInfo.phone = content.phone
    state.orchestraInfo.nickname = content.nickname
    state.orchestraInfo.avatar = content.avatar
    state.orchestraInfo.unionId = content.unionId || 0
  })

  // 管乐团里面，获取是否已安装酷乐秀
  const type = browser().orchestraAndroid
    ? 'com.cooleshow.student'
    : 'ColexiuStudent://'
  promisefiyPostMessage({ api: 'isInstall', content: { type: type } }).then((res: any) => {
    const content = res.content
    state.orchestraInfo.installStatus = content.installStatus
  })

  // 从缓存里面获取token
  promisefiyPostMessage({ api: 'getCache', content: { key: 'h5-colexiu-token' } }).then((res: any) => {
    const content = res.content
    if (content.value) {
      setAuth('bearer ' + content.value)
    }
  })
}

if (browser().isTeacher) {
  state.platformType = 'TEACHER'
} else if (browser().isStudent) {
  state.platformType = 'STUDENT'
} else {
  state.platformType = 'STUDENT'
}
if (state.platformType === 'TEACHER') {
  state.platformApi = '/api-teacher'
} else {
  state.platformApi = '/api-student'
}

dayjs.locale('zh-ch')
app.config.globalProperties.$dayjs = dayjs
app.config.globalProperties.$filters = vueFilter
app.use(router)

app.mount('#app')
