using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace MonoGame.FourEngine.Tiled
{
    internal static class XContainerExt
    {
        public static IEnumerable<XElement> Descendants(this XContainer xContainer, params XName[] names)
            => xContainer.Descendants()
            .Where(x => names.Contains(x.Name));

        public static XAttribute AttributeOrZero(this XElement xElement, XName name) => AttributeOrString(xElement, name, "0");
        public static XAttribute AttributeOrEmpty(this XElement xElement, XName name) => AttributeOrString(xElement, name, string.Empty);
        public static XAttribute AttributeOrString(this XElement xElement, XName name, string value)
            => xElement.Attributes(name).FirstOrDefault()
            ?? new XAttribute(name, value);

        public static XElement ElementOrNull(this XElement xElement, XName name) 
            => xElement.Elements(name).FirstOrDefault();
    }
}