package colorprofile

import (
	
	
	
	
	
	
	

	
	
)

const dumbTerm = "dumb"

// Detect returns the color profile based on the terminal output, and
// environment variables. This respects NO_COLOR, CLICOLOR, and CLICOLOR_FORCE
// environment variables.
//
// The rules as follows:
//   - TERM=dumb is always treated as NoTTY unless CLICOLOR_FORCE=1 is set.
//   - If COLORTERM=truecolor, and the profile is not NoTTY, it gest upgraded to TrueColor.
//   - Using any 256 color terminal (e.g. TERM=xterm-256color) will set the profile to ANSI256.
//   - Using any color terminal (e.g. TERM=xterm-color) will set the profile to ANSI.
//   - Using CLICOLOR=1 without TERM defined should be treated as ANSI if the
//     output is a terminal.
//   - NO_COLOR takes precedence over CLICOLOR/CLICOLOR_FORCE, and will disable
//     colors but not text decoration, i.e. bold, italic, faint, etc.
//
// See https://no-color.org/ and https://bixense.com/clicolors/ for more information.
func ( io.Writer,  []string) Profile {
	,  := .(term.File)
	 := newEnviron()
	 := isTTYForced() || ( && term.IsTerminal(.Fd()))
	,  := .lookup("TERM")
	 := ! ||  == dumbTerm
	 := colorProfile(, )
	if  == TrueColor || envNoColor() {
		// We already know we have TrueColor, or NO_COLOR is set.
		return 
	}

	if  && ! {
		 := Terminfo()
		 := tmux()

		// Color profile is the maximum of env, terminfo, and tmux.
		return max(, max(, ))
	}

	return 
}

// Env returns the color profile based on the terminal environment variables.
// This respects NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment variables.
//
// The rules as follows:
//   - TERM=dumb is always treated as NoTTY unless CLICOLOR_FORCE=1 is set.
//   - If COLORTERM=truecolor, and the profile is not NoTTY, it gest upgraded to TrueColor.
//   - Using any 256 color terminal (e.g. TERM=xterm-256color) will set the profile to ANSI256.
//   - Using any color terminal (e.g. TERM=xterm-color) will set the profile to ANSI.
//   - Using CLICOLOR=1 without TERM defined should be treated as ANSI if the
//     output is a terminal.
//   - NO_COLOR takes precedence over CLICOLOR/CLICOLOR_FORCE, and will disable
//     colors but not text decoration, i.e. bold, italic, faint, etc.
//
// See https://no-color.org/ and https://bixense.com/clicolors/ for more information.
func ( []string) ( Profile) {
	return colorProfile(true, newEnviron())
}

func ( bool,  environ) ( Profile) {
	,  := .lookup("TERM")
	 := (! && runtime.GOOS != "windows") ||  == dumbTerm
	 := envColorProfile()
	if ! ||  {
		// Check if the output is a terminal.
		// Treat dumb terminals as NoTTY
		 = NoTTY
	} else {
		 = 
	}

	if envNoColor() &&  {
		if  > ASCII {
			 = ASCII
		}
		return //nolint:nakedret
	}

	if cliColorForced() {
		if  < ANSI {
			 = ANSI
		}
		if  >  {
			 = 
		}

		return //nolint:nakedret
	}

	if cliColor() {
		if  && ! &&  < ANSI {
			 = ANSI
		}
	}

	return 
}

// envNoColor returns true if the environment variables explicitly disable color output
// by setting NO_COLOR (https://no-color.org/).
func ( environ) bool {
	,  := strconv.ParseBool(.get("NO_COLOR"))
	return 
}

func ( environ) bool {
	,  := strconv.ParseBool(.get("CLICOLOR"))
	return 
}

func ( environ) bool {
	,  := strconv.ParseBool(.get("CLICOLOR_FORCE"))
	return 
}

func ( environ) bool {
	,  := strconv.ParseBool(.get("TTY_FORCE"))
	return 
}

func ( environ) bool {
	 := strings.ToLower(.get("COLORTERM"))
	return  == "truecolor" ||  == "24bit" ||
		 == "yes" ||  == "true"
}

