import * as React from 'react';
import moment from 'moment';
import store from 'store';
import { useIntl } from 'umi';
import { i18n } from './config';

export const languages = i18n ? i18n.languages.map((item) => item.key) : [];
export const defaultLanguage = i18n ? i18n.defaultLanguage : '';
export function getLocale() {
  return store.get('locale') || defaultLanguage;
}

export function setLocale(language: string) {
  if (getLocale() !== language) {
    moment.locale(language === 'zh-CN' ? 'zh-CN' : language);
    store.set('locale', language);
    window.location.reload();
  }
}

interface ITransProps {
  children: string;
}
const Trans: React.FC<ITransProps> = ({ children }: any) => {
  const intl = useIntl();
  return intl.formatMessage({ id: children });
};
