package d2graph

import 

const (
	// MaxGridDimension bounds each axis of an explicitly dimensioned grid. In
	// addition to layout work, this limits the coordinate span contributed by
	// empty cells before output-specific resource limits run.
	MaxGridDimension = 10_000
	// MaxGridCells bounds the capacity of an explicitly dimensioned grid. Grid
	// layout retains empty cells so that diagrams can add objects in later boards,
	// which makes rows*columns the relevant amount of user-controlled work even
	// when the current board contains few objects.
	MaxGridCells = 1_000_000
)

// GridCapacity returns rows*columns when both axes and their product are
// bounded. The division check keeps the multiplication from overflowing an int.
func (,  int) (int, error) {
	if  <= 0 ||  <= 0 {
		return 0, fmt.Errorf("grid dimensions must be positive, got %d by %d", , )
	}
	if  > MaxGridDimension ||  > MaxGridDimension {
		return 0, fmt.Errorf("grid dimensions %d by %d exceed the maximum of %d rows or columns", , , MaxGridDimension)
	}
	if  > MaxGridCells/ {
		return 0, fmt.Errorf("grid dimensions %d by %d exceed the limit of %d cells", , , MaxGridCells)
	}
	return  * , nil
}

func ( *Object) () bool {
	return  != nil &&
		(.GridRows != nil || .GridColumns != nil)
}

func ( *Object) () *Object {
	if  == nil {
		return nil
	}
	if .IsGridDiagram() {
		return 
	}
	return .Parent.()
}

func ( *Object) () *Object {
	if  == nil {
		return nil
	}
	// grid cells can be a nested grid diagram
	if .Parent.IsGridDiagram() {
		return 
	}
	return .Parent.()
}

// TopGridDiagram returns the least nested (outermost) grid diagram
func ( *Object) () *Object {
	if  == nil {
		return nil
	}
	var  *Object
	if .IsGridDiagram() {
		 = 
	}
	 := .Parent
	for  != nil {
		if .IsGridDiagram() {
			 = 
		}
		 = .Parent
	}
	return 
}