package logrus

import (
	
	
)

const (
	ansiReset    = "\x1b[0m"    // reset attributes
	ansiRed      = "\x1b[31m"   // red
	ansiYellow   = "\x1b[33m"   // yellow
	ansiCyan     = "\x1b[36m"   // cyan
	ansiDimCyan  = "\x1b[2;36m" // dim cyan
	ansiDimWhite = "\x1b[2;37m" // dim white (light gray)
)

type lvlPrefix struct {
	full      string
	truncated string
	padded    string
}

func colorize( Level,  string) string {
	 := ansiCyan
	switch  {
	case TraceLevel:
		 = ansiDimWhite
	case DebugLevel:
		 = ansiDimCyan
	case WarnLevel:
		 = ansiYellow
	case ErrorLevel, FatalLevel, PanicLevel:
		 = ansiRed
	case InfoLevel:
		 = ansiCyan
	}
	return  +  + ansiReset
}

func formatLevel( Level, ,  bool,  int) string {
	 := strings.ToUpper(.String())

	if  &&  > len() {
		 += strings.Repeat(" ", -len())
	}

	if ! && ! && len() > 4 {
		 = [:4]
	}

	return colorize(, )
}

var levelPrefixOnce = sync.OnceValues(func() (map[Level]lvlPrefix, lvlPrefix) {
	var  Level
	 := 0
	for ,  := range AllLevels {
		if  >  {
			 = 
		}
		if  := len(.String());  >  {
			 = 
		}
	}

	 := make(map[Level]lvlPrefix, len(AllLevels))
	for ,  := range AllLevels {
		[] = lvlPrefix{
			full:      formatLevel(, true, false, ),
			truncated: formatLevel(, false, false, ),
			padded:    formatLevel(, true, true, ),
		}
	}

	 :=  + 1
	 := lvlPrefix{
		full:      formatLevel(, true, false, ),
		truncated: formatLevel(, false, false, ),
		padded:    formatLevel(, true, true, ),
	}

	return , 
})

func levelPrefix( Level, ,  bool) string {
	,  := levelPrefixOnce()

	,  := []
	if ! {
		 = 
	}

	switch {
	case :
		return .padded
	case !:
		return .truncated
	default:
		return .full
	}
}