using System.Collections.ObjectModel;

namespace File_Folder_Helper.Helpers;

internal static class HelperDirectory
{

    internal static ReadOnlyCollection<string> GetDirectoryNames(string directory)
    {
        List<string> results = [];
        string? fileName;
        string? checkDirectory = directory;
        string? pathRoot = Path.GetPathRoot(directory);
        string extension = Path.GetExtension(directory);
        if (string.IsNullOrEmpty(pathRoot))
            throw new NullReferenceException(nameof(pathRoot));
        if (Directory.Exists(directory))
        {
            fileName = Path.GetFileName(directory);
            if (!string.IsNullOrEmpty(fileName))
                results.Add(fileName);
        }
        else if ((string.IsNullOrEmpty(extension) || extension.Length > 3) && !File.Exists(directory))
        {
            fileName = Path.GetFileName(directory);
            if (!string.IsNullOrEmpty(fileName))
                results.Add(fileName);
        }
        for (int i = 0; i < int.MaxValue; i++)
        {
            checkDirectory = Path.GetDirectoryName(checkDirectory);
            if (string.IsNullOrEmpty(checkDirectory) || checkDirectory == pathRoot)
                break;
            fileName = Path.GetFileName(checkDirectory);
            if (string.IsNullOrEmpty(fileName))
                continue;
            results.Add(fileName);
        }
        results.Add(pathRoot);
        results.Reverse();
        return new(results);
    }

}