use super::menus::{add_node_modal_ui, create_node};
use super::selection::EntitySelection;
use super::tree::TreeCache;
use super::undo::UndoHistory;
use nightshade::prelude::*;

pub fn render_add_node_modal(
    selection: &mut EntitySelection,
    undo_history: &mut UndoHistory,
    add_node_open: &mut bool,
    add_node_search: &mut String,
    tree_cache: &mut TreeCache,
    world: &mut World,
    ui_context: &egui::Context,
) -> bool {
    let modal_result = add_node_modal_ui(ui_context, add_node_open, add_node_search);
    if modal_result.close_modal {
        *add_node_open = false;
    }
    if let Some(node_type) = modal_result.node_to_create {
        let new_entity = create_node(world, undo_history, node_type, None);
        selection.set_single(new_entity);
        tree_cache.mark_dirty();
        return true;
    }
    false
}
