use alloc::borrow::ToOwned;
use alloc::format;
use alloc::string::String;
use crate::query::{label_text, match_ast_d, pred_ast};
use crate::syntax::builders::import_global::build_import_global_node;
use crate::syntax::fields::python::{aliased_import, import_from_statement};
use crate::syntax::{SyntaxGraphArgs, SyntaxGraphError};
use crate::utilities::text_nodes::node_to_str;
use crate::NodeId;
pub fn reader(
args: &mut SyntaxGraphArgs<'_>,
n_id: NodeId,
) -> Result<Option<NodeId>, SyntaxGraphError> {
let graph = args.ast_graph;
let mut alias: Option<String> = None;
let mut expression = node_to_str(graph, n_id);
for p_id in pred_ast(graph, n_id, Some(-1)) {
if let Ok(alias_id) = args.required_field_alt(p_id, aliased_import::ALIAS) {
alias = label_text(graph, alias_id).map(ToOwned::to_owned);
}
if let Ok(module_name_id) =
args.required_field_alt(p_id, import_from_statement::MODULE_NAME)
{
if match_ast_d(graph, p_id, "wildcard_import", None).is_none() {
let name = node_to_str(graph, module_name_id);
expression = format!("{name}.{expression}");
}
}
}
build_import_global_node(args, n_id, expression, &[], alias).map(Some)
}