//! CLI binary for manual regeneration of `.huskies/source-map.json`.
//!
//! Usage: `source-map-regen [--project-root <path>]`
//!
//! Scans every tracked Rust and TypeScript file in the project via `git ls-files`,
//! extracts public item signatures, and writes a fresh sorted JSON map. The output
//! is byte-identical across runs on the same source tree (deterministic).
//!
//! The pre-commit gate (`script/check`) no longer calls this binary directly — map
//! regeneration is now inlined into the coder spawn path (`local_prompt.rs`) so every
//! agent session starts with a fresh snapshot. This binary is kept as an escape hatch
//! for manual out-of-band regeneration (e.g. after bulk refactors outside the pipeline).

use source_map_gen::regenerate_source_map;
use std::path::Path;

fn main() {
    let args: Vec<String> = std::env::args().collect();
    let root = parse_arg(&args, "--project-root").unwrap_or_else(|| ".".to_string());
    let root_path = Path::new(&root);
    let map_path = root_path.join(".huskies").join("source-map.json");

    if let Err(e) = regenerate_source_map(root_path, &map_path) {
        eprintln!("source-map-regen: {e}");
        std::process::exit(1);
    }
}

/// Parse a flag value from an argument list (e.g. `--flag value`).
fn parse_arg(args: &[String], flag: &str) -> Option<String> {
    args.windows(2).find(|w| w[0] == flag).map(|w| w[1].clone())
}
