package proto

import (
	
	
)

// Typed Redis errors for better error handling with wrapping support.
// These errors maintain backward compatibility by keeping the same error messages.

// LoadingError is returned when Redis is loading the dataset in memory.
type LoadingError struct {
	msg string
}

func ( *LoadingError) () string {
	return .msg
}

func ( *LoadingError) () {}

// NewLoadingError creates a new LoadingError with the given message.
func ( string) *LoadingError {
	return &LoadingError{msg: }
}

// ReadOnlyError is returned when trying to write to a read-only replica.
type ReadOnlyError struct {
	msg string
}

func ( *ReadOnlyError) () string {
	return .msg
}

func ( *ReadOnlyError) () {}

// NewReadOnlyError creates a new ReadOnlyError with the given message.
func ( string) *ReadOnlyError {
	return &ReadOnlyError{msg: }
}

// MovedError is returned when a key has been moved to a different node in a cluster.
type MovedError struct {
	msg  string
	addr string
}

func ( *MovedError) () string {
	return .msg
}

func ( *MovedError) () {}

// Addr returns the address of the node where the key has been moved.
func ( *MovedError) () string {
	return .addr
}

// NewMovedError creates a new MovedError with the given message and address.
func ( string,  string) *MovedError {
	return &MovedError{msg: , addr: }
}

// AskError is returned when a key is being migrated and the client should ask another node.
type AskError struct {
	msg  string
	addr string
}

func ( *AskError) () string {
	return .msg
}

func ( *AskError) () {}

// Addr returns the address of the node to ask.
func ( *AskError) () string {
	return .addr
}

// NewAskError creates a new AskError with the given message and address.
func ( string,  string) *AskError {
	return &AskError{msg: , addr: }
}

// ClusterDownError is returned when the cluster is down.
type ClusterDownError struct {
	msg string
}

func ( *ClusterDownError) () string {
	return .msg
}

func ( *ClusterDownError) () {}

// NewClusterDownError creates a new ClusterDownError with the given message.
func ( string) *ClusterDownError {
	return &ClusterDownError{msg: }
}

// TryAgainError is returned when a command cannot be processed and should be retried.
type TryAgainError struct {
	msg string
}

func ( *TryAgainError) () string {
	return .msg
}

func ( *TryAgainError) () {}

// NewTryAgainError creates a new TryAgainError with the given message.
func ( string) *TryAgainError {
	return &TryAgainError{msg: }
}

// MasterDownError is returned when the master is down.
type MasterDownError struct {
	msg string
}

func ( *MasterDownError) () string {
	return .msg
}

func ( *MasterDownError) () {}

// NewMasterDownError creates a new MasterDownError with the given message.
func ( string) *MasterDownError {
	return &MasterDownError{msg: }
}

// MaxClientsError is returned when the maximum number of clients has been reached.
type MaxClientsError struct {
	msg string
}

func ( *MaxClientsError) () string {
	return .msg
}

func ( *MaxClientsError) () {}

// NewMaxClientsError creates a new MaxClientsError with the given message.
func ( string) *MaxClientsError {
	return &MaxClientsError{msg: }
}

// AuthError is returned when authentication fails.
type AuthError struct {
	msg string
}

func ( *AuthError) () string {
	return .msg
}

func ( *AuthError) () {}

// NewAuthError creates a new AuthError with the given message.
func ( string) *AuthError {
	return &AuthError{msg: }
}

// PermissionError is returned when a user lacks required permissions.
type PermissionError struct {
	msg string
}

func ( *PermissionError) () string {
	return .msg
}

func ( *PermissionError) () {}

// NewPermissionError creates a new PermissionError with the given message.
func ( string) *PermissionError {
	return &PermissionError{msg: }
}

// ExecAbortError is returned when a transaction is aborted.
type ExecAbortError struct {
	msg string
}

func ( *ExecAbortError) () string {
	return .msg
}

func ( *ExecAbortError) () {}

// NewExecAbortError creates a new ExecAbortError with the given message.
func ( string) *ExecAbortError {
	return &ExecAbortError{msg: }
}

// OOMError is returned when Redis is out of memory.
type OOMError struct {
	msg string
}

func ( *OOMError) () string {
	return .msg
}

func ( *OOMError) () {}

// NewOOMError creates a new OOMError with the given message.
func ( string) *OOMError {
	return &OOMError{msg: }
}

// NoReplicasError is returned when not enough replicas acknowledge a write.
// This error occurs when using WAIT/WAITAOF commands or CLUSTER SETSLOT with
// synchronous replication, and the required number of replicas cannot confirm
// the write within the timeout period.
type NoReplicasError struct {
	msg string
}

func ( *NoReplicasError) () string {
	return .msg
}

func ( *NoReplicasError) () {}

// NewNoReplicasError creates a new NoReplicasError with the given message.
func ( string) *NoReplicasError {
	return &NoReplicasError{msg: }
}

