namespace View_by_Distance.Shared.Models.Stateless.Methods;

internal abstract class FileHolder
{

    internal static List<Models.FileHolder> GetFileHolders((string, string[])[] collection)
    {
        List<Models.FileHolder> results = new();
        foreach ((string _, string[] files) in collection)
            results.AddRange(files.Select(l => new Models.FileHolder(l)));
        return results;
    }

    internal static IEnumerable<Models.FileHolder> GetFileHolders(IEnumerable<(string, string)> collection)
    {
        foreach ((string _, string file) in collection)
            yield return new(file);
    }

    internal static IEnumerable<(string, string[])> GetFiles(string root, string searchPattern)
    {
        Stack<string> pending = new();
        pending.Push(root);
        while (pending.Count != 0)
        {
            string[]? next = null;
            string path = pending.Pop();
            try
            {
                next = Directory.GetFiles(path, searchPattern);
            }
            catch { }
            if (next is not null && next.Any())
                yield return new(path, next);
            try
            {
                next = Directory.GetDirectories(path);
                foreach (string subDirectory in next)
                    pending.Push(subDirectory);
            }
            catch { }
        }
    }

}