import {
  TranslationLang,
  TranslationData,
  TranslationWithNS,
} from '../ui/definition';

export const addLangsTranslations = ({
  i18n,
  langs,
  namespace,
  data,
}: {
  i18n: any;
  langs: TranslationLang[];
  namespace: string;
  data: TranslationData;
}) => {
  langs.forEach((lang) => {
    i18n.addResourceBundle(lang, namespace, data[lang]);
  });
};

export const addTranslations = ({
  i18n,
  translation,
  translations,
  langs = [TranslationLang.En],
}: {
  i18n: any;
  translation?: TranslationWithNS;
  translations?: TranslationWithNS[];
  langs?: TranslationLang[];
}) => {
  if (translation) {
    const { namespace, data } = translation;
    i18n.addResourceBundle(TranslationLang.En, namespace, data.en);
    addLangsTranslations({ i18n, langs, namespace, data });
  } else {
    (translations || []).forEach(({ namespace, data }) => {
      addLangsTranslations({ i18n, langs, namespace, data });
    });
  }
};
