/// RFC 08 验收 example — Dirty::TREE 生效
///
/// 验证：
/// - `#[reactive(tree)]` 宏生成 getter/setter/update
/// - 字段变化时自动 invalidate_tree（触发 relayout + repaint）
/// - 按 `a` 添加项，按 `d` 删除最后一项，按 `q` 退出

use lv_tui::prelude::*;
use lv_tui::Component;

#[derive(Component)]
struct DynamicList {
    #[reactive(tree)]
    items: Vec<String>,
}

impl DynamicList {
fn new() -> Self {
        Self { items: vec![] }
    }
}

impl Component for DynamicList {
fn render(&self, cx: &mut RenderCx) {
        if self.items().is_empty() {
            cx.line("(empty)");
        } else {
            for item in self.items() {
                cx.line(format!("  - {}", item));
            }
        }
        cx.line("");
        cx.line("press a to add, d to delete last, q to quit");
    }
fn event(&mut self, event: &Event, cx: &mut EventCx) {
        if cx.phase() != lv_tui::event::EventPhase::Target { return; }
        if event.is_key(Key::Char('a')) {
            self.update_items(cx, |items| {
                items.push(format!("item #{}", items.len() + 1));
            });
        }
        if event.is_key(Key::Char('d')) {
            self.update_items(cx, |items| {
                items.pop();
            });
        }
        if event.is_key(Key::Char('q')) {
            cx.quit();
        }
    }
}

fn main() -> lv_tui::Result<()> {
    App::new(DynamicList::new()).run()
}
