from blends.models import (
    NId,
)
from blends.query import (
    adj_ast,
)
from blends.syntax.builders.string_literal import (
    build_string_literal_node,
)
from blends.syntax.models import (
    SyntaxGraphArgs,
)


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

    int_subs = [
        child
        for child in adj_ast(graph, args.n_id)
        if args.ast_graph.nodes[child]["label_type"] in {"template_substitution", "string_literal"}
    ]

    if len(int_subs) > 0:
        return build_string_literal_node(args, n_attrs["label_text"], iter(int_subs))
    return build_string_literal_node(args, n_attrs["label_text"])