// envColorProfile returns infers the color profile from the environment.
func ( environ) ( Profile) {
	,  := .lookup("TERM")
	if ! || len() == 0 ||  == dumbTerm {
		 = NoTTY
		if runtime.GOOS == "windows" {
			// Use Windows API to detect color profile. Windows Terminal and
			// cmd.exe don't define $TERM.
			if ,  := windowsColorProfile();  {
				 = 
			}
		}
	} else {
		 = ANSI
	}

	switch {
	case strings.Contains(, "alacritty"),
		strings.Contains(, "contour"),
		strings.Contains(, "foot"),
		strings.Contains(, "ghostty"),
		strings.Contains(, "kitty"),
		strings.Contains(, "rio"),
		strings.Contains(, "st"),
		strings.Contains(, "wezterm"):
		return TrueColor
	case strings.HasPrefix(, "tmux"), strings.HasPrefix(, "screen"):
		if  < ANSI256 {
			 = ANSI256
		}
	case strings.HasPrefix(, "xterm"):
		if  < ANSI {
			 = ANSI
		}
	}

	if len(["WT_SESSION"]) > 0 {
		// Windows Terminal supports TrueColor
		return TrueColor
	}

	if ,  := strconv.ParseBool(.get("GOOGLE_CLOUD_SHELL"));  {
		return TrueColor
	}

	// GNU Screen doesn't support TrueColor
	// Tmux doesn't support $COLORTERM
	if colorTerm() && !strings.HasPrefix(, "screen") && !strings.HasPrefix(, "tmux") {
		return TrueColor
	}

	if strings.HasSuffix(, "256color") &&  < ANSI256 {
		 = ANSI256
	}

	// Direct color terminals support true colors.
	if strings.HasSuffix(, "direct") {
		return TrueColor
	}

	return //nolint:nakedret
}

// Terminfo returns the color profile based on the terminal's terminfo
// database. This relies on the Tc and RGB capabilities to determine if the
// terminal supports TrueColor.
// If term is empty or "dumb", it returns NoTTY.
func ( string) ( Profile) {
	if len() == 0 ||  == "dumb" {
		return NoTTY
	}

	 = ANSI
	,  := terminfo.Load()
	if  != nil {
		return
	}

	 := .ExtBoolCapsShort()
	if ,  := ["Tc"];  {
		return TrueColor
	}

	if ,  := ["RGB"];  {
		return TrueColor
	}

	return
}

// Tmux returns the color profile based on `tmux info` output. Tmux supports
// overriding the terminal's color capabilities, so this function will return
// the color profile based on the tmux configuration.
func ( []string) Profile {
	return tmux(newEnviron())
}

// tmux returns the color profile based on the tmux environment variables.
func ( environ) ( Profile) {
	if ,  := .lookup("TMUX"); ! || len() == 0 {
		// Not in tmux
		return NoTTY
	}

	// Check if tmux has either Tc or RGB capabilities. Otherwise, return
	// ANSI256.
	 = ANSI256
	 := exec.CommandContext(context.Background(), "tmux", "info")
	,  := .Output()
	if  != nil {
		return
	}

	for  := range bytes.SplitSeq(, []byte("\n")) {
		if (bytes.Contains(, []byte("Tc")) || bytes.Contains(, []byte("RGB"))) &&
			bytes.Contains(, []byte("true")) {
			return TrueColor
		}
	}

	return
}

// environ is a map of environment variables.
type environ map[string]string

// newEnviron returns a new environment map from a slice of environment
// variables.
func ( []string) environ {
	 := make(map[string]string, len())
	for ,  := range  {
		 := strings.SplitN(, "=", 2)
		var  string
		if len() == 2 {
			 = [1]
		}
		[[0]] = 
	}
	return 
}

// lookup returns the value of an environment variable and a boolean indicating
// if it exists.
func ( environ) ( string) (string, bool) {
	,  := []
	return , 
}

// get returns the value of an environment variable and empty string if it
// doesn't exist.
func ( environ) ( string) string {
	,  := .lookup()
	return 
}