import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import '@/assets/styles/index.scss'
import i18n from './i18n/index'
import axios from "axios";
import './assets/icons/iconfont/iconfont.css'
import {$addStorageEvent} from '../src/utils/publicMethods'
import MetaInfo from 'vue-meta-info'
import loadingStateMixin from './utils/loadingStateMixin';
import networkRecoveryMixin from './mixins/networkRecoveryMixin';
import './utils/loadingRecovery';
import errorNotificationManager from '../src/utils/errorNotificationManager';



// 创建事件总线 用于组件通信
Vue.prototype.$bus = new Vue();

Vue.use(MetaInfo)
Vue.prototype.$addStorageEvent = $addStorageEvent // 添加storage事件
Vue.config.productionTip = false
Vue.use(ElementUI, {  
  i18n: (key, value) => i18n.t(key, value)
});
Vue.prototype.$axios = axios

console.log = ()=>{}  //全局关闭打印
// 全局注册混入 
Vue.mixin(loadingStateMixin);//loading状态管理
Vue.mixin(networkRecoveryMixin);//网络恢复后数据刷新

Vue.prototype.$baseApi = process.env.VUE_APP_BASE_URL //图片base路径
const screenWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
const isNarrowScreen = screenWidth < 1280;
Vue.prototype.$isMobile = isNarrowScreen





/**
 * 全局处理未捕获的 Promise 拒绝（仅限网络类错误），
 * 防止错误遮罩层把页面完全遮挡。不影响其他错误的默认行为。
 */
window.addEventListener('unhandledrejection', (event) => {
  const reason = event && event.reason;
  const message = typeof reason === 'string' ? reason : (reason && reason.message);
  if (message && (message === 'Network Error' || message.includes('timeout'))) {
    event.preventDefault();
    console.warn('[unhandledrejection] blocked network error:', message);
  }
});

// 在路由守卫中设置
router.beforeEach((to, from, next) => {
  // 从路由中获取语言参数
  const lang = to.params.lang || (localStorage.getItem('lang') || 'en');
  
  // 设置 HTML 的 lang 属性
  document.documentElement.lang = lang;

   // 设置i18n语言
   if (i18n.locale !== lang) {
    i18n.locale = lang;
  }
  const token = localStorage.getItem('token');
  if (!token || token === 'null') {
    // 通知断开所有客服连接
    localStorage.setItem('cs_disconnect_all', Date.now().toString());
  }
  
  
  next();
});

// 定期清理过期的错误记录
setInterval(() => {
  errorNotificationManager.cleanup();
}, 60000); // 每分钟清理一次

window.vm = new Vue({
  router,
  store,
  i18n,
  render: h => h(App),
  created() {
    this.$watch(
      () => this.$i18n.locale,
      (newLocale) => {
        this.updateMetaAndTitle();
      }
    );
    this.updateMetaAndTitle();
  },
  mounted () {
    document.dispatchEvent(new Event('render-event'))
  },
  methods: {
    updateMetaAndTitle() {
      document.title = this.$i18n.t('home.appTitle');
      const descriptionMeta = document.querySelector('meta[name="description"]');
      // const keywordsMeta = document.querySelector('meta[name="keywords"]');

      if (descriptionMeta) {
        descriptionMeta.content = this.$i18n.t('home.metaDescription');
      }
      // if (keywordsMeta) {
      //   keywordsMeta.content = this.$i18n.t('home.metaKeywords');
      // }
    }
  }
}).$mount('#app')

// 初次加载和DOM加载完成时更新
document.addEventListener('DOMContentLoaded', () => {
  window.vm.updateMetaAndTitle();
});