from blends.models import (
    NId,
)
from blends.query import (
    match_ast,
)
from blends.syntax.builders.while_statement import (
    build_while_statement_node,
)
from blends.syntax.models import (
    SyntaxGraphArgs,
)


def reader(args: SyntaxGraphArgs) -> NId:
    graph = args.ast_graph
    n_attrs = graph.nodes[args.n_id]

    body_id = n_attrs["label_field_body"]
    if graph.nodes[body_id]["label_type"] == "expression_statement":
        body_id = match_ast(graph, body_id)["__0__"]
    if graph.nodes[body_id]["label_type"] == "parenthesized_expression":
        body_id = match_ast(graph, body_id)["__1__"]

    cond_id = n_attrs["label_field_condition"]
    if graph.nodes[cond_id]["label_type"] == "parenthesized_expression":
        cond_id = match_ast(graph, cond_id)["__1__"]

    return build_while_statement_node(args, body_id, cond_id)
