//! Build-script for `rdom-tui` — generates camelCase alias methods
//! on `StyleDeclaration` (one per property in
//! `rdom_style::property_dispatch::property_names()`).
//!
//! Why a build script rather than a `macro_rules!` expansion: the
//! authoritative list of CSS properties lives in `rdom-style` as
//! a runtime function (called by the parser + CSSOM `length` / `item`
//! / `cssText` iteration). Re-typing those names in a macro
//! definition would be the duplication we removed in step 25.
//! Build-script reads the runtime list at compile time, no drift.
//!
//! Output file: `$OUT_DIR/cssom_aliases.rs`. Included from
//! `crates/rdom-tui/src/cssom/aliases.rs` inside an
//! `impl StyleDeclaration { … }` block.

use std::path::PathBuf;

fn main() {
    println!("cargo::rerun-if-changed=build.rs");

    let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR set by cargo"));
    let out_file = out_dir.join("cssom_aliases.rs");

    let mut generated = String::new();
    generated.push_str(
        "// AUTO-GENERATED by build.rs from \
         rdom_style::property_dispatch::property_names().\n\
         // DO NOT EDIT — regenerate by touching build.rs.\n\n\
         impl crate::cssom::StyleDeclaration {\n",
    );

    for &name in rdom_style::property_dispatch::property_names() {
        let method = snake_case(name);
        generated.push_str(&format!(
            "    /// `el.style.{name}` — CSS `{name}` property value\n\
             \x20   /// as a string (alias for `get_property_value({name:?})`).\n\
             \x20   #[inline]\n\
             \x20   #[must_use]\n\
             \x20   pub fn {method}(&self) -> String {{\n\
             \x20       self.get_property_value({name:?})\n\
             \x20   }}\n\n",
            name = name,
            method = method,
        ));
    }

    generated.push_str("}\n");

    std::fs::write(&out_file, generated)
        .unwrap_or_else(|e| panic!("write {}: {e}", out_file.display()));
}

/// Convert a CSS property name (`"background-color"`,
/// `"transition-timing-function"`, …) to a Rust snake_case method
/// name. Hyphens become underscores; everything else is already
/// ASCII-lowercase per the property table.
fn snake_case(name: &str) -> String {
    name.replace('-', "_")
}
