package click.kamil.file_combine.common;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FileUtils {
    public static List<Path> findFilesWithExtension(Set<Path> startDirs, String extension) throws RuntimeException {
        try (Stream<Path> paths = startDirs.stream()
                .flatMap(startDir -> {
                    try {
                        return Files.walk(startDir);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })) {
            return paths
                    .filter(p -> Files.isRegularFile(p) && p.toString().endsWith(extension))
                    .collect(Collectors.toList());
        }
    }
}
