import { useHttp } from '@crami/http';
import { setI18nLanguage, useI18n } from '@crami/locale';
import { getInfo, ICP_CONFIG } from '@/utils/commMethods';
import type { BaseApi } from '@crami/http';
import { omit, pick } from 'lodash-es';
import { defaultLang, langMap } from '@/locales';
// import zhCNPermission from '@/views/platform/Permission/locales/zh-CN';
// import enUSPermission from '@/views/platform/Permission/locales/en-US';
// import zhCNDict from '@/views/platform/DictManagement/locales/zh-CN';
// import enUSDict from '@/views/platform/DictManagement/locales/en-US';
// import zhCNQueryConfiguration from '@/views/platform/QueryConfiguration/locales/zh-CN';
// import enUSQueryConfiguration from '@/views/platform/QueryConfiguration/locales/en-US';

const extraZhCN = {
  // ...zhCNPermission,
  // ...zhCNDict,
  // ...zhCNQueryConfiguration,
};

const extraEnUS = {
  // ...enUSPermission,
  // ...enUSDict,
  // ...enUSQueryConfiguration,
};
export interface DynamicLocaleParams {
  project?: string;
  lang?: string[];
}
export interface ICustomLocale {
  // 如果要返回全部字段，则返回空数组
  navs?: Array<{ [key: string]: Array<any> }>;
  views?: Array<{ [key: string]: Array<any> }>;
  menus?: Array<{ [key: string]: Array<any> }>;
  customs?: Array<{ [key: string]: Array<any> }>;
}
const LOCALE_API = '/api/studio-cache/dic/getGlobalDic';
export const getDynamicLocale = async (params?: DynamicLocaleParams, config?: Record<any, any>) => {
  const i18n = useI18n();
  const userInfo = getInfo(config ? config : ICP_CONFIG);
  const apiInfo: BaseApi = {
    url: LOCALE_API,
    method: 'post',
    params: {
      project: "PLM_PROJECT",
      // project: params?.project ? params.project : userInfo.projectCode || '',
      lang: params?.lang ? params.lang : ['zhs', 'en'],
    },
  };
  const result = await useHttp(apiInfo);
  const globalLocales = ['navs', 'link'];
  if (result.code === 200) {
    const data: Record<any, any> = result.data || {};
    // 将获取到的数据合并入多语言
    i18n.mergeLocaleMessage('zh-CN', {
      antd: {
        ...omit(data.zhs || {}, globalLocales),
        ...extraZhCN,
      },
      ...pick(data.zhs || {}, globalLocales),
    });
    i18n.mergeLocaleMessage('en-US', {
      antd: {
        ...omit(data.en || {}, globalLocales),
        ...extraEnUS,
      },
      ...pick(data.en || {}, globalLocales),
    });
  }
};
// 获取执行多语言
export const getCustomLocale = async (params: ICustomLocale, config?: any) => {
  const i18n = useI18n();
  const userInfo = getInfo(config ? config : ICP_CONFIG);

  const result = await useHttp({
    url: '/api/studio-cache/dic/getGeneralDic',
    method: 'post',
    params: {
      project: userInfo.project || '',
      lang: ['zhs', 'en'],
      params: {
        ...params,
      },
    },
  });

  // 合并多语言
  if (result.code === 200) {
    const data = result.data || {};
    // 将获取到的数据合并入多语言
    for (const key in data) {
      if (Object.prototype.hasOwnProperty.call(data, key)) {
        switch (key) {
          // 中文
          case 'zhs':
            i18n.mergeLocaleMessage('zh-CN', data[key]);
            break;
          // 英文
          case 'en':
            i18n.mergeLocaleMessage('en-US', data[key]);
            break;
          default:
            i18n.mergeLocaleMessage(key as string, data[key]);
            break;
        }
      }
    }
  }
};

// 多语言改变之后的处理
export const setLanguage = async (lang: string = defaultLang) => {
  // 获取多语言
  getDynamicLocale();
  localStorage.setItem('lang', lang);
  // 设置多语言
  setI18nLanguage(langMap[lang]);
};
