import { setupI18n, autoLoadI18n } from '@crami/locale';
// import { STORAGE_LANG_KEY } from '@/store/mutation-type';
import { defaultLang } from '../locales';

export function createLanguageGuard(router: any, app: any): void {
  // const lang = localStorage.get('zh-CN', defaultLang);
  setupI18n(app, {
    messages: {},
    locale: 'zh-CN',
    missingWarn: false,
    fallbackWarn: false,
    // 如果不需要国际化，可以打开如下两个配置，取消控制台的警告
    silentTranslationWarn: true,
    silentFallbackWarn: true,
    // 配置多语言模块加载信息
    loadLocalePool: [
      // 全局配置
      {
        name: 'home',
        type: 'global',
        requestType: 'ts',
        glob: import.meta.glob('/src/locales/lang/*.ts'),
      },
      {
        name: 'FormList',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/FormBuilder/locales/*.ts'),
      },
      {
        name: 'FormQuery',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/FormBuilder/locales/*.ts'),
      },
      {
        name: 'FormDesigner',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/FormBuilder/locales/*.ts'),
      },
      {
        name: 'CreateFormInstance',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/FormBuilder/locales/*.ts'),
      },
      {
        name: 'InitiateWorkflow',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'CreateProcess',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'ApprovalManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'viewProcess',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'WorkflowAgent',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'WorkflowTemplate',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'WorkflowExample',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
      {
        name: 'AdjustmentProcess',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Workflow/locales/*.ts'),
      },
    ],
  });

  // 使用router的路由守卫进行多语言的自动加载
  router.beforeEach(async (to, before, next) => {
    await autoLoadI18n(to.name as string);
    // await autoLoadI18n({ name: 'Message' });
    next();
  });
}
