#include "Directory.h"

std::vector<Path> Directory::getFilesWithExtension(const Path& path, const std::string& extension, bool recursive)
{
    std::vector<Path> paths;
    if (std::filesystem::is_directory(path))
    {
        for (const auto& entry : std::filesystem::directory_iterator(path))
        {
            if (std::filesystem::is_regular_file(entry) && entry.path().extension() == extension)
            {
                paths.push_back(entry.path());
            }
            else if(recursive && std::filesystem::is_directory(entry))
            {
                const auto child_paths = getFilesWithExtension(entry, extension, recursive);
                paths.insert(paths.end(), child_paths.begin(), child_paths.end());
            }
        }
    }
    return paths;
}

void Directory::createIfNotExisting(const Path& path)
{
    Path working_path;
    if (std::filesystem::is_directory(path))
    {
        working_path = path;
    }
    else
    {
        working_path = path.parent_path();
    }

    if (!std::filesystem::exists(working_path))
    {
        std::filesystem::create_directories(working_path);
    }
}
