import { createApp } from 'vue';
import './plugins/assets';
import { localStg } from '@/utils/storage';
// main.js or main.ts
import 'font-awesome/css/font-awesome.css'; // 导入所有的 Font Awesome 样式
import { setupDayjs, setupIconifyOffline, setupLoading, setupNProgress } from './plugins';
import { setupStore } from './store';
import { setupRouter } from './router';
import { setupI18n } from './locales';
import App from './App.vue';

let timeoutHandle: string | number | NodeJS.Timeout | undefined;

function startInactivityTimer(app: any) {
  clearTimeout(timeoutHandle);
  timeoutHandle = setTimeout(
    () => {
      localStg.remove('token');
      localStg.remove('refreshToken');
      localStg.remove('userInfo');
      app.config.globalProperties.$router.push('/login/pwd-login');
    },
    29 * 60 * 1000
  ); // 30分钟 = 30 * 60 * 1000 毫秒
}

function resetInactivityTimer(app: any) {
  startInactivityTimer(app);
}

async function setupApp() {
  setupLoading();

  setupNProgress();

  setupIconifyOffline();

  setupDayjs();

  const app = createApp(App);

  setupStore(app);

  await setupRouter(app);

  setupI18n(app);
  // 系统更新提示
  // setupAppVersionNotification();

  // 监听用户的操作
  window.addEventListener('mousemove', () => resetInactivityTimer(app));
  window.addEventListener('keydown', () => resetInactivityTimer(app));
  window.addEventListener('click', () => resetInactivityTimer(app));
  window.addEventListener('scroll', () => resetInactivityTimer(app));

  // 启动计时器
  startInactivityTimer(app);

  app.mount('#app');
}

setTimeout(() => {
  setupApp();
}, 200);
