import { setupI18n, autoLoadI18n } from '@crami/locale';
// import { STORAGE_LANG_KEY } from '@/store/mutation-type';
import { localStorage } from '@remote/icpx-platform/utils';
// 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: 'OrganizationPage',
        type: 'part',
        requestType: 'ts', // 请求文件的类型可选json/ts，建议使用ts，比json要灵活,默认ts
        glob: import.meta.glob('/src/views/Organization/locales/*.ts'),
      },
      {
        name: 'AdminGroupManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/AdminGroupManagement/locales/*.ts'),
      },
      {
        name: 'GroupManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/GroupManagement/locales/*.ts'),
      },
      {
        name: 'UserManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/UserManagement/locales/*.ts'),
      },
      {
        name: 'RoleManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/RoleManagement/locales/*.ts'),
      },
      {
        name: 'PostManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/PostManagement/locales/*.ts'),
      },
      {
        name: 'TenantManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/TenantManagement/locales/*.ts'),
      },
      {
        name: 'TenantAuthorization',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/TenantManagement/locales/*.ts'),
      },
      {
        name: 'DictManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/DictManagement/locales/*.ts'),
      },
      {
        name: 'AuthManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/AuthManagement/locales/*.ts'),
      },
      // 数据权限策略
      {
        name: 'DataPermission',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'DataPermissionPolicy',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'DataPermissionParameters',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'ProductionPermission',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'PermissionPllicy',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'AddPermissionPolicy',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'EditPermissionPolicy',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      {
        name: 'DetailPermissionPolicy',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/Permission/locales/*.ts'),
      },
      // 编码管理
      {
        name: 'CodeManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/CodeManagement/locales/*.ts'),
      },
      //导入配置
      {
        name: 'FileImport',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/ImpExp/locales/*.ts'),
      },
      //导出配置
      {
        name: 'FileExport',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/ImpExp/locales/*.ts'),
      },
      //座位管理
      {
        name: 'ImexTest',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/ImpExp/locales/*.ts'),
      },
      //应用管理
      {
        name: 'AppManagement',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/AppManagement/locales/*.ts'),
      },
      //简单查询
      {
        name: 'SimpleQuery',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
      //通用与分类查询
      {
        name: 'GeneralQueryIndex',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/GeneralQuery/locales/*.ts'),
      },
      //索引管理
      {
        name: 'EsIndexConfiguration',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
      //简单查询配置（DB）
      {
        name: 'DBConfigguration',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
      //通用查询配置
      {
        name: 'GeneralQueryConfig',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
      {
        name: 'GeneralQueryConfigCreate',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
      {
        name: 'GeneralQueryConfigEdit',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
      {
        name: 'GeneralQueryConfigView',
        type: 'part',
        requestType: 'ts',
        glob: import.meta.glob('/src/views/QueryConfiguration/locales/*.ts'),
      },
    ],
  });

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