import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
import App from './App.vue'
import router from './router'
import curdPlugin from './utils/curd'
import webApiCall from '@/utils/webApiCall'
// import * as echarts from 'echarts'

const app = createApp(App)
app.config.errorHandler = (err, instance, info) => {
  console.error('Vue 错误:', err)
  console.log('发生错误的组件:', instance)
  console.log('错误信息:', info)
}
app.use(webApiCall)

// 应用启动时，从 localStorage 恢复 mqttTopic
const savedMqttTopic = localStorage.getItem('mqttTopic')
if (savedMqttTopic && window.updateMqttTopic) {
  window.updateMqttTopic(savedMqttTopic)
}
// 未处理的 Promise 拒绝
window.addEventListener('unhandledrejection', (event) => {
  // console.error('未处理的 Promise 拒绝:', event.reason)
  event.preventDefault()
})
// 注册 Element Plus 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(curdPlugin)
app.use(createPinia())
app.use(ElementPlus)
app.use(router)

app.mount('#app')
