package errors

import (
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

const defaultGRPCCode = codes.Internal

var grpcCodes = map[error]codes.Code{
	ErrInvalidArgs:          codes.InvalidArgument,
	ErrConflict:             codes.AlreadyExists,
	ErrInsufficientFunds:    codes.Unavailable,
	ErrInternalError:        codes.Internal,
	ErrMethodNotImplemented: codes.Unimplemented,
	ErrNoAccess:             codes.Unavailable,
	ErrNotFound:             codes.NotFound,
}

func GRPC(message string, err Error) error {
	currentStatus, ok := grpcCodes[err]
	if !ok {
		return status.Errorf(defaultGRPCCode, message, err)
	}

	return status.Errorf(currentStatus, message, err)
}
