using System.Xml;

namespace aeqw89.tools.Publish;

internal sealed class PackageReference : Item {
    public PackageReference(XmlElement node) : base(node) { }
    
    // inside PackageReference
    public string? GetPackageVersion() {
        // Prefer attribute, then child <Version>
        var attr = GetAttribute("Version");
        if (!string.IsNullOrEmpty(attr)) return attr;

        var child = Node.SelectSingleNode("./Version") as XmlElement;
        return child?.InnerText;
    }

    public void SetPackageVersion(string? version) {
        if (Node.HasAttribute("Version") || (Node.SelectSingleNode("./Version") == null)) {
            // If attribute exists (or no child yet), use attribute
            SetAttribute("Version", version);
            return;
        }

        // Else write to existing child <Version>
        var child = (XmlElement)Node.SelectSingleNode("./Version")!;
        child.InnerText = version ?? string.Empty;
    }

}