use crate::THEME;
use anyhow::{bail, Result};
use dialoguer::MultiSelect;
use libium::config::structs::Profile;
pub fn remove(profile: &mut Profile, mod_names: Vec<String>) -> Result<()> {
let names = profile
.mods
.iter()
.map(|mod_| &mod_.name)
.collect::<Vec<_>>();
let mut items_to_remove = if mod_names.is_empty() {
match MultiSelect::with_theme(&*THEME)
.with_prompt("Select mods to remove")
.items(&names)
.interact_opt()?
{
Some(items_to_remove) => items_to_remove,
None => return Ok(()), }
} else {
let mut items_to_remove = Vec::new();
for mod_name in mod_names {
if let Some(index) = names
.iter()
.position(|name| name.to_lowercase() == mod_name.to_lowercase())
{
items_to_remove.push(index);
} else {
bail!("A mod called {mod_name} is not present in this profile");
}
}
items_to_remove
};
items_to_remove.sort_unstable();
items_to_remove.reverse();
for index in items_to_remove {
profile.mods.swap_remove(index);
}
Ok(())
}