using System.Collections.Concurrent;

namespace Sunfish.Web;

/// <summary>Recursos incrustados de la web (iconos GIF, favicon), servidos en /$sunfish/res/.</summary>
internal static class WebResources
{
    private static readonly ConcurrentDictionary<string, byte[]?> Cache = new(StringComparer.OrdinalIgnoreCase);

    public static byte[]? Get(string name) => Cache.GetOrAdd(name, Load);

    private static byte[]? Load(string name)
    {
        using var stream = typeof(WebResources).Assembly.GetManifestResourceStream("web/" + name);
        if (stream is null)
            return null;
        var ms = new MemoryStream();
        stream.CopyTo(ms);
        return ms.ToArray();
    }
}
