use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use crate::query::{adj_ast, match_ast, match_ast_d};
use crate::syntax::builders::switch_section::build_switch_section_node;
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, "switch_label", None)
.and_then(|case_id| {
match_ast(graph, case_id, &["case"], None)
.get("__0__")
.copied()
.flatten()
})
.map_or_else(
|| "Default".to_owned(),
|case_expr_id| node_to_str(graph, case_expr_id),
);
let execution_ids: Vec<NodeId> = adj_ast(graph, n_id, None, &[])
.into_iter()
.filter(|c_id| {
matches!(
graph.label_type(*c_id),
Some(
"declaration"
| "expression_statement"
| "labeled_statement"
| "if_statement"
| "while_statement"
| "for_statement"
| "enhanced_for_statement"
| "block"
| "assert_statement"
| "do_statement"
| "return_statement"
| "yield_statement"
| "switch_expression"
| "synchronized_statement"
| "local_variable_declaration"
| "throw_statement"
| "try_statement"
| "try_with_resources_statement"
)
)
})
.collect();
build_switch_section_node(args, n_id, case_expr, &execution_ids).map(Some)
}