using CashLineERP.Contracts.Localization;

namespace CashLineERP.Application.Localization;

public sealed class LocalizationService : ILocalizationService
{
    private readonly ILocalizationResourceRepository _repository;

    public LocalizationService(ILocalizationResourceRepository repository)
    {
        _repository = repository;
    }

    public IReadOnlyCollection<SupportedCultureResponse> GetSupportedCultures() =>
        _repository.GetSupportedCultures()
            .Select(culture => new SupportedCultureResponse(
                culture.CultureCode,
                culture.DisplayName,
                culture.NativeName,
                culture.TextDirectionCode,
                culture.IsDefault))
            .ToArray();

    public LocalizationResourcePackResponse GetResourcePack(string resourceSetCode, string? cultureCode = null)
    {
        var pack = _repository.GetResourcePack(resourceSetCode, cultureCode);
        return new LocalizationResourcePackResponse(
            pack.ResourceSetCode,
            pack.RequestedCultureCode,
            pack.ResolvedCultureCode,
            pack.TextDirectionCode,
            pack.Resources.ToDictionary(item => item.Key, item => item.Value, StringComparer.OrdinalIgnoreCase));
    }
}
