using System;
using System.Security.Cryptography;
using System.Text;
namespace ZB.MOM.WW.MxGateway.Worker.MxAccess;
///
/// Derives a deterministic synthetic from an alarm
/// reference for the subtag-provider fallback path, which has no native
/// MxAccess alarm GUID. Hashing the reference yields a stable identity so
/// repeated transitions for the same alarm reference correlate downstream
/// (acknowledge, snapshot, OPC UA mapping) without an alarmmgr-supplied GUID.
///
public static class SyntheticAlarmGuid
{
///
/// Produces a stable for the given alarm reference.
/// The same reference always maps to the same GUID; distinct references
/// map to distinct GUIDs with overwhelming probability.
///
///
/// The fully-qualified alarm reference (for example
/// "Galaxy!Area.Tag.HiHi"). Treated as UTF-8 bytes.
///
/// A deterministic, non-empty GUID derived from the reference.
///
/// Thrown when is .
///
public static Guid ForReference(string reference)
{
if (reference is null) throw new ArgumentNullException(nameof(reference));
byte[] bytes = Encoding.UTF8.GetBytes(reference);
// MD5 is used purely for a stable, non-cryptographic identity mapping
// (reference -> 16-byte GUID), never for security. Its 128-bit output
// fits a GUID exactly, which is why it is preferred here.
using MD5 md5 = MD5.Create();
byte[] hash = md5.ComputeHash(bytes);
return new Guid(hash);
}
}