package redis

import (
	
)

// note: the APIs is experimental and may be subject to change.
//
// ArrayCmdable defines the interface for Redis Array data structure commands
// available in Redis 8.8.0+.
//
// Redis array supports index range [0, math.MaxUint64-1), so index parameters use uint64.
type ArrayCmdable interface {
	ARSet(ctx context.Context, key string, index uint64, values ...string) *IntCmd
	ARGet(ctx context.Context, key string, index uint64) *StringCmd
	ARGetRange(ctx context.Context, key string, start, end uint64) *SliceCmd
	ARMGet(ctx context.Context, key string, indexes ...uint64) *SliceCmd
	ARMSet(ctx context.Context, key string, members ...AREntry) *IntCmd
	ARInsert(ctx context.Context, key string, values ...string) *UintCmd
	ARDel(ctx context.Context, key string, indexes ...uint64) *IntCmd
	ARDelRange(ctx context.Context, key string, ranges ...ARRange) *UintCmd
	ARLen(ctx context.Context, key string) *UintCmd
	ARCount(ctx context.Context, key string) *UintCmd
	ARNext(ctx context.Context, key string) *UintCmd
	ARSeek(ctx context.Context, key string, index uint64) *IntCmd
	ARInfo(ctx context.Context, key string) *MapStringInterfaceCmd
	ARInfoFull(ctx context.Context, key string) *MapStringInterfaceCmd
	ARScan(ctx context.Context, key string, start, end uint64, args *ARScanArgs) *AREntrySliceCmd
	AROpSum(ctx context.Context, key string, start, end uint64) *StringCmd
	AROpMin(ctx context.Context, key string, start, end uint64) *StringCmd
	AROpMax(ctx context.Context, key string, start, end uint64) *StringCmd
	AROpAnd(ctx context.Context, key string, start, end uint64) *IntCmd
	AROpOr(ctx context.Context, key string, start, end uint64) *IntCmd
	AROpXor(ctx context.Context, key string, start, end uint64) *IntCmd
	AROpMatch(ctx context.Context, key string, start, end uint64, value string) *IntCmd
	AROpUsed(ctx context.Context, key string, start, end uint64) *IntCmd
	ARGrep(ctx context.Context, key string, start, end string, args *ARGrepArgs) *UintSliceCmd
	ARGrepWithValues(ctx context.Context, key string, start, end string, args *ARGrepArgs) *AREntrySliceCmd
	ARRing(ctx context.Context, key string, size uint64, values ...string) *UintCmd
	ARLastItems(ctx context.Context, key string, count uint64, rev bool) *SliceCmd
}

// AREntry represents an index-value pair for ARMSET.
type AREntry struct {
	Index uint64
	Value string
}

// ARRange represents a start-end range for ARDELRANGE.
type ARRange struct {
	Start uint64
	End   uint64
}

// ARScanArgs contains optional arguments for ARSCAN.
type ARScanArgs struct {
	Limit uint64
}

// ARGrepPredicateType defines the type of predicate for ARGREP.
type ARGrepPredicateType string

const (
	ARGrepExact ARGrepPredicateType = "EXACT"
	ARGrepMatch ARGrepPredicateType = "MATCH"
	ARGrepGlob  ARGrepPredicateType = "GLOB"
	ARGrepRegex ARGrepPredicateType = "RE"
)

// ARGrepPredicate represents a search predicate for ARGREP.
type ARGrepPredicate struct {
	Type  ARGrepPredicateType
	Value string
}

// ARGrepArgs contains optional arguments for ARGREP.
// Redis ARGREP defaults to OR when multiple predicates are given.
// Set CombineAnd to true to combine predicates with AND instead.
type ARGrepArgs struct {
	Predicates []ARGrepPredicate
	CombineAnd bool
	Limit      uint64
	NoCase     bool
}

// ARSet sets one or more contiguous values starting at an index in an array.
// Returns the number of new slots that were set (previously empty).
func ( cmdable) ( context.Context,  string,  uint64,  ...string) *IntCmd {
	 := make([]any, 3, 3+len())
	[0] = "arset"
	[1] = 
	[2] = 
	for ,  := range  {
		 = append(, )
	}
	 := NewIntCmd(, ...)
	_ = (, )
	return 
}

