//! Zoxide integration.
//!
//! When `[zoxide] enabled = true` is set in the global config, `limb add`
//! registers the new worktree path with `zoxide` and `limb remove`
//! unregisters it. Both calls are best-effort: a missing `zoxide` binary
//! or a non-zero exit logs a warning to stderr but never fails the
//! underlying worktree operation.

use std::path::Path;
use std::process::Command;

/// Adds `path` to zoxide's database via `zoxide add <path>`.
pub fn register(path: &Path) {
    run("add", path);
}

/// Removes `path` from zoxide's database via `zoxide remove <path>`.
pub fn unregister(path: &Path) {
    run("remove", path);
}

fn run(subcommand: &str, path: &Path) {
    let result = Command::new("zoxide")
        .arg(subcommand)
        .arg(path)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status();
    match result {
        Ok(status) if status.success() => {}
        Ok(status) => {
            eprintln!(
                "warning: zoxide {subcommand} {} exited {}",
                path.display(),
                status.code().unwrap_or(-1)
            );
        }
        Err(e) => {
            eprintln!(
                "warning: failed to run zoxide {subcommand} {}: {e}",
                path.display()
            );
        }
    }
}
