package helpers

type OrderedMap[K comparable, V any] struct {
	keys []K
	data map[K]V
}

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V] {
	return &OrderedMap[K, V]{
		keys: make([]K, 0),
		data: make(map[K]V),
	}
}

func (om *OrderedMap[K, V]) Set(key K, value V) {
	if _, exists := om.data[key]; !exists {
		om.keys = append(om.keys, key)
	}
	om.data[key] = value
}

func (om *OrderedMap[K, V]) Range(fn func(K, V) bool) {
	for _, key := range om.keys {
		if !fn(key, om.data[key]) {
			break
		}
	}
}

func (om *OrderedMap[K, V]) ToSlice() []V {
	result := make([]V, 0, len(om.keys))
	for _, key := range om.keys {
		result = append(result, om.data[key])
	}
	return result
}
