package logger

import (
	"log/slog"
	"os"
	"strings"
)

var logLevel slog.LevelVar

func getLevel(levelStr string) slog.Level {
	switch strings.ToLower(levelStr) {
	case "debug":
		return slog.LevelDebug
	case "warn":
		return slog.LevelWarn
	case "error":
		return slog.LevelError
	default:
		return slog.LevelInfo
	}
}

func New(levelStr string) *slog.Logger {
	logLevel.Set(getLevel(levelStr))

	return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		Level: &logLevel,
	}))
}
