//! el-ui-compiler CLI
//!
//! Usage:
//!   el-ui-compiler <input.el> [-o <output.js>]

use std::fs;
use std::path::PathBuf;

fn main() {
    let args: Vec<String> = std::env::args().collect();

    if args.len() < 2 {
        eprintln!("Usage: el-ui-compiler <input.el> [-o output.js]");
        std::process::exit(1);
    }

    let input = PathBuf::from(&args[1]);
    let output = if args.len() >= 4 && args[2] == "-o" {
        PathBuf::from(&args[3])
    } else {
        input.with_extension("js")
    };

    let source = match fs::read_to_string(&input) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("Error reading {}: {}", input.display(), e);
            std::process::exit(1);
        }
    };

    let compiler = el_ui_compiler::Compiler::new();
    match compiler.compile_component(&source) {
        Ok(js) => {
            if let Err(e) = fs::write(&output, js) {
                eprintln!("Error writing {}: {}", output.display(), e);
                std::process::exit(1);
            }
            println!("Compiled {} -> {}", input.display(), output.display());
        }
        Err(e) => {
            eprintln!("Compile error: {}", e);
            std::process::exit(1);
        }
    }
}
