/*
 * @Author: Mm
 * @Date: 2025-05-08 17:46:47
 * @LastEditors: Mm
 * @LastEditTime: 2025-06-18 10:46:57
 * 不写bug的程序员不是一个好程序员!
 */
/*
 * @Author: Mm
 * @Date: 2025-05-06 14:08:27
 * @LastEditors: Mm
 * @LastEditTime: 2025-05-26 09:22:40
 * 不写bug的程序员不是一个好程序员!
 */
import { createApp, watchEffect } from 'vue';

import { registerAccessDirective } from '@vben/access';
import { initTippy } from '@vben/common-ui';
import { preferences } from '@vben/preferences';
import { initStores } from '@vben/stores';
import '@vben/styles';
import '@vben/styles/antd';

import { useTitle } from '@vueuse/core';

import { setupGlobalComponent } from '#/components/global';
import { $t, setupI18n } from '#/locales';

import { initComponentAdapter } from './adapter/component';
import App from './app.vue';
import { router } from './router';
import  vuexStore from './store/vuex.js'
import utils from './utils/utils.js'
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import '#/assets/global.css';
import '#/assets/theme.less';
import 'vant/lib/index.css';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import Ellipsis from '#/components/common/Ellipsis.vue';
import WDialog from '#/components/common/WDialog.vue';
import Tip from '#/components/common/Tip.vue';
import Avatar from '#/components/common/Avatar.vue';
//新增导入 addAPIProvider
import { Icon, addAPIProvider } from '@iconify/vue';

//注册自定义图标服务地址
const iconifyAPI = import.meta.env.VITE_ICONIFY_API || 'http://53.1.252.95:8080';
addAPIProvider('', {
  resources: [iconifyAPI],
});

async function bootstrap(namespace: string) {
  // 初始化组件适配器
  await initComponentAdapter();

  // // 设置弹窗的默认配置
  // setDefaultModalProps({
  //   fullscreenButton: false,
  // });
  // // 设置抽屉的默认配置
  // setDefaultDrawerProps({
  //   zIndex: 1020,
  // });

  const app = createApp(App);
  app.use(ElementPlus);
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
  }
  app.component('Icon', Icon);
  app.component('iconify', Icon);
  app.component('Ellipsis', Ellipsis)
  app.component('WDialog', WDialog)
  app.component('Tip', Tip)
  app.component('w-avatar', Avatar);
  

  // 全局组件
  setupGlobalComponent(app);

  // 国际化 i18n 配置
  await setupI18n(app);

  // 配置 pinia-tore
  await initStores(app, { namespace });

  // 安装权限指令
  registerAccessDirective(app);

  // 初始化 tippy
  initTippy(app);

  // 配置路由及路由守卫
  app.use(router);

  app.use(vuexStore)
  app.use(utils);

  // 动态更新标题
  watchEffect(() => {
    if (preferences.app.dynamicTitle) {
      const routeTitle = router.currentRoute.value.meta?.title;
      const pageTitle =
        (routeTitle ? `${$t(routeTitle)} - ` : '') + preferences.app.name;
      useTitle(pageTitle);
    }
  });

  app.mount('#app');
}

export { bootstrap };
