package dagro

func runNestingGraph( *Graph) {
	 := addDummyNode(, "root", Attrs{}, "_root")
	 := treeDepths()
	 := 0
	for ,  := range  {
		if -1 >  {
			 =  - 1
		}
	}
	 := 2* + 1
	 := asAttrs(.Graph())
	["nestingRoot"] = 
	for ,  := range .Edges() {
		 := asAttrs(.Edge())
		["minlen"] = num(, "minlen") * float64()
	}
	 := sumWeights() + 1
	for ,  := range .Children() {
		nestingDFS(, , , , , , )
	}
	["nodeRankFactor"] = float64()
}

func nestingDFS( *Graph,  string,  int,  float64,  int,  map[string]int,  string) {
	 := .Children()
	if len() == 0 {
		if  !=  {
			.SetEdge(, , Attrs{"weight": float64(0), "minlen": float64()})
		}
		return
	}
	 := addBorderNode(, "_bt")
	 := addBorderNode(, "_bb")
	 := asAttrs(.Node())
	_ = .setParentKnownAcyclic(, )
	["borderTop"] = 
	_ = .setParentKnownAcyclic(, )
	["borderBottom"] = 
	for ,  := range  {
		(, , , , , , )
		 := asAttrs(.Node())
		,  := , 
		 := 2 * 
		if has(, "borderTop") {
			 = stringValue(, "borderTop")
			 = stringValue(, "borderBottom")
			 = 
		}
		 := 1
		if  ==  {
			 =  - [] + 1
		}
		.SetEdge(, , Attrs{"weight": , "minlen": float64(), "nestingEdge": true})
		.SetEdge(, , Attrs{"weight": , "minlen": float64(), "nestingEdge": true})
	}
	if ,  := .Parent(); ! {
		.SetEdge(, , Attrs{"weight": float64(0), "minlen": float64( + [])})
	}
}

func treeDepths( *Graph) map[string]int {
	 := map[string]int{}
	var  func(string, int)
	 = func( string,  int) {
		for ,  := range .Children() {
			(, +1)
		}
		[] = 
	}
	for ,  := range .Children() {
		(, 1)
	}
	return 
}

func sumWeights( *Graph) float64 {
	 := 0.0
	for ,  := range .Edges() {
		 += num(asAttrs(.Edge()), "weight")
	}
	return 
}

func cleanupNestingGraph( *Graph) {
	 := asAttrs(.Graph())
	 := stringValue(, "nestingRoot")
	.RemoveNode()
	delete(, "nestingRoot")
	for ,  := range .Edges() {
		if boolValue(asAttrs(.Edge()), "nestingEdge") {
			.RemoveEdge()
		}
	}
}