from blends.models import (
    NId,
)
from blends.query import (
    get_nodes_by_path,
    match_ast_d,
)
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]
    body_id = (
        match_ast_d(args.ast_graph, args.n_id, "block")
        or match_ast_d(args.ast_graph, args.n_id, "infix_expression")
        or match_ast_d(args.ast_graph, args.n_id, "indented_block")
    )

    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)
