﻿using System.Collections.Generic;
using Essentions.IO.Globbing.Nodes;

namespace Essentions.IO.Globbing
{
    internal static class GlobNodeRewriter
    {
        public static GlobNode Rewrite(IEnumerable<GlobNode> nodes)
        {
            return CreateLinkedList(RewriteSingleWildcards(nodes));
        }

        private static GlobNode CreateLinkedList(IEnumerable<GlobNode> nodes)
        {
            var result = new Stack<GlobNode>();
            var stack = new Stack<GlobNode>(nodes);
            var previous = stack.Pop();
            result.Push(previous);
            while (stack.Count > 0)
            {
                var current = stack.Pop();
                current.Next = previous;
                previous = current;
                result.Push(previous);
            }
            return result.Pop();
        }

        private static IEnumerable<GlobNode> RewriteSingleWildcards(IEnumerable<GlobNode> nodes)
        {
            foreach (var node in nodes)
            {
                var segmentNode = node as PathSegment;
                if (segmentNode?.Tokens.Count == 1)
                {
                    if (segmentNode.Tokens[0].Kind == GlobTokenKind.Wildcard)
                    {
                        yield return new WildcardSegment();
                        continue;
                    }
                }

                yield return node;
            }
        }
    }
}