namespace ClaudeDo.Localization;

public sealed class LocaleStore
{
    private readonly Dictionary<string, LocaleFile> _byCode;

    private LocaleStore(Dictionary<string, LocaleFile> byCode) => _byCode = byCode;

    public IReadOnlyList<LocaleFile> Available => _byCode.Values.ToList();

    public bool TryGet(string code, out LocaleFile? file) => _byCode.TryGetValue(code, out file);

    public static LocaleStore Load(string folder)
    {
        var byCode = new Dictionary<string, LocaleFile>(StringComparer.OrdinalIgnoreCase);
        if (Directory.Exists(folder))
        {
            foreach (var path in Directory.EnumerateFiles(folder, "*.json"))
            {
                try
                {
                    var file = LocaleJson.Parse(File.ReadAllText(path));
                    if (!string.IsNullOrWhiteSpace(file.Code))
                        byCode[file.Code] = file;
                }
                catch { /* skip malformed locale files */ }
            }
        }
        return new LocaleStore(byCode);
    }
}
