import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import router from './router'
import { createPinia } from 'pinia'
import App from './App.vue'
import './styles/global.scss'

import authService from '@/api/authService'

const app = createApp(App)

// Register all Element Plus icons
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(ElementPlus, { locale: zhCn })
app.use(createPinia())
app.use(router)
app.mount('#app')

// 后台异步获取配置与游客 Token，不阻塞首屏渲染
authService.getUserConfig().then(res => {
  if (res && res.data) {
    console.log("用户配置", res.data)
    // 只有在本地没有 token 时，才写入游客 token
    if (res.data.token && !localStorage.getItem('token')) {
      localStorage.setItem('token', res.data.token)
    }
    if (res.data.config) {
      window.__USER_CONFIG__ = res.data.config
    }
  }
}).catch(err => {
  console.error('获取用户配置失败:', err)
})
