use alloc::borrow::ToOwned;
use alloc::vec::Vec;
use crate::query::adj_ast;
use crate::syntax::builders::switch_section::build_switch_section_node;
use crate::syntax::fields::javascript::switch_case;
use crate::syntax::readers::constants::JAVASCRIPT_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_value = args
.required_field_alt(n_id, switch_case::VALUE)
.map_or_else(
|_| "Default".to_owned(),
|value_id| node_to_str(graph, value_id),
);
let execution_ids: Vec<NodeId> = adj_ast(graph, n_id, None, &[])
.into_iter()
.filter(|c_id| {
graph.label_type(*c_id).is_some_and(|label| {
JAVASCRIPT_STATEMENT.contains(&label) && label != "break_statement"
})
})
.collect();
build_switch_section_node(args, n_id, case_value, &execution_ids).map(Some)
}