use std::{
    fs,
    path::{Path, PathBuf},
};

#[allow(dead_code)] // false positive
pub(crate) fn is_noise(name: &str) -> bool {
    name.starts_with('.') || matches!(name, "target" | "node_modules" | "__pycache__")
}

/// Recursively collect all **file** paths under `dir`, stored relative to
/// `root`.  Used by search-replace for project-wide file enumeration.
#[allow(dead_code)] // false positive
pub(crate) fn collect_all_files(dir: &Path, root: &Path, out: &mut Vec<PathBuf>) {
    let Ok(rd) = fs::read_dir(dir) else { return };

    let mut entries: Vec<fs::DirEntry> = rd
        .filter_map(|e| e.ok())
        .filter(|e| {
            e.file_name()
                .to_str()
                .map(|n| !is_noise(n))
                .unwrap_or(false)
        })
        .collect();

    entries.sort_unstable_by_key(|a| a.file_name());

    for entry in entries {
        let path = entry.path();
        let is_dir = entry
            .file_type()
            .map(|ft| ft.is_dir())
            .unwrap_or_else(|_| path.is_dir());

        if is_dir {
            collect_all_files(&path, root, out);
        } else {
            let rel = path.strip_prefix(root).unwrap_or(&path).to_path_buf();
            out.push(rel);
        }
    }
}
