// Copyright (c) 2010, 2020 Kiel University and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0.
//
// SPDX-License-Identifier: EPL-2.0

package orthogonal

// DependencyType is ELK 0.8.1's HyperEdgeSegmentDependency.DependencyType.
type DependencyType uint8

const (
	RegularDependency DependencyType = iota
	CriticalDependency
)

const criticalDependencyWeight = 1

// HyperEdgeSegmentDependency says that Source wants a lower routing slot than
// Target. Critical dependencies prevent overlaps; regular dependencies carry
// the crossing/conflict cost of ignoring them.
type HyperEdgeSegmentDependency struct {
	Type   DependencyType
	Source *HyperEdgeSegment
	Target *HyperEdgeSegment
	Weight int
}

func (,  *HyperEdgeSegment,  int) *HyperEdgeSegmentDependency {
	return addDependency(RegularDependency, , , )
}

func (,  *HyperEdgeSegment) *HyperEdgeSegmentDependency {
	return addDependency(CriticalDependency, , , criticalDependencyWeight)
}

func addDependency( DependencyType, ,  *HyperEdgeSegment,  int) *HyperEdgeSegmentDependency {
	 := &HyperEdgeSegmentDependency{Type: , Weight: }
	.setSource()
	.setTarget()
	return 
}

func ( *HyperEdgeSegmentDependency) () {
	.setSource(nil)
	.setTarget(nil)
}

func ( *HyperEdgeSegmentDependency) () {
	,  := .Source, .Target
	.setSource()
	.setTarget()
}

func ( *HyperEdgeSegmentDependency) ( *HyperEdgeSegment) {
	if .Source != nil {
		.Source.OutgoingDependencies = removeDependency(.Source.OutgoingDependencies, )
	}
	.Source = 
	if  != nil {
		.OutgoingDependencies = append(.OutgoingDependencies, )
	}
}

func ( *HyperEdgeSegmentDependency) ( *HyperEdgeSegment) {
	if .Target != nil {
		.Target.IncomingDependencies = removeDependency(.Target.IncomingDependencies, )
	}
	.Target = 
	if  != nil {
		.IncomingDependencies = append(.IncomingDependencies, )
	}
}

func removeDependency( []*HyperEdgeSegmentDependency,  *HyperEdgeSegmentDependency) []*HyperEdgeSegmentDependency {
	for ,  := range  {
		if  ==  {
			copy([:], [+1:])
			return [:len()-1]
		}
	}
	return 
}