package app

// updateManager manages scheduled updates for UI components. It ensures that
// components are updated in an order respecting their depth in the UI
// hierarchy.
type updateManager struct {
	pending []map[Composer]int
}

// Add queues a component for an update and increments its associated counter by
// the given value. The component will be marked for update if its counter
// becomes greater than 0.
func ( *updateManager) ( Composer,  int) {
	 := int(.depth())
	if len(.pending) <=  {
		 := max(+1, 100)
		 := make([]map[Composer]int, )
		copy(, .pending)
		.pending = 
	}

	 := .pending[]
	if  == nil {
		 = make(map[Composer]int)
		.pending[] = 
	}
	[] += 
}

// Done removes the given component from the update queue, marking it as updated.
func ( *updateManager) ( Composer) {
	 := .depth()
	if len(.pending) <= int() {
		return
	}
	 := .pending[]
	delete(, )
}

// UpdateForEach iterates over all components queued for updates via the Add
// method, executing a specified function on each component with an associated
// counter greater than 0. After the function is invoked on a component, its
// entry is removed from the queue.
//
// This method ensures actions are taken only on components ready for an update
// (counter > 0) and maintains the queue's cleanliness by removing components
// once processed.
func ( *updateManager) ( func(Composer)) {
	for ,  := range .pending {
		for ,  := range  {
			if  > 0 {
				()
			}
			delete(, )
		}
	}
}