use crate::cli::{eval, ok, DEFAULT_PORT};
use anyhow::Result;
use clap::Parser;

#[derive(Parser)]
pub struct Args {
    /// Friendly name of the node to isolate.
    pub name: String,

    #[arg(long, default_value_t = 5)]
    pub timeout_secs: u64,

    #[arg(long, default_value_t = DEFAULT_PORT)]
    pub port: u16,
}

pub fn run(args: Args) -> Result<()> {
    let code = format!(
        "return window.adomStep ? window.adomStep.isolate({}) : {{ error: 'adomStep not loaded' }};",
        serde_json::to_string(&args.name).unwrap()
    );
    let result = eval::eval_snippet(&code, args.timeout_secs, args.port, true)?;
    ok(format!("isolated {}: {result}", args.name));
    Ok(())
}