package dagro

func buildLayerGraph( *Graph,  int,  string) *Graph {
	var  []string
	for ,  := range .Nodes() {
		 := asAttrs(.Node())
		 := has(, "rank") && integer(, "rank") == 
		 := has(, "minRank") && has(, "maxRank") &&
			integer(, "minRank") <=  &&  <= integer(, "maxRank")
		if  ||  {
			 = append(, )
		}
	}
	return buildLayerGraphNodes(, , , )
}

func buildLayerGraphNodes( *Graph,  int,  string,  []string) *Graph {
	 := createOrderRootNode()
	 := 0
	switch  {
	case "inEdges":
		for ,  := range  {
			if  := .in[];  != nil {
				 += len(.items)
			}
		}
	case "outEdges":
		for ,  := range  {
			if  := .out[];  != nil {
				 += len(.items)
			}
		}
	default:
		panic("dagro: unsupported layer-graph relationship: " + )
	}
	 := newGraphWithCapacity(GraphOptions{Compound: true}, len()+1, ).SetGraph(Attrs{"root": })
	.SetDefaultNodeLabel(func( string) any { return .Node() })

	for ,  := range  {
		 := asAttrs(.Node())
		.SetNode()
		,  := .Parent()
		if ! ||  == "" {
			 = 
		}
		if  := .setParentKnownAcyclic(, );  != nil {
			panic()
		}

		var  []Edge
		if  == "inEdges" {
			 = .InEdges()
		} else {
			 = .OutEdges()
		}
		for ,  := range  {
			 := .V
			if  ==  {
				 = .W
			}
			 := 0.0
			if .HasEdge(, ) {
				 = num(asAttrs(.EdgeByArgs(, )), "weight")
			}
			 += num(asAttrs(.Edge()), "weight")
			.SetEdge(, , Attrs{"weight": })
		}

		if has(, "minRank") {
			.SetNode(, Attrs{
				"borderLeft":  orderBorderAt(["borderLeft"], ),
				"borderRight": orderBorderAt(["borderRight"], ),
			})
		}
	}

	return 
}

func createOrderRootNode( *Graph) string {
	for {
		 := .uniqueID("_root")
		if !.HasNode() {
			return 
		}
	}
}

func orderBorderAt( any,  int) string {
	switch borders := .(type) {
	case []string:
		if  >= 0 &&  < len() {
			return []
		}
	case []any:
		if  >= 0 &&  < len() {
			if ,  := [].(string);  {
				return 
			}
		}
	case map[int]string:
		return []
	}
	return ""
}