use alloc::string::String;
use alloc::vec::Vec;
use crate::query::{adj_ast, match_ast_d};
use crate::syntax::builders::switch_section::build_switch_section_node;
use crate::syntax::readers::constants::C_SHARP_STATEMENT;
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::{CodeGraph, NodeId};
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let case_expr = match_ast_d(graph, n_id, "case_switch_label", Some(1)).map_or_else(
|| String::from("Default"),
|case_id| node_to_str(graph, case_id),
);
let execution_ids: Vec<NodeId> = adj_ast(graph, n_id, Some(1), &[])
.into_iter()
.filter(|c_id| {
graph
.label_type(*c_id)
.is_some_and(|kind| C_SHARP_STATEMENT.contains(&kind) && kind != "break_statement")
})
.collect();
build_switch_section_node(args, n_id, case_expr, &execution_ids).map(Some)
}