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

namespace CoverageFramework.Operator.Selector
{
	// TODO: 未使用
	public class XElementRangeSelectorAnyInRangePipe : IXElementRangeSelector
	{
		private readonly IXElementRangeSelector _next;
		private readonly IXElementRangeSelector _prev;

		public XElementRangeSelectorAnyInRangePipe(IXElementRangeSelector prev, IXElementRangeSelector next)
		{
			_prev = prev;
			_next = next;
		}

		#region IXElementRangeSelector メンバ

		public IEnumerable<IEnumerable<XElement>> Select(XElement root)
		{
			return _prev.Select(root)
				.Select(block_ =>
					block_.Select(e_ => _next.Select(e_)))
				.PickUpValidFirst(r_ => !r_.IsEmpty())
				.Planarize();

			//return _prev.Select(root)
			//    .SelectMany(block_ =>
			//        block_.Select(block_ => _next.Select(block_))
			//            .Where(r_ => !r_.IsEmpty())
			//            .Take(1))
			//    .Planarize();

			//foreach (var block in _prev.Select(root))
			//{
			//    // ブロック内の要素で、上手くいく１要素だけの結果を利用する
			//    foreach (var element in block)
			//    {
			//        var result = _next.Select(element);
			//        if (result.IsEmpty()) continue;

			//        yield return result;
			//        break;
			//    }
			//}
		}

		#endregion
	}
}