using Nostr.Client.Identifiers;
using Nostr.Client.Utils;

namespace NostrServices;

public class NostrBareIdentifier : NostrIdentifier
{
    public NostrBareIdentifier(string hrp, string hex)
    {
        if (hrp is not "npub" and not "note")
        {
            throw new InvalidOperationException($"{hrp} is not supported");
        }

        Hrp = hrp;
        Special = hex;
    }

    public override string ToBech32()
    {
        return NostrConverter.ToBech32(Special, Hrp)!;
    }

    public static NostrBareIdentifier? Parse(string bech32)
    {
        var hexBytes = NostrConverter.ToHexBytes(bech32, out var hrp);
        if (hrp is "npub" or "note")
        {
            return new NostrBareIdentifier(hrp, Convert.ToHexString(hexBytes!).ToLower());
        }

        return default;
    }

    public override string Hrp { get; }
    public override string Special { get; }
}
