import os

LangNameMap = {
   "af": "Afrikaans",
   "ar": "اللغة العربية",
   "ca": "Català",
   "cs": "	Čeština",
   "da": "Dansk",
   "de": "Deutsch",
   "el": "Ελληνικά",
   "en": "English",
   "es": "Español",
   "fi": "Suomi",
   "fr": "Français",
   "he": "עברית",
   "hu": "Magyar",
   "it": "Italiano",
   "ja": "日本語",
   "ko": "한국어",
   "lv": "Latviešu",
   "nl": "Nederlands",
   "no": "Norsk",
   "pl": "Polski",
   "pt": "Português (Portugal)",
   "pt-BR": "Português (Brasil)",
   "ro": "Română",
   "ru": "Русский",
   "sr-Cyrl": "Српски (Cyrillic)",
   "sv": "Svenska",
   "tr": "Türkçe",
   "uk": "Українська",
   "vi": "Tiếng Việt",
   "zh-Hans": "简体中文",
   "zh-Hant": "繁體中文",
}

print('Read  ../translations/*.json')
LanguageCodeList = [ code.replace('.json', '') for code in os.listdir('../translations/') ]

print('Read  ./l10n_configs/config_templete.ts')
templete = ""
with open('./l10n_configs/config_templete.ts', 'r', encoding='utf-8') as file:
   templete = file.read()

for LanguageCode in LanguageCodeList:
   content = templete

   content = content.replace("$LanguageName$", LangNameMap[LanguageCode], -1)

   content = content.replace("$LanguageCode$", LanguageCode, -1)

   content = content.replace("$LanguageCodeEscaped$", LanguageCode.replace('-', '_', -1), -1)

   if (LanguageCode == 'en'):
      content = content.replace("$PathToPage$", '', -1)
   else:
      content = content.replace("$PathToPage$", '/l10n/' + LanguageCode, -1)

   print("Write ./l10n_configs/{}.ts".format(LanguageCode))

   with open('./l10n_configs/{}.ts'.format(LanguageCode), 'w+', encoding='utf-8') as file:
      file.write('/* This file is automatically generated by "gen_configs.py" */\n')
      file.write(content)

print("Write ./locales_config.ts")

with open('./locales_config.ts', 'w+', encoding='utf-8') as file:
   file.write('/* This file is automatically generated by "gen_configs.py" */\n')
   file.write(r"import type { SiteLocaleConfig } from '@vuepress/shared'" + '\n')
   file.write(r"import type { LocaleConfig } from '@vuepress/shared'" + '\n')

   file.write('\n')

   for LanguageCode in LanguageCodeList:
      LanguageCodeEscaped = LanguageCode.replace('-', '_', -1)
      file.write(r"import { mainConfig_")
      file.write(LanguageCodeEscaped)
      file.write(r", defaultThemeConfig_")
      file.write(LanguageCodeEscaped)
      file.write(r" } from './l10n_configs/")
      file.write(LanguageCode)
      file.write(".js'\n")

   file.write('\n')

   file.write(r"export const mainConfig: SiteLocaleConfig = {" + '\n')
   for LanguageCode in LanguageCodeList:
      file.write(r"    '")
      if (LanguageCode != 'en'):
         file.write(r"/l10n/")
         file.write(LanguageCode)
      file.write(r"/': mainConfig_")
      file.write(LanguageCode.replace('-', '_', -1))
      file.write(",\n")
   file.write(r"}" + '\n')

   file.write('\n')

   file.write(r"export const defaultThemeConfig: LocaleConfig = {" + '\n')
   for LanguageCode in LanguageCodeList:
      file.write(r"    '")
      if (LanguageCode != 'en'):
         file.write(r"/l10n/")
         file.write(LanguageCode)
      file.write(r"/': defaultThemeConfig_")
      file.write(LanguageCode.replace('-', '_', -1))
      file.write(",\n")
   file.write(r"}" + '\n')

print("** Done! **")
