using IWS5.Importer.Abstractions.Enums;

namespace IWS5.Importer.Worker.Validation;

public static class SupportedExtensionValidator
{
    public static void ValidateConfigExtensions(IEnumerable<string> configExtensions)
    {
        var validExtensions = Enum.GetNames(typeof(SupportedExtension))
            .Select(e => e.ToLowerInvariant())
            .ToHashSet();

        var invalid = configExtensions
            .Select(e => e.Trim().ToLowerInvariant())
            .Where(e => !validExtensions.Contains(e))
            .ToList();

        if (invalid.Any())
        {
            var list = string.Join(", ", invalid);
            throw new InvalidOperationException($"Invalid extension(s) in config: {list}");
        }
    }

    public static bool IsSupported(string extension)
    {
        if (string.IsNullOrWhiteSpace(extension)) return false;

        var ext = extension.TrimStart('.').ToLowerInvariant();
        return Enum.TryParse<SupportedExtension>(ext, true, out _);
    }
}