// ARGet gets the value at an index in an array.
// Returns redis.Nil if the key or index does not exist.
func ( cmdable) ( context.Context,  string,  uint64) *StringCmd {
	 := NewStringCmd(, "arget", , )
	_ = (, )
	return 
}

// ARGetRange gets values in a range of indexes.
// Returns values in the range, with nil for unset indexes.
func ( cmdable) ( context.Context,  string, ,  uint64) *SliceCmd {
	 := NewSliceCmd(, "argetrange", , , )
	_ = (, )
	return 
}

// ARMGet gets values at multiple indexes in an array.
// Returns values at the specified indexes, with nil for unset indexes.
func ( cmdable) ( context.Context,  string,  ...uint64) *SliceCmd {
	 := make([]any, 2+len())
	[0] = "armget"
	[1] = 
	for ,  := range  {
		[2+] = 
	}
	 := NewSliceCmd(, ...)
	_ = (, )
	return 
}

// ARMSet sets multiple index-value pairs in an array.
// Returns the number of new slots that were set (previously empty).
func ( cmdable) ( context.Context,  string,  ...AREntry) *IntCmd {
	 := make([]any, 2, 2+2*len())
	[0] = "armset"
	[1] = 
	for ,  := range  {
		 = append(, .Index, .Value)
	}
	 := NewIntCmd(, ...)
	_ = (, )
	return 
}

// ARInsert inserts one or more values at consecutive indexes.
// Returns the last index where a value was inserted.
func ( cmdable) ( context.Context,  string,  ...string) *UintCmd {
	 := make([]any, 2, 2+len())
	[0] = "arinsert"
	[1] = 
	for ,  := range  {
		 = append(, )
	}
	 := NewUintCmd(, ...)
	_ = (, )
	return 
}

// ARDel deletes elements at the specified indexes in an array.
// Returns the number of elements deleted.
func ( cmdable) ( context.Context,  string,  ...uint64) *IntCmd {
	 := make([]any, 2+len())
	[0] = "ardel"
	[1] = 
	for ,  := range  {
		[2+] = 
	}
	 := NewIntCmd(, ...)
	_ = (, )
	return 
}

// ARDelRange deletes elements in one or more ranges.
// Returns the number of elements deleted.
func ( cmdable) ( context.Context,  string,  ...ARRange) *UintCmd {
	 := make([]any, 2, 2+2*len())
	[0] = "ardelrange"
	[1] = 
	for ,  := range  {
		 = append(, .Start, .End)
	}
	 := NewUintCmd(, ...)
	_ = (, )
	return 
}

// ARLen returns the length of an array (max index + 1).
// Returns 0 if the key does not exist.
func ( cmdable) ( context.Context,  string) *UintCmd {
	 := NewUintCmd(, "arlen", )
	_ = (, )
	return 
}

// ARCount returns the number of non-empty elements in an array.
// Returns 0 if the key does not exist.
func ( cmdable) ( context.Context,  string) *UintCmd {
	 := NewUintCmd(, "arcount", )
	_ = (, )
	return 
}

// ARNext returns the next index ARINSERT would use.
// Returns 0 for missing keys or when no insert happened yet.
// Returns nil when the insertion cursor is exhausted / would overflow.
func ( cmdable) ( context.Context,  string) *UintCmd {
	 := NewUintCmd(, "arnext", )
	_ = (, )
	return 
}

// ARSeek sets the ARINSERT / ARRING cursor to a specific index.
// Returns 1 if the cursor was set, 0 if the key does not exist.
func ( cmdable) ( context.Context,  string,  uint64) *IntCmd {
	 := NewIntCmd(, "arseek", , )
	_ = (, )
	return 
}

// ARInfo returns metadata about an array.
func ( cmdable) ( context.Context,  string) *MapStringInterfaceCmd {
	 := NewMapStringInterfaceCmd(, "arinfo", )
	_ = (, )
	return 
}

// ARInfoFull returns detailed metadata about an array including slice statistics.
func ( cmdable) ( context.Context,  string) *MapStringInterfaceCmd {
	 := NewMapStringInterfaceCmd(, "arinfo", , "full")
	_ = (, )
	return 
}

