package terminfo

import (
	
	
	
)

// ColorLevel is the color level supported by a terminal.
type ColorLevel uint

// ColorLevel values.
const (
	ColorLevelNone ColorLevel = iota
	ColorLevelBasic
	ColorLevelHundreds
	ColorLevelMillions
)

// String satisfies the Stringer interface.
func ( ColorLevel) () string {
	switch  {
	case ColorLevelBasic:
		return "basic"
	case ColorLevelHundreds:
		return "hundreds"
	case ColorLevelMillions:
		return "millions"
	}
	return "none"
}

// ChromaFormatterName returns the github.com/alecthomas/chroma compatible
// formatter name for the color level.
func ( ColorLevel) () string {
	switch  {
	case ColorLevelBasic:
		return "terminal"
	case ColorLevelHundreds:
		return "terminal256"
	case ColorLevelMillions:
		return "terminal16m"
	}
	return "noop"
}

// ColorLevelFromEnv returns the color level COLORTERM, FORCE_COLOR,
// TERM_PROGRAM, or determined from the TERM environment variable.
func () (ColorLevel, error) {
	// check for overriding environment variables
	, ,  := os.Getenv("COLORTERM"), os.Getenv("TERM_PROGRAM"), os.Getenv("FORCE_COLOR")
	switch {
	case strings.Contains(, "truecolor") || strings.Contains(, "24bit") ||  == "Hyper":
		return ColorLevelMillions, nil
	case  != "" ||  != "":
		return ColorLevelBasic, nil
	case  == "Apple_Terminal":
		return ColorLevelHundreds, nil
	case  == "iTerm.app":
		 := os.Getenv("TERM_PROGRAM_VERSION")
		if  == "" {
			return ColorLevelHundreds, nil
		}
		,  := strconv.Atoi(strings.Split(, ".")[0])
		if  != nil {
			return ColorLevelNone, ErrInvalidTermProgramVersion
		}
		if  == 3 {
			return ColorLevelMillions, nil
		}
		return ColorLevelHundreds, nil
	}
	// otherwise determine from TERM's max_colors capability
	if  := os.Getenv("TERM");  != "" {
		,  := Load()
		if  != nil {
			return ColorLevelNone, 
		}
		,  := .Nums[MaxColors]
		switch {
		case ! ||  <= 16:
			return ColorLevelNone, nil
		case  &&  >= 256:
			return ColorLevelHundreds, nil
		}
	}
	return ColorLevelBasic, nil
}