package layoutgraph

import (
	
	

	
)

const maxPositionedRoutePoints = 1_000_000

// ValidatePositionedGraphSelection checks the topology and positioned route
// geometry consumed by label placement and completed-layout evaluation.
// extraEdges may repeat graph edges once (the normal route-only selection),
// but neither input list may contain aliases within itself.
func ( context.Context,  string,  *Graph,  []*Edge) error {
	// Keep the central topology preflight first. Besides bounding hidden graph
	// state before candidate allocations, its graph-before-context error order
	// is part of the direct engine API contract.
	if  := validateEngineGraph(, , );  != nil {
		return 
	}
	if  == nil {
		return fmt.Errorf("TALA %s requires a context", )
	}
	if  := .Err();  != nil {
		return fmt.Errorf("%s: %w", , )
	}
	if  == nil {
		return fmt.Errorf("TALA %s requires a graph", )
	}
	if len(.Nodes) > limits.MaxEngineNodes {
		return fmt.Errorf("TALA %s node count exceeds limit %d", , limits.MaxEngineNodes)
	}
	if len(.Edges) > limits.MaxEngineEdges {
		return fmt.Errorf("TALA %s edge count exceeds limit %d", , limits.MaxEngineEdges)
	}
	if len() > limits.MaxEngineEdges {
		return fmt.Errorf("TALA %s requested edge count exceeds limit %d", , limits.MaxEngineEdges)
	}

	 := 0
	 := func() error {
		++
		if %64 != 0 {
			return nil
		}
		if  := .Err();  != nil {
			return fmt.Errorf("%s: %w", , )
		}
		return nil
	}
	for ,  := range .Nodes {
		if  := ();  != nil {
			return 
		}
		if  == nil {
			return fmt.Errorf("TALA %s graph node at index %d is nil", , )
		}
		if .TopLeft == nil {
			return fmt.Errorf("TALA %s graph node %d has no position", , .ID)
		}
		if .Cluster != nil && .Cluster.Vessel == nil {
			return fmt.Errorf("TALA %s graph node %d has a cluster without a vessel", , .ID)
		}
		if .Sequence != nil && .Sequence.Vessel == nil {
			return fmt.Errorf("TALA %s graph node %d has a sequence without a vessel", , .ID)
		}
	}

	 := make(map[*Edge]struct{}, len(.Edges))
	 := make(map[*Edge]struct{}, len())
	 := make(map[*Edge]struct{}, len(.Edges)+len())
	 := 0
	 := func( *Edge,  int,  string,  map[*Edge]struct{}) error {
		if  := ();  != nil {
			return 
		}
		if  == nil {
			return fmt.Errorf("TALA %s %s at index %d is nil", , , )
		}
		if ,  := [];  {
			return fmt.Errorf("TALA %s %s at index %d repeats edge %d", , , , .entityID())
		}
		[] = struct{}{}
		if ,  := [];  {
			return nil
		}
		[] = struct{}{}
		if .From == nil || .To == nil {
			return fmt.Errorf("TALA %s edge %d has missing endpoints", , .entityID())
		}
		if len(.Points) > maxPositionedRoutePoints- {
			return fmt.Errorf("TALA %s route point count exceeds limit %d", , maxPositionedRoutePoints)
		}
		 += len(.Points)
		for ,  := range .Points {
			if  := ();  != nil {
				return 
			}
			if  == nil {
				return fmt.Errorf("TALA %s edge %d route point at index %d is nil", , .entityID(), )
			}
		}
		if (.Label != nil || .SourceArrowheadLabel != nil || .TargetArrowheadLabel != nil) && len(.Points) < 2 {
			return fmt.Errorf("TALA %s labeled edge %d requires at least two route points", , .entityID())
		}
		return nil
	}
	for ,  := range .Edges {
		if  := (, , "graph edge", );  != nil {
			return 
		}
	}
	for ,  := range  {
		if  := (, , "requested edge", );  != nil {
			return 
		}
	}
	if  := .Err();  != nil {
		return fmt.Errorf("%s: %w", , )
	}
	return nil
}