// ARScan iterates existing elements in a range, returning index-value pairs.
func ( cmdable) ( context.Context,  string, ,  uint64,  *ARScanArgs) *AREntrySliceCmd {
	 := make([]any, 4, 6)
	[0], [1], [2], [3] = "arscan", , , 
	if  != nil && .Limit > 0 {
		 = append(, "limit", .Limit)
	}
	 := NewAREntrySliceCmd(, ...)
	_ = (, )
	return 
}

// AROpSum returns the sum of numeric elements in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *StringCmd {
	 := NewStringCmd(, "arop", , , , "SUM")
	_ = (, )
	return 
}

// AROpMin returns the minimum numeric element in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *StringCmd {
	 := NewStringCmd(, "arop", , , , "MIN")
	_ = (, )
	return 
}

// AROpMax returns the maximum numeric element in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *StringCmd {
	 := NewStringCmd(, "arop", , , , "MAX")
	_ = (, )
	return 
}

// AROpAnd returns the bitwise AND of integer elements in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *IntCmd {
	 := NewIntCmd(, "arop", , , , "AND")
	_ = (, )
	return 
}

// AROpOr returns the bitwise OR of integer elements in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *IntCmd {
	 := NewIntCmd(, "arop", , , , "OR")
	_ = (, )
	return 
}

// AROpXor returns the bitwise XOR of integer elements in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *IntCmd {
	 := NewIntCmd(, "arop", , , , "XOR")
	_ = (, )
	return 
}

// AROpMatch returns the count of elements matching a target string in a range.
func ( cmdable) ( context.Context,  string, ,  uint64,  string) *IntCmd {
	 := NewIntCmd(, "arop", , , , "MATCH", )
	_ = (, )
	return 
}

// AROpUsed returns the count of non-empty slots in a range.
func ( cmdable) ( context.Context,  string, ,  uint64) *IntCmd {
	 := NewIntCmd(, "arop", , , , "USED")
	_ = (, )
	return 
}

// ARGrep searches array elements in a range using textual predicates.
// Returns matching indexes only. Use ARGrepWithValues to also get the values.
func ( cmdable) ( context.Context,  string, ,  string,  *ARGrepArgs) *UintSliceCmd {
	 := make([]any, 4, 4+.Len())
	[0], [1], [2], [3] = "argrep", , , 
	 = .Append()
	 := NewUintSliceCmd(, ...)
	_ = (, )
	return 
}

// ARGrepWithValues searches array elements in a range using textual predicates.
// Returns matching indexes and their values as index-value pairs.
func ( cmdable) ( context.Context,  string, ,  string,  *ARGrepArgs) *AREntrySliceCmd {
	 := make([]any, 4, 5+.Len())
	[0], [1], [2], [3] = "argrep", , , 
	 = .Append()
	 = append(, "withvalues")
	 := NewAREntrySliceCmd(, ...)
	_ = (, )
	return 
}

func ( *ARGrepArgs) () int {
	if  == nil {
		return 0
	}
	 := 2 * len(.Predicates)
	if .CombineAnd {
		++
	}
	if .Limit > 0 {
		 += 2
	}
	if .NoCase {
		++
	}
	return 
}

func ( *ARGrepArgs) ( []any) []any {
	if  == nil {
		return 
	}
	for ,  := range .Predicates {
		 = append(, string(.Type), .Value)
	}
	if .CombineAnd {
		 = append(, "and")
	}
	if .Limit > 0 {
		 = append(, "limit", .Limit)
	}
	if .NoCase {
		 = append(, "nocase")
	}
	return 
}

// ARRing inserts values into a ring buffer of specified size, wrapping and truncating as needed.
// Returns the last index where a value was inserted.
func ( cmdable) ( context.Context,  string,  uint64,  ...string) *UintCmd {
	 := make([]any, 3, 3+len())
	[0] = "arring"
	[1] = 
	[2] = 
	for ,  := range  {
		 = append(, )
	}
	 := NewUintCmd(, ...)
	_ = (, )
	return 
}

// ARLastItems returns the most recently inserted elements.
// When rev is true, returns items in reverse order.
func ( cmdable) ( context.Context,  string,  uint64,  bool) *SliceCmd {
	 := make([]any, 3, 4)
	[0], [1], [2] = "arlastitems", , 
	if  {
		 = append(, "rev")
	}
	 := NewSliceCmd(, ...)
	_ = (, )
	return 
}