import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.replace.js";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import React, { useContext } from 'react';
import { noteOnce } from 'rc-util/es/warning';
import AntdConfigProvider from '../config-provider';
import arEG from './locale/ar_EG';
import zhCN from './locale/zh_CN';
import enUS from './locale/en_US';
import viVN from './locale/vi_VN';
import itIT from './locale/it_IT';
import esES from './locale/es_ES';
import jaJP from './locale/ja_JP';
import ruRU from './locale/ru_RU';
import msMY from './locale/ms_MY';
import zhTW from './locale/zh_TW';
import frFR from './locale/fr_FR';
import ptBR from './locale/pt_BR';
import koKR from './locale/ko_KR';
import idID from './locale/id_ID';
import deDE from './locale/de_DE';
import faIR from './locale/fa_IR';
import './style';

/**
 * 支持 Map 和 Object
 *
 * @name ValueEnum 的类型
 */

/** Render 第二个参数，里面包含了一些常用的参数 */
import { jsx as _jsx } from "react/jsx-runtime";
function get(source, path, defaultValue) {
  // a[3].b -> a.3.b
  const paths = path.replace(/\[(\d+)\]/g, '.$1').split('.');
  let result = source;
  let message = defaultValue;
  // eslint-disable-next-line no-restricted-syntax
  for (const p of paths) {
    message = Object(result)[p];
    result = Object(result)[p];
    if (message === undefined) {
      return defaultValue;
    }
  }
  return message;
}

/**
 * 创建一个操作函数
 *
 * @param locale
 * @param localeMap
 */
const createIntl = (locale, localeMap) => ({
  getMessage: (id, defaultMessage) => get(localeMap, id, defaultMessage) || defaultMessage,
  locale
});
const arEGIntl = createIntl('ar_EG', arEG);
const zhCNIntl = createIntl('zh_CN', zhCN);
const enUSIntl = createIntl('en_US', enUS);
const viVNIntl = createIntl('vi_VN', viVN);
const itITIntl = createIntl('it_IT', itIT);
const jaJPIntl = createIntl('ja_JP', jaJP);
const esESIntl = createIntl('es_ES', esES);
const ruRUIntl = createIntl('ru_RU', ruRU);
const msMYIntl = createIntl('ms_MY', msMY);
const zhTWIntl = createIntl('zh_TW', zhTW);
const frFRIntl = createIntl('fr_FR', frFR);
const ptBRIntl = createIntl('pt_BR', ptBR);
const koKRIntl = createIntl('ko_KR', koKR);
const idIDIntl = createIntl('id_ID', idID);
const deDEIntl = createIntl('de_DE', deDE);
const faIRIntl = createIntl('fa_IR', faIR);
const intlMap = {
  'ar-EG': arEGIntl,
  'zh-CN': zhCNIntl,
  'en-US': enUSIntl,
  'vi-VN': viVNIntl,
  'it-IT': itITIntl,
  'ja-JP': jaJPIntl,
  'es-ES': esESIntl,
  'ru-RU': ruRUIntl,
  'ms-MY': msMYIntl,
  'zh-TW': zhTWIntl,
  'fr-FR': frFRIntl,
  'pt-BR': ptBRIntl,
  'ko-KR': koKRIntl,
  'id-ID': idIDIntl,
  'de-DE': deDEIntl,
  'fa-IR': faIRIntl
};
const intlMapKeys = Object.keys(intlMap);
export { arEGIntl, enUSIntl, zhCNIntl, viVNIntl, itITIntl, jaJPIntl, esESIntl, ruRUIntl, msMYIntl, zhTWIntl, frFRIntl, ptBRIntl, koKRIntl, idIDIntl, deDEIntl, faIRIntl, intlMap, intlMapKeys };
const ConfigContext = /*#__PURE__*/React.createContext({
  intl: {
    ...zhCNIntl,
    locale: 'default'
  },
  valueTypeMap: {}
});
const {
  Consumer: ConfigConsumer,
  Provider: ConfigProvider
} = ConfigContext;

/**
 * 根据 antd 的 key 来找到的 locale 插件的 key
 *
 * @param localeKey
 */
const findIntlKeyByAntdLocaleKey = localeKey => {
  if (!localeKey) {
    return 'zh-CN';
  }
  const localeName = localeKey.toLocaleLowerCase();
  return intlMapKeys.find(intlKey => {
    const LowerCaseKey = intlKey.toLocaleLowerCase();
    return LowerCaseKey.includes(localeName);
  }) || 'zh-CN';
};

/**
 * 如果没有配置 locale，这里组件会根据 antd 的 key 来自动选择
 *
 * @param param0
 */
const ConfigProviderWrap = ({
  children
}) => {
  const {
    locale
  } = useContext(AntdConfigProvider.ConfigContext);
  return /*#__PURE__*/_jsx(ConfigConsumer, {
    children: value => {
      var _value$intl;
      const localeName = locale === null || locale === void 0 ? void 0 : locale.locale;
      const key = findIntlKeyByAntdLocaleKey(localeName);
      // antd 的 key 存在的时候以 antd 的为主
      const intl = localeName && ((_value$intl = value.intl) === null || _value$intl === void 0 ? void 0 : _value$intl.locale) === 'default' ? intlMap[key] : value.intl || intlMap[key];
      return /*#__PURE__*/_jsx(ConfigProvider, {
        value: {
          ...value,
          intl: intl || zhCNIntl
        },
        children: children
      });
    }
  });
};
export { ConfigConsumer, ConfigProvider, ConfigProviderWrap, createIntl };
export function useIntl() {
  const context = useContext(ConfigContext);
  noteOnce(!!context.intl, `
为了提升兼容性
<IntlProvider value={zhCNIntl}/>
需要修改为:
<ConfigProvider
  value={{
    intl: zhCNIntl,
  }}
/>
我们将会在下个版本中删除它
    `);
  noteOnce(!!context.intl, `
To improve compatibility
  <IntlProvider value={zhCNIntl}/>
Need to be modified to:
  <ConfigProvider
    value={{
      intl: zhCNIntl,
    }}
  />
We will remove it in the next version
    `);
  if (!context.intl) {
    return context || zhCNIntl;
  }
  return context.intl || zhCNIntl;
}
export default ConfigContext;