/*
 * Copyright 2021 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package circuitbreak

import (
	"context"
	"errors"

	"github.com/cloudwego/kitex/pkg/kerrors"
)

// some types of error won't trigger circuit breaker
var ignoreErrTypes = map[error]ErrorType{
	kerrors.ErrInternalException: TypeIgnorable,
	kerrors.ErrServiceDiscovery:  TypeIgnorable,
	kerrors.ErrACL:               TypeIgnorable,
	kerrors.ErrLoadbalance:       TypeIgnorable,
}

// ErrorTypeOnServiceLevel determines the error type with a service level criteria.
func ErrorTypeOnServiceLevel(ctx context.Context, request, response interface{}, err error) ErrorType {
	if err != nil {
		for e, t := range ignoreErrTypes {
			if errors.Is(err, e) {
				return t
			}
		}
		var we *errorWrapperWithType
		if ok := errors.As(err, &we); ok {
			return we.errType
		}
		if kerrors.IsTimeoutError(err) {
			return TypeTimeout
		}
		return TypeFailure
	}
	return TypeSuccess
}

// ErrorTypeOnInstanceLevel determines the error type with an instance level criteria.
// Basically, it treats only the connection error as failure.
func ErrorTypeOnInstanceLevel(ctx context.Context, request, response interface{}, err error) ErrorType {
	if errors.Is(err, kerrors.ErrGetConnection) {
		return TypeFailure
	}
	return TypeSuccess
}

// FailIfError return TypeFailure if err is not nil, otherwise TypeSuccess.
func FailIfError(ctx context.Context, request, response interface{}, err error) ErrorType {
	if err != nil {
		return TypeFailure
	}
	return TypeSuccess
}

// NoDecoration returns the original err.
func NoDecoration(ctx context.Context, request interface{}, err error) error {
	return err
}
