import I18n from 'i18n-js'
import moment from 'moment'
import { uni2zg } from 'rabbit-node'

import 'moment/locale/th'
import 'moment/locale/my'
import 'moment/locale/km'

import ReactNative from 'react-native'
// Import all locales
import en from 'src/translations/en.json'
import th from 'src/translations/th.json'
import my from 'src/translations/my.json'
import km from 'src/translations/km.json'

I18n.defaultLocale = 'en'
I18n.fallbacks = true
I18n.translations = { th, en, my, km }
const currentLocale = I18n.currentLocale()
// // Is it a RTL language?
export const isRTL = currentLocale.indexOf('th') === 0 || currentLocale.indexOf('en-US') === 0
// Allow RTL alignment in RTL languages
ReactNative.I18nManager.allowRTL(isRTL)

// The method we'll use instead of a regular string
export function t (name, params = {}) {
  let text = I18n.t(name, params)

  // แปลงเป็น Zawgyi กรณีเป็นภาษาพม่า
  if (I18n.locale.startsWith('my')) {
    text = uni2zg(text)
  }

  return text
}

export function locale () {
  return I18n.currentLocale()
}

export const switchLanguage = (lang, component = null) => {
  I18n.locale = lang
  moment.locale(lang)
  if (component)
    component.forceUpdate()
}
