<script setup lang="ts">
import { onMounted } from 'vue';
import { RouterView, useRouter } from 'vue-router';
import { VxeUI } from 'vxe-table';
import { useDark } from '@vueuse/core';
import i18n from '@/utils/i18n';

const isDark = useDark();
const router = useRouter();
const w: any = window;
const $wujie: any = w.$wujie;

localStorage.setItem('USER_INFO_DATA', $wujie?.props?.USER_INFO_DATA || '{"tenant_id":"1979091834410176514","sub":"13866066913","iss":"https://honeycombis.com","active":true,"token_type":{"value":"Bearer"},"client_id":"pig","access_token":"Qt4aGUkre17RBhQ29MArqsjek51kb3i4DPaNiKJIYkiw9tKGvf8y2fzoBOXBd7KYQA6tm6mDquSfY8byft02x8IqaxQ-kquHmi9GHsWcM8EsE42MKKmjCZYMwvxW9hP6","refresh_token":"6D9kzwttTCkFLA-t9fcfXHa-G586RUjeA85wZk5y3rm2KG3--KwvJFHRuyRNRPrw32AOf1TKLT5Ei4Gssmu2BoLCQg-VvLqo0siaPdVNtAOfgSEuOG3-y8ARLz2Pc7Ku","aud":["pig"],"license":"https://honeycombis.com","nbf":"2025-11-06T07:59:44.541671288Z","user_id":"1980235559149838337","scope":["server"],"exp":"2025-12-26T08:04:44.541671288Z","iat":"2025-11-06T07:59:44.541671288Z","jti":"472dda93-5373-4f76-9d96-4fdef350e29c","username":"13866066913"}');

if ($wujie) {
  // 路径跳转
  $wujie.bus.$on('ROUTER_CHANGE', (data: any) => {
    router.replace({
      path: data.path.split('/spdm')[1],
      query: data.query,
    });
  });
  // 语言变化
  $wujie.bus.$on('LANGUAGE_CHANGE', (lang: string) => {
    langToggleFun(lang === 'zh-cn' ? 'ZH' : 'EN');
  });
  // 黑色主题变化
  $wujie.bus.$on('DARK_THEME_CHANGE', (darkMode: boolean) => {
    darkToggleFun(darkMode);
  });
}

onMounted(() => {
  if ($wujie) {
    $wujie.bus.$emit('SPMD_LOADED');
  }
});

const langToggleFun = (lang: string) => {
  if (lang === 'ZH') {
    i18n.global.locale = 'ZH';
  } else {
    i18n.global.locale = 'EN';
  }
  localStorage.setItem('LANG_LOCALE', i18n.global.locale);
};

const darkToggleFun = (darkMode: boolean) => {
  isDark.value = darkMode;
  VxeUI.setTheme(isDark.value ? 'dark' : 'light');
};
</script>

<template>
  <RouterView />
</template>
