import { createApp } from 'vue';
import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
import '@wangeditor/editor/dist/css/style.css';
import VueGridLayout from 'vue-grid-layout';
import MateChat from '@matechat/core';
import '@devui-design/icons/icomoon/devui-icon.css';
import './plugins/assets';
import 'font-awesome/css/font-awesome.css';
// 引入 KaTeX 样式以支持数学公式渲染
import 'katex/dist/katex.min.css';
import { setupDayjs, setupIconifyOffline, setupLoading, setupNProgress } from './plugins';
import { setupStore } from './store';
import { setupRouter } from './router';
import { setupI18n } from './locales';
import { setupGlobalAxiosInterceptor, setupGlobalFetchInterceptor } from './utils/global-axios-interceptor';
import { globalAPI } from './utils/global-api';
import App from './App.vue';

async function setupApp() {
  setupLoading();
  setupNProgress();
  setupIconifyOffline();
  setupDayjs();

  const app = createApp(App);
  setupStore(app);
  await setupRouter(app);
  setupI18n(app);

  // 设置全局401错误拦截器
  setupGlobalAxiosInterceptor();
  setupGlobalFetchInterceptor();

  // 全局注册API工具，确保所有请求都通过中间件
  window.$api = globalAPI;

  app.use(VueGridLayout);
  app.use(MateChat);
  // 全局注册 wangeditor 组件
  app.component('WangEditor', Editor);
  app.component('WangToolbar', Toolbar);

  app.mount('#app');
}
setTimeout(() => {
  setupApp();
}, 200);