// parseTypedRedisError parses a Redis error message and returns a typed error if applicable.
// This function maintains backward compatibility by keeping the same error messages.
func parseTypedRedisError( string) error {
	// Check for specific error patterns and return typed errors
	switch {
	case strings.HasPrefix(, "LOADING "):
		return NewLoadingError()
	case strings.HasPrefix(, "READONLY "):
		return NewReadOnlyError()
	case strings.HasPrefix(, "MOVED "):
		// Extract address from "MOVED <slot> <addr>"
		 := extractAddr()
		return NewMovedError(, )
	case strings.HasPrefix(, "ASK "):
		// Extract address from "ASK <slot> <addr>"
		 := extractAddr()
		return NewAskError(, )
	case strings.HasPrefix(, "CLUSTERDOWN "):
		return NewClusterDownError()
	case strings.HasPrefix(, "TRYAGAIN "):
		return NewTryAgainError()
	case strings.HasPrefix(, "MASTERDOWN "):
		return NewMasterDownError()
	case strings.HasPrefix(, "NOREPLICAS "):
		return NewNoReplicasError()
	case  == "ERR max number of clients reached":
		return NewMaxClientsError()
	case strings.HasPrefix(, "NOAUTH "), strings.HasPrefix(, "WRONGPASS "), strings.Contains(, "unauthenticated"):
		return NewAuthError()
	case strings.HasPrefix(, "NOPERM "):
		return NewPermissionError()
	case strings.HasPrefix(, "EXECABORT "):
		return NewExecAbortError()
	case strings.HasPrefix(, "OOM "):
		return NewOOMError()
	default:
		// Return generic RedisError for unknown error types
		return RedisError()
	}
}

// extractAddr extracts the address from MOVED/ASK error messages.
// Format: "MOVED <slot> <addr>" or "ASK <slot> <addr>"
func extractAddr( string) string {
	 := strings.LastIndex(, " ")
	if  == -1 {
		return ""
	}
	return [+1:]
}

// IsLoadingError checks if an error is a LoadingError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *LoadingError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with LOADING prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "LOADING ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "LOADING ")
}

// IsReadOnlyError checks if an error is a ReadOnlyError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *ReadOnlyError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with READONLY prefix or Lua script READONLY
	var  RedisError
	if errors.As(, &) {
		 := .Error()
		if strings.HasPrefix(, "READONLY ") {
			return true
		}
		// Lua script wrapped READONLY errors:
		//   "ERR Error running script (call to f_<sha>): @user_script:N: -READONLY You can't write against a read only replica."
		if strings.Contains(, "-READONLY You can't write against a read only replica") {
			return true
		}
	}
	// Fallback to string checking for backward compatibility
	 := .Error()
	if strings.HasPrefix(, "READONLY ") {
		return true
	}
	return strings.Contains(, "-READONLY You can't write against a read only replica")
}

// IsMovedError checks if an error is a MovedError, even if wrapped.
// Returns the error and a boolean indicating if it's a MovedError.
func ( error) (*MovedError, bool) {
	if  == nil {
		return nil, false
	}
	var  *MovedError
	if errors.As(, &) {
		return , true
	}
	// Fallback to string checking for backward compatibility
	 := .Error()
	if strings.HasPrefix(, "MOVED ") {
		// Parse: MOVED 3999 127.0.0.1:6381
		 := strings.Split(, " ")
		if len() == 3 {
			return &MovedError{msg: , addr: [2]}, true
		}
	}
	return nil, false
}

// IsAskError checks if an error is an AskError, even if wrapped.
// Returns the error and a boolean indicating if it's an AskError.
func ( error) (*AskError, bool) {
	if  == nil {
		return nil, false
	}
	var  *AskError
	if errors.As(, &) {
		return , true
	}
	// Fallback to string checking for backward compatibility
	 := .Error()
	if strings.HasPrefix(, "ASK ") {
		// Parse: ASK 3999 127.0.0.1:6381
		 := strings.Split(, " ")
		if len() == 3 {
			return &AskError{msg: , addr: [2]}, true
		}
	}
	return nil, false
}

// IsClusterDownError checks if an error is a ClusterDownError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *ClusterDownError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with CLUSTERDOWN prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "CLUSTERDOWN ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "CLUSTERDOWN ")
}

// IsTryAgainError checks if an error is a TryAgainError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *TryAgainError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with TRYAGAIN prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "TRYAGAIN ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "TRYAGAIN ")
}

// IsMasterDownError checks if an error is a MasterDownError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *MasterDownError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with MASTERDOWN prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "MASTERDOWN ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "MASTERDOWN ")
}

// IsMaxClientsError checks if an error is a MaxClientsError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *MaxClientsError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with max clients prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "ERR max number of clients reached") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "ERR max number of clients reached")
}

// IsAuthError checks if an error is an AuthError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *AuthError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with auth error prefix
	var  RedisError
	if errors.As(, &) {
		 := .Error()
		return strings.HasPrefix(, "NOAUTH ") || strings.HasPrefix(, "WRONGPASS ") || strings.Contains(, "unauthenticated")
	}
	// Fallback to string checking for backward compatibility
	 := .Error()
	return strings.HasPrefix(, "NOAUTH ") || strings.HasPrefix(, "WRONGPASS ") || strings.Contains(, "unauthenticated")
}

// IsPermissionError checks if an error is a PermissionError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *PermissionError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with NOPERM prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "NOPERM ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "NOPERM ")
}

// IsExecAbortError checks if an error is an ExecAbortError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *ExecAbortError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with EXECABORT prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "EXECABORT ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "EXECABORT ")
}

// IsOOMError checks if an error is an OOMError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *OOMError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with OOM prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "OOM ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "OOM ")
}

// IsNoReplicasError checks if an error is a NoReplicasError, even if wrapped.
func ( error) bool {
	if  == nil {
		return false
	}
	var  *NoReplicasError
	if errors.As(, &) {
		return true
	}
	// Check if wrapped error is a RedisError with NOREPLICAS prefix
	var  RedisError
	if errors.As(, &) && strings.HasPrefix(.Error(), "NOREPLICAS ") {
		return true
	}
	// Fallback to string checking for backward compatibility
	return strings.HasPrefix(.Error(), "NOREPLICAS ")
}