package main

import (
	"context"
	"fmt"

	selectionqueries "visualizador_instanciados/backend_go/selection_queries"
)

func runSelectionQuery(
	ctx context.Context,
	sparql *AnzoGraphClient,
	snapshot GraphResponse,
	queryID string,
	selectedIDs []uint32,
	includeBNodes bool,
) ([]uint32, error) {
	def, ok := selectionqueries.Get(queryID)
	if !ok {
		return nil, fmt.Errorf("unknown query_id: %s", queryID)
	}

	idToNode := make(map[uint32]selectionqueries.NodeRef, len(snapshot.Nodes))
	keyToID := make(map[string]uint32, len(snapshot.Nodes))
	for _, n := range snapshot.Nodes {
		nr := selectionqueries.NodeRef{ID: n.ID, TermType: n.TermType, IRI: n.IRI}
		idToNode[n.ID] = nr
		keyToID[n.TermType+"\x00"+n.IRI] = n.ID
	}

	return def.Run(ctx, sparql, selectionqueries.Index{IDToNode: idToNode, KeyToID: keyToID}, selectedIDs, includeBNodes)
}
