namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;

/// <summary>
///     Converts raw string values into typed values based on the current value's runtime type.
///     Ported from the CLI tool's OpcUaHelper.ConvertValue.
/// </summary>
public static class ValueConverter
{
    /// <summary>
    ///     Converts a raw string value into the runtime type expected by the target node.
    /// </summary>
    /// <param name="rawValue">The raw string supplied by the user.</param>
    /// <param name="currentValue">The current node value used to infer the target type. May be null.</param>
    /// <returns>A typed value suitable for an OPC UA write request.</returns>
    public static object ConvertValue(string rawValue, object? currentValue)
    {
        return currentValue switch
        {
            bool => bool.Parse(rawValue),
            byte => byte.Parse(rawValue),
            short => short.Parse(rawValue),
            ushort => ushort.Parse(rawValue),
            int => int.Parse(rawValue),
            uint => uint.Parse(rawValue),
            long => long.Parse(rawValue),
            ulong => ulong.Parse(rawValue),
            float => float.Parse(rawValue),
            double => double.Parse(rawValue),
            _ => rawValue
        };
    }
}