import { createApp, nextTick } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

import App from './App.vue'
import router from './router'
import './style/main.scss'

// 創建應用實例
const app = createApp(App)

// 註冊 Element Plus 圖標
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 使用插件
app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
  locale: zhCn
})

// 全局錯誤處理
app.config.errorHandler = (err, vm, info) => {
  console.error('全局錯誤處理:', err, info)
}

// 隱藏載入畫面
const hideLoading = () => {
  const loading = document.getElementById('loading')
  if (loading) {
    loading.style.display = 'none'
  }
}

// 掛載應用
app.mount('#app')

// 應用載入完成後隱藏載入畫面
nextTick(() => {
  hideLoading()
})

export default app