import App from './App.vue';
import { createApp } from 'vue';
import { setupStore } from '@/sheep/store';
import { setupSheep } from '@/sheep';
import { setupRouter } from '@/sheep/router';
// element-plus
import ElementPlus from 'element-plus';
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
import en from 'element-plus/dist/locale/en.mjs';
// i18n
import i18n from '@/locales';

// css reset
import 'normalize.css/normalize.css';

// unocss
import 'uno.css';

// iconify
import '@iconify/iconify';

// mock service
import { startMocking } from '@/sheep/mock';

async function bootstrap() {
  // 启动 Mock 服务（仅在开发环境且启用 Mock 时）
  if (import.meta.env.SHEEP_USE_MOCK === 'true') {
    await startMocking();
  }
  const app = createApp(App);

  // i18n
  app.use(i18n);

  // element-plus with dynamic locale
  const elementLocales = {
    'zh-CN': zhCn,
    'en-US': en,
  };
  app.use(ElementPlus, {
    locale: elementLocales[i18n.global.locale.value] || en,
  });

  // pinia store
  setupStore(app);

  // sheepjs init
  await setupSheep(app);

  // router
  await setupRouter(app).isReady();

  app.mount('#app');

  // 应用加载完成后，延迟一点时间确保页面完全渲染，然后隐藏加载页面
  setTimeout(() => {
    window.dispatchEvent(new Event('app-loaded'));
  }, 500);
}

bootstrap();
