import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './stores'
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/reset.css'
// Ant Design Vue的中文语言包
import antdZhCN from 'ant-design-vue/es/locale/zh_CN'
// 导入API拦截器和Token自动刷新机制
import './utils/request'

// 抑制ResizeObserver警告
const resizeObserverErrorHandler = (e) => {
  if (e.message === 'ResizeObserver loop completed with undelivered notifications.' ||
      e.message.includes('ResizeObserver loop')) {
    e.preventDefault()
    e.stopImmediatePropagation()
    return false
  }
}

// 处理未捕获的错误
window.addEventListener('error', resizeObserverErrorHandler)

// 处理未捕获的Promise错误
window.addEventListener('unhandledrejection', (e) => {
  if (e.reason && e.reason.message && e.reason.message.includes('ResizeObserver loop')) {
    e.preventDefault()
    return false
  }
})

// Ant Design Vue 4.x配置
// 使用Ant Design Vue内置的dayjs实例，避免版本冲突
const antdConfig = {
  locale: antdZhCN,
  getPopupContainer: (trigger) => trigger.parentNode
};

// 创建应用实例
const app = createApp(App)

// 安装Vue插件
app.use(router)
app.use(store)
app.use(Antd, antdConfig)

// 启动Token过期提醒功能
import { setupTokenExpirationWarning } from './utils/request'
setupTokenExpirationWarning()

app.mount('#app')