package de.arxsilex.maptools.converter.desktop

import java.util.Locale
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

class LocalizationTest {
    @Test
    fun `all Android app languages have complete converter translations`() {
        val expected = LocalizationCatalog.keys(AppLanguage.ENGLISH)
        assertFalse(expected.isEmpty())
        AppLanguage.entries.forEach { language ->
            assertEquals(expected, LocalizationCatalog.keys(language), language.tag)
        }
    }

    @Test
    fun `translations preserve every numbered placeholder`() {
        val placeholder = Regex("\\{\\d+}")
        val english = LocalizationCatalog.strings(AppLanguage.ENGLISH)
        LocalizationCatalog.keys(AppLanguage.ENGLISH).forEach { key ->
            val expected = placeholder.findAll(english[key]).map { it.value }.toSet()
            AppLanguage.entries.forEach { language ->
                val actual = placeholder.findAll(LocalizationCatalog.strings(language)[key])
                    .map { it.value }
                    .toSet()
                assertEquals(expected, actual, "$key (${language.tag})")
            }
        }
    }

    @Test
    fun `system locale selects a supported language and otherwise English`() {
        assertEquals(AppLanguage.GERMAN, AppLanguage.systemDefault(Locale.GERMANY))
        assertEquals(AppLanguage.CHINESE_SIMPLIFIED, AppLanguage.systemDefault(Locale.forLanguageTag("zh-CN")))
        assertEquals(AppLanguage.ENGLISH, AppLanguage.systemDefault(Locale.forLanguageTag("da-DK")))
    }
}
