// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package simplelru

import (
	

	
)

// EvictCallback is used to get a callback when a cache entry is evicted
type EvictCallback[ comparable,  any] func(key , value )

// LRU implements a non-thread safe fixed size LRU cache
type LRU[ comparable,  any] struct {
	size      int
	evictList *internal.LruList[, ]
	items     map[]*internal.Entry[, ]
	onEvict   EvictCallback[, ]
}

// NewLRU constructs an LRU of the given size
func [ comparable,  any]( int,  EvictCallback[, ]) (*LRU[, ], error) {
	if  <= 0 {
		return nil, errors.New("must provide a positive size")
	}

	 := &LRU[, ]{
		size:      ,
		evictList: internal.NewList[, ](),
		items:     make(map[]*internal.Entry[, ]),
		onEvict:   ,
	}
	return , nil
}

// Purge is used to completely clear the cache.
func ( *LRU[, ]) () {
	for ,  := range .items {
		if .onEvict != nil {
			.onEvict(, .Value)
		}
		delete(.items, )
	}
	.evictList.Init()
}

// Add adds a value to the cache.  Returns true if an eviction occurred.
func ( *LRU[, ]) ( ,  ) ( bool) {
	// Check for existing item
	if ,  := .items[];  {
		.evictList.MoveToFront()
		.Value = 
		return false
	}

	// Add new item
	 := .evictList.PushFront(, )
	.items[] = 

	 := .evictList.Length() > .size
	// Verify size not exceeded
	if  {
		.removeOldest()
	}
	return 
}

// Get looks up a key's value from the cache.
func ( *LRU[, ]) ( ) ( ,  bool) {
	if ,  := .items[];  {
		.evictList.MoveToFront()
		return .Value, true
	}
	return
}

// Contains checks if a key is in the cache, without updating the recent-ness
// or deleting it for being stale.
func ( *LRU[, ]) ( ) ( bool) {
	_,  = .items[]
	return 
}

// Peek returns the key value (or undefined if not found) without updating
// the "recently used"-ness of the key.
func ( *LRU[, ]) ( ) ( ,  bool) {
	var  *internal.Entry[, ]
	if ,  = .items[];  {
		return .Value, true
	}
	return
}

// Remove removes the provided key from the cache, returning if the
// key was contained.
func ( *LRU[, ]) ( ) ( bool) {
	if ,  := .items[];  {
		.removeElement()
		return true
	}
	return false
}

// RemoveOldest removes the oldest item from the cache.
func ( *LRU[, ]) () ( ,  ,  bool) {
	if  := .evictList.Back();  != nil {
		.removeElement()
		return .Key, .Value, true
	}
	return
}

// GetOldest returns the oldest entry
func ( *LRU[, ]) () ( ,  ,  bool) {
	if  := .evictList.Back();  != nil {
		return .Key, .Value, true
	}
	return
}

// Keys returns a slice of the keys in the cache, from oldest to newest.
func ( *LRU[, ]) () [] {
	 := make([], .evictList.Length())
	 := 0
	for  := .evictList.Back();  != nil;  = .PrevEntry() {
		[] = .Key
		++
	}
	return 
}

// Values returns a slice of the values in the cache, from oldest to newest.
func ( *LRU[, ]) () [] {
	 := make([], len(.items))
	 := 0
	for  := .evictList.Back();  != nil;  = .PrevEntry() {
		[] = .Value
		++
	}
	return 
}

// Len returns the number of items in the cache.
func ( *LRU[, ]) () int {
	return .evictList.Length()
}

// Resize changes the cache size.
func ( *LRU[, ]) ( int) ( int) {
	 := .Len() - 
	if  < 0 {
		 = 0
	}
	for  := 0;  < ; ++ {
		.removeOldest()
	}
	.size = 
	return 
}

// removeOldest removes the oldest item from the cache.
func ( *LRU[, ]) () {
	if  := .evictList.Back();  != nil {
		.removeElement()
	}
}

// removeElement is used to remove a given list element from the cache
func ( *LRU[, ]) ( *internal.Entry[, ]) {
	.evictList.Remove()
	delete(.items, .Key)
	if .onEvict != nil {
		.onEvict(.Key, .Value)
	}
}