/**
 * i18n.js
 *
 * This will setup the i18n language files and locale data for your app.
 *
 */
const addLocaleData = require('react-intl').addLocaleData;
const enLocaleData = require('react-intl/locale-data/en');
const zhLocaleData = require('react-intl/locale-data/zh');
const jaLocaleData = require('react-intl/locale-data/ja');

const enTranslationMessages = require('./translations/en.json');
const zhTranslationMessages = require('./translations/zh.json');
const jaTranslationMessages = require('./translations/ja-JP.json');

export const DEFAULT_LOCALE = 'en';

export const appLocales = [
  'en',
  'zh',
  'ja-JP',
];

addLocaleData(enLocaleData);
addLocaleData(zhLocaleData);
addLocaleData(jaLocaleData);

export const formatTranslationMessages = (locale, messages) => {
  const defaultFormattedMessages = locale !== DEFAULT_LOCALE
    ? formatTranslationMessages(DEFAULT_LOCALE, enTranslationMessages)
    : {};
  return Object.keys(messages).reduce((formattedMessages, key) => {
    let message = messages[key];
    if (!message && locale !== DEFAULT_LOCALE) {
      message = defaultFormattedMessages[key];
    }
    return Object.assign(formattedMessages, { [key]: message });
  }, {});
};

export const translationMessages = {
  'en': formatTranslationMessages('en', enTranslationMessages),
  'zh': formatTranslationMessages('zh', zhTranslationMessages),
  'ja-JP': formatTranslationMessages('ja-JP', jaTranslationMessages),
};
