using System;
using System.Reflection;

namespace StellaOps.Feedser.Exporter.Json;

public static class ExporterVersion
{
    public static string GetVersion(Type anchor)
    {
        ArgumentNullException.ThrowIfNull(anchor);
        var assembly = anchor.Assembly;

        var informational = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
        if (!string.IsNullOrWhiteSpace(informational))
        {
            return informational;
        }

        var fileVersion = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version;
        if (!string.IsNullOrWhiteSpace(fileVersion))
        {
            return fileVersion!;
        }

        var version = assembly.GetName().Version;
        return version?.ToString() ?? "0.0.0";
    }
}
