#!/usr/bin/env python3
import os

import requests
import sys

PROJECT_IDENTIFIER = 'sponge-docs'
CROWDIN_API_KEY = os.environ['CROWDIN_API_KEY']

# Load language mappings
r = requests.get('https://api.crowdin.com/api/supported-languages?json')
r.raise_for_status()

# Map Crowdin codes to locale codes
languages = {lang['crowdin_code']: lang['locale'].replace('-', '_') for lang in r.json()}

r = requests.get('https://api.crowdin.com/api/project/%s/status' % PROJECT_IDENTIFIER, params={
    'key': CROWDIN_API_KEY,
    'json': True
})
r.raise_for_status()

locales = []
for language in r.json():
    # Include only languages with at least 5% progress
    if language['translated_progress'] >= 5 or language['code'].startswith('en-'):
        locales.append(languages[language['code']])
    else:
        print("Skipping %s with %d%% completion" % (language['name'], language['translated_progress']), file=sys.stderr)

# Sort locales
locales.sort()

# Add special In-Context Localization language
locales.append('translate')

# Print result (locales separated by space)
print(' '.join(locales))
