package ratelimit

import (
	"context"
	"fmt"
	"hash/fnv"

	"github.com/tidbcloud/pkgv2/auth/auth0"
)

func FnvHash64(raw string) (string, error) {
	hash := fnv.New64()
	if _, err := hash.Write([]byte(raw)); err != nil {
		return "", err
	}
	return fmt.Sprintf("%x", hash.Sum64()), nil
}

func userFromContext(ctx context.Context) (string, bool) {
	u, ok := ctx.Value(auth0.AuthUserIDKey).(string)
	return u, ok
}

func checkMethod(method string, methods []Method) bool {
	for _, m := range methods {
		if m.Name == "*" || method == m.Name {
			return true
		}
	}
	return false
}
