package redis

import 

type ScriptingFunctionsCmdable interface {
	Eval(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd
	EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd
	EvalRO(ctx context.Context, script string, keys []string, args ...interface{}) *Cmd
	EvalShaRO(ctx context.Context, sha1 string, keys []string, args ...interface{}) *Cmd
	ScriptExists(ctx context.Context, hashes ...string) *BoolSliceCmd
	ScriptFlush(ctx context.Context) *StatusCmd
	ScriptKill(ctx context.Context) *StatusCmd
	ScriptLoad(ctx context.Context, script string) *StringCmd

	FunctionLoad(ctx context.Context, code string) *StringCmd
	FunctionLoadReplace(ctx context.Context, code string) *StringCmd
	FunctionDelete(ctx context.Context, libName string) *StringCmd
	FunctionFlush(ctx context.Context) *StringCmd
	FunctionKill(ctx context.Context) *StringCmd
	FunctionFlushAsync(ctx context.Context) *StringCmd
	FunctionList(ctx context.Context, q FunctionListQuery) *FunctionListCmd
	FunctionDump(ctx context.Context) *StringCmd
	FunctionRestore(ctx context.Context, libDump string) *StringCmd
	FunctionStats(ctx context.Context) *FunctionStatsCmd
	FCall(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd
	FCallRo(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd
	FCallRO(ctx context.Context, function string, keys []string, args ...interface{}) *Cmd
}

func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	return .eval(, "eval", , , ...)
}

func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	return .eval(, "eval_ro", , , ...)
}

func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	return .eval(, "evalsha", , , ...)
}

func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	return .eval(, "evalsha_ro", , , ...)
}

func ( cmdable) ( context.Context, ,  string,  []string,  ...interface{}) *Cmd {
	 := make([]interface{}, 3+len(), 3+len()+len())
	[0] = 
	[1] = 
	[2] = len()
	for ,  := range  {
		[3+] = 
	}
	 = appendArgs(, )
	 := NewCmd(, ...)

	// it is possible that only args exist without a key.
	// rdb.eval(ctx, eval, script, nil, arg1, arg2)
	if len() > 0 {
		.SetFirstKeyPos(3)
	}
	_ = (, )
	// Normalize NOSCRIPT to ErrNoScript for Script.Run/RunRO's EVAL fallback,
	// but only when the result is already readable: on the deferred
	// autopipeline face the call above merely enqueues, and reading the outcome
	// here would await execution — making the whole Eval family synchronous on
	// a face whose contract is to return immediately (review finding by codex
	// on #3942). When the result is still pending the normalization is skipped;
	// Script.Run/RunRO also match the raw NOSCRIPT prefix, so the fallback
	// keeps working on that face.
	if .resultReady() {
		if  := .rawErr();  != nil && HasErrorPrefix(, "NOSCRIPT") {
			.SetErr(ErrNoScript)
		}
	}
	return 
}

func ( cmdable) ( context.Context,  ...string) *BoolSliceCmd {
	 := make([]interface{}, 2+len())
	[0] = "script"
	[1] = "exists"
	for ,  := range  {
		[2+] = 
	}
	 := NewBoolSliceCmd(, ...)
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *StatusCmd {
	 := NewStatusCmd(, "script", "flush")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *StatusCmd {
	 := NewStatusCmd(, "script", "kill")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context,  string) *StringCmd {
	 := NewStringCmd(, "script", "load", )
	_ = (, )
	return 
}

// ------------------------------------------------------------------------------

// FunctionListQuery is used with FunctionList to query for Redis libraries
//
//	  	LibraryNamePattern 	- Use an empty string to get all libraries.
//	  						- Use a glob-style pattern to match multiple libraries with a matching name
//	  						- Use a library's full name to match a single library
//		WithCode			- If true, it will return the code of the library
type FunctionListQuery struct {
	LibraryNamePattern string
	WithCode           bool
}

func ( cmdable) ( context.Context,  string) *StringCmd {
	 := NewStringCmd(, "function", "load", )
	_ = (, )
	return 
}

func ( cmdable) ( context.Context,  string) *StringCmd {
	 := NewStringCmd(, "function", "load", "replace", )
	_ = (, )
	return 
}

func ( cmdable) ( context.Context,  string) *StringCmd {
	 := NewStringCmd(, "function", "delete", )
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *StringCmd {
	 := NewStringCmd(, "function", "flush")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *StringCmd {
	 := NewStringCmd(, "function", "kill")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *StringCmd {
	 := NewStringCmd(, "function", "flush", "async")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context,  FunctionListQuery) *FunctionListCmd {
	 := make([]interface{}, 2, 5)
	[0] = "function"
	[1] = "list"
	if .LibraryNamePattern != "" {
		 = append(, "libraryname", .LibraryNamePattern)
	}
	if .WithCode {
		 = append(, "withcode")
	}
	 := NewFunctionListCmd(, ...)
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *StringCmd {
	 := NewStringCmd(, "function", "dump")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context,  string) *StringCmd {
	 := NewStringCmd(, "function", "restore", )
	_ = (, )
	return 
}

func ( cmdable) ( context.Context) *FunctionStatsCmd {
	 := NewFunctionStatsCmd(, "function", "stats")
	_ = (, )
	return 
}

func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	 := fcallArgs("fcall", , , ...)
	 := NewCmd(, ...)
	if len() > 0 {
		.SetFirstKeyPos(3)
	}
	_ = (, )
	return 
}

// FCallRo this function simply calls FCallRO,
// Deprecated: to maintain convention FCallRO.
func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	return .FCallRO(, , , ...)
}

func ( cmdable) ( context.Context,  string,  []string,  ...interface{}) *Cmd {
	 := fcallArgs("fcall_ro", , , ...)
	 := NewCmd(, ...)
	if len() > 0 {
		.SetFirstKeyPos(3)
	}
	_ = (, )
	return 
}

func fcallArgs( string,  string,  []string,  ...interface{}) []interface{} {
	 := make([]interface{}, 3+len(), 3+len()+len())
	[0] = 
	[1] = 
	[2] = len()
	for ,  := range  {
		[3+] = 
	}

	 = append(, ...)
	return 
}