using System.Collections;

namespace ScadaLink.Commons.Types;

/// <summary>
/// Formats attribute values for display. Handles scalar types directly
/// and converts arrays/collections to comma-separated strings.
/// </summary>
public static class ValueFormatter
{
    /// <summary>
    /// Formats a value for display as a string. Returns the value's natural
    /// string representation for scalars, and comma-separated elements for
    /// array/collection types.
    /// </summary>
    public static string FormatDisplayValue(object? value)
    {
        if (value is null) return "";
        if (value is string s) return s;
        if (value is IFormattable) return value.ToString() ?? "";

        if (value is IEnumerable enumerable)
        {
            return string.Join(",", enumerable.Cast<object?>().Select(e => e?.ToString() ?? ""));
        }

        return value.ToString() ?? "";
    }
}
