using System.Globalization;
using System.Text.Json;

namespace GameIdeas.Resources;

public class TranslationService
{
	private readonly Dictionary<string, Dictionary<string, string>?> _translations = new();

	public void Initialize(Dictionary<string, string> translations)
	{
		foreach (var translation in translations)
		{
			var json = JsonSerializer.Deserialize<Dictionary<string, string>>(translation.Value);
			_translations[translation.Key] = json;
		}
	}

	public string Translate(string key, string? culture = "fr")
	{
		culture ??= CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
		if (_translations.TryGetValue(culture, out var value) && value?.TryGetValue(key, out var translate) == true)
			return translate;
		return key; // Fallback to key if translation is missing
	}
}