import 'amfe-flexible';
import 'core-js/stable';
import 'regenerator-runtime/runtime';
import { createApp } from 'vue';
import { createPinia } from 'pinia';
import App from './App.vue';
import router from './router';
import utils from '@/assets/js/common';
import 'vant/lib/index.css';
import '@/style/utils.scss';
import appBridge from '@/assets/js/appBridge';
import VConsole from 'vconsole';
import './assets/css/main.scss';
// 引入 swiper 样式
import 'swiper/css';
import 'swiper/css/navigation';
import 'swiper/css/pagination';

const app = createApp(App);

if (import.meta.env.VITE_APP_ENV !== 'production') {
  const vconsole = new VConsole();
  // app.use(vconsole);
}

declare global {
  interface Window {
    onAndroidBack: (() => void) | null;
    appBridge?: any;
  }
}
// 定义路由是否可以返回的判断
const routerCanGoBack = () => {
  const position = router.options.history.state?.position;
  return typeof position === 'number' && position > 0;
};
router.beforeEach((to, from, next) => {
  if (to.query.digitalYiliToken) {
    utils.setSessionStorage('xToken', to.query.digitalYiliToken);
  }
  // appBridge.setTitle(to.meta.title as string);
  // 添加 Android 返回按钮监听方法
  window.onAndroidBack = () => {
    if (routerCanGoBack()) {
      router.go(-1);
    } else {
      appBridge.navigateBack();
    }
  };
  next();
});
app.use(createPinia());
app.use(router);
app.mount('#app');
