use serde_json;
use crate::command_init::fetch::Fetch;
use crate::shared::cli_error::{CliError, CliResult};
use crate::shared::rust_ui_client::RustUIClient;
const LABEL: &str = "label";
pub struct UserInput {}
impl UserInput {
pub async fn handle_index_styles() -> CliResult<()> {
let styles_index_result = Fetch::from_url(&RustUIClient::styles_index_url()).await;
if let Ok(styles_index) = styles_index_result {
let vec_styles = serde_json::from_str::<Vec<serde_json::Value>>(&styles_index).map_err(|e| {
CliError::malformed_registry(&format!("Failed to parse styles index JSON: {e}"))
})?;
ask_user_choose_style(vec_styles)?
}
Ok(())
}
}
fn ask_user_choose_style(vec_styles: Vec<serde_json::Value>) -> CliResult<()> {
for style in &vec_styles {
if let Some(label) = style.get(LABEL)
&& label.as_str() == Some("Default")
{
println!("🎨 Automatically selecting Default style (no user input required)");
println!("Selected style: {label}");
return Ok(());
}
}
if let Some(first_style) = vec_styles.first()
&& let Some(label) = first_style.get(LABEL)
{
println!("🎨 No Default style found, automatically selecting first available style: {label}");
return Ok(());
}
Err(CliError::validation("No styles available in registry"))
}