Code/Core/Pin.cs
global using PinIndex = byte;

namespace Nodebox;

public struct Pin : IMeta {
    public Pin() { }
    public Pin(Type type, string name, string description = null) : this() {
        Type = type;
        Name = name == "" ? null : name;
        Description = description;
    }

    [JsonIgnore]
    public Type Type { get; set; }

    [JsonInclude]
    public SerializableType SerializableType {
        readonly get => Type;
        set => Type = value;
    }

    public string Name { get; set; }
    public string Description { get; set; }
    public bool HideInput { get; set; } = false;
    public Dictionary<string, object> Meta { get; private set; } = [];

    public static Pin New<T>(string name = null, string description = null) => new(typeof(T), name, description);

    public readonly Pin WithType(Type type) => this with { Type = type };
    public readonly Pin WithName(string name) => this with { Name = name };
    public readonly Pin WithDescription(string description) => this with { Description = description };
    public readonly Pin WithHideInput(bool hideInput) => this with { HideInput = hideInput };

    public readonly object CreateDefaultValue() {
        if (Type == typeof(Color)) {
            return new Color(0f, 0f, 0f);
        }

        return TypeLibrary.GetType(Type).CreateDefault();
    }

    public override readonly string ToString() => $"Pin<{DisplayInfo.ForGenericType(Type).Name}>(\"{Name}\")";
}