import 'moment/locale/zh-cn';

import React from 'react';

import { ConfigProvider } from 'antd';
import moment from 'moment';

import {
  getDirection,
  getIntl,
  getLocale,
  localeInfo,
  RawIntlProvider,
  setIntl,
} from './localeExports';

export const LANG_CHANGE_EVENT = Symbol('LANG_CHANGE');

export function onCreate() {
  const locale = getLocale();
  if (moment.locale) {
    moment.locale(
      (localeInfo[locale] && localeInfo[locale].momentLocale) || '',
    );
  }
  setIntl(locale);
}

export const LocaleContainer = props => {
  const [locale, setLocale] = React.useState(() => getLocale());
  const [intl, setContainerIntl] = React.useState(() => getIntl(locale, true));
  const defaultAntdLocale = {};
  const direcition = getDirection();
  return (
    <ConfigProvider
      direction={direcition}
      locale={
        (localeInfo[locale] && localeInfo[locale].antd) || defaultAntdLocale
      }
    >
      <RawIntlProvider value={intl}>{props.children}</RawIntlProvider>
    </ConfigProvider>
  );
};
