from blends.models import (
    NId,
)
from blends.query import (
    adj_ast,
    get_nodes_by_path,
)
from blends.syntax.builders.method_declaration import (
    DirectChildren,
    ListChildren,
    build_method_declaration_node,
)
from blends.syntax.models import (
    SyntaxGraphArgs,
)


def reader(args: SyntaxGraphArgs) -> NId:
    graph = args.ast_graph
    n_attrs = graph.nodes[args.n_id]
    children = adj_ast(args.ast_graph, args.n_id)
    body_id = children[-1] if children else None

    direct: DirectChildren = {}
    list_children: ListChildren = {}
    if body_id:
        direct["block_id"] = body_id
    if param_id := n_attrs.get("label_field_parameters"):
        direct["parameters_id"] = param_id
    elif condition_nodes := list(get_nodes_by_path(graph, args.n_id, "bindings", "binding")):
        list_children["parameters_ids"] = condition_nodes

    return build_method_declaration_node(args, "LambdaExpression", direct, list_children)
