// Package helpers provides shared utilities for WebSocket handlers.
package helpers

import (
	"context"
	"slices"
	"time"
)

// DBContext provides a standard database operation context.
// It creates a context with the specified timeout and returns the context and cancel function.
func DBContext(timeout time.Duration) (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), timeout)
}

// DBContextShort returns a short-lived context for quick DB operations (3 seconds).
func DBContextShort() (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), 3*time.Second)
}

// DBContextMedium returns a medium-lived context for standard DB operations (5 seconds).
func DBContextMedium() (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), 5*time.Second)
}

// DBContextLong returns a long-lived context for complex DB operations (10 seconds).
func DBContextLong() (context.Context, context.CancelFunc) {
	return context.WithTimeout(context.Background(), 10*time.Second)
}

// StringSliceContains checks if a string slice contains a specific string.
func StringSliceContains(slice []string, item string) bool {
	return slices.Contains(slice, item)
}

// StringSliceFilter filters a string slice based on a predicate.
func StringSliceFilter(slice []string, predicate func(string) bool) []string {
	result := make([]string, 0)
	for _, s := range slice {
		if predicate(s) {
			result = append(result, s)
		}
	}
	return result
}
