use crate::colors::get_colors;
use crate::config::get_config;
use crate::load_data::load_data_json;
use crate::requires::resolve_requires;
use crate::utils::{package_exists, prepare_packages};

/// Prints the transitive dependency tree for each requested package.
pub fn deps(packages: &[String]) {
    let packages = package_exists(packages);
    let packages = prepare_packages(&packages);

    let cfg = get_config();
    let color = get_colors(cfg.colors);

    let repo = load_data_json();

    println!(
        "{}Dependencies for package(s):{}\n",
        color.bold, color.reset
    );

    for pkg in &packages {
        if let Some(pkg_data) = repo.get(pkg) {
            println!("{}{}-{}:{}", color.cyan, pkg, pkg_data.version, color.reset);
        }

        let deps = resolve_requires(pkg, &repo);
        if deps.is_empty() {
            println!("  No dependencies found");
        } else {
            for dep in &deps {
                if let Some(pkg_data) = repo.get(dep) {
                    println!(
                        "  {} {}({}){}",
                        dep, color.yellow, pkg_data.version, color.reset
                    );
                }
            }
        }

        println!();
    }
}
