import { I18n, Scope, TranslateOptions } from "i18n-js";

import { translations } from './translations';
import AsyncStorage from '@react-native-async-storage/async-storage';

const localI18n = new I18n(translations);

localI18n.locale = 'fr';
localI18n.defaultLocale = 'fr';

AsyncStorage.getItem('storedLanguage').then(data => {
  if (data === null) {
    localI18n.locale = 'fr'
  } else {
    localI18n.locale = data
  }

}).catch((error) => console.log(error))

localI18n.enableFallback = true

type ScopedTranslationFunction = (
  scope: Scope,
  options?: Omit<TranslateOptions, 'scope'>
) => string;

const scoped = (key: string): ScopedTranslationFunction => {
  return (scope, options) => localI18n.t(scope, { ...options, scope: key });
};

const t: ScopedTranslationFunction = (scope, options?) => localI18n.t(scope, options)

export const i18n = { ...localI18n, localI18n, t, scoped };


