import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import dayjs from 'dayjs'

import App from './App.vue'
import router from './router'
import { useUserStore } from './stores/user'
import './style.css'

// 配置NProgress
NProgress.configure({ showSpinner: false })

const app = createApp(App)
const pinia = createPinia()

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(pinia)
app.use(router)
app.use(ElementPlus, {
  locale: zhCn,
})

// 全局注册dayjs
app.config.globalProperties.$dayjs = dayjs

// 应用初始化后检查用户状态
app.mount('#app')

// 初始化用户状态检查
const userStore = useUserStore()
if (userStore.isAuthenticated) {
  // 如果用户已登录，启动状态检查
  userStore.checkAuth().then((isValid) => {
    if (isValid) {
      userStore.startStatusCheck()
    }
  })
}