using System;

namespace ZB.MOM.WW.LmxProxy.Host.Domain
{
    /// <summary>
    ///     Type-aware equality comparison for WriteBatchAndWait flag matching.
    /// </summary>
    public static class TypedValueComparer
    {
        /// <summary>
        ///     Returns true if both values are the same type and equal.
        ///     Mismatched types are never equal.
        ///     Null equals null only.
        /// </summary>
        public new static bool Equals(object? a, object? b)
        {
            if (a == null && b == null) return true;
            if (a == null || b == null) return false;
            if (a.GetType() != b.GetType()) return false;

            if (a is Array arrA && b is Array arrB)
            {
                if (arrA.Length != arrB.Length) return false;
                for (int i = 0; i < arrA.Length; i++)
                {
                    if (!object.Equals(arrA.GetValue(i), arrB.GetValue(i)))
                        return false;
                }
                return true;
            }

            return object.Equals(a, b);
        }
    }
}
