using System.Reflection;
using System.Text;

namespace JSMR.Tests.Utilities;

public static class ResourceHelper
{
    /// <summary>
    /// Reads an embedded resource from the calling assembly.
    /// </summary>
    /// <param name="resourceName">The full resource name, e.g. "MyNamespace.Folder.sample.json".</param>
    public static async Task<string> ReadAsync(string resourceName)
    {
        Assembly assmbly = Assembly.GetExecutingAssembly();

        using Stream? stream = assmbly.GetManifestResourceStream(resourceName)
            ?? throw new FileNotFoundException($"Resource '{resourceName}' not found.");

        using StreamReader reader = new(stream, Encoding.UTF8);

        return await reader.ReadToEndAsync();
    }
}