/**
 * Tests for js/i18n.js
 */

import { test, describe, assertEquals, assertTrue } from './test-runner.js'
import { i18n, t } from '../js/i18n.js'

describe('i18n.getNestedValue', () => {
    const obj = {
        home: {
            title: 'Welcome',
            nested: {
                deep: 'Deep Value'
            }
        },
        simple: 'Simple Value'
    }

    test('gets top-level value', () => {
        assertEquals(i18n.getNestedValue(obj, 'simple'), 'Simple Value')
    })

    test('gets nested value', () => {
        assertEquals(i18n.getNestedValue(obj, 'home.title'), 'Welcome')
    })

    test('gets deeply nested value', () => {
        assertEquals(i18n.getNestedValue(obj, 'home.nested.deep'), 'Deep Value')
    })

    test('returns undefined for missing key', () => {
        assertEquals(i18n.getNestedValue(obj, 'missing'), undefined)
    })

    test('returns undefined for missing nested key', () => {
        assertEquals(i18n.getNestedValue(obj, 'home.missing'), undefined)
    })
})

describe('i18n.t (translation)', () => {
    // Note: These tests require translations to be loaded
    // They test the parameter interpolation logic

    test('returns key if translation missing', () => {
        const result = i18n.t('this.key.does.not.exist')
        assertEquals(result, 'this.key.does.not.exist')
    })
})

describe('i18n.getLocale', () => {
    test('returns current locale', () => {
        const locale = i18n.getLocale()
        assertTrue(['de', 'en', 'fr'].includes(locale))
    })
})

describe('i18n.getSupportedLocales', () => {
    test('returns array of supported locales', () => {
        const locales = i18n.getSupportedLocales()
        assertTrue(Array.isArray(locales))
        assertTrue(locales.includes('de'))
        assertTrue(locales.includes('en'))
        assertTrue(locales.includes('fr'))
    })
})

describe('i18n.getLocaleDisplayName', () => {
    test('returns Deutsch for de', () => {
        assertEquals(i18n.getLocaleDisplayName('de'), 'Deutsch')
    })

    test('returns English for en', () => {
        assertEquals(i18n.getLocaleDisplayName('en'), 'English')
    })

    test('returns Français for fr', () => {
        assertEquals(i18n.getLocaleDisplayName('fr'), 'Français')
    })

    test('returns locale code for unknown locale', () => {
        assertEquals(i18n.getLocaleDisplayName('xx'), 'xx')
    })
})

describe('i18n.subscribe', () => {
    test('returns unsubscribe function', () => {
        const unsubscribe = i18n.subscribe(() => {})
        assertEquals(typeof unsubscribe, 'function')
        unsubscribe()
    })
})
