package colorprofile

import (
	
	
	
	
	

	
)

// NewWriter creates a new color profile writer that downgrades color sequences
// based on the detected color profile.
//
// If environ is nil, it will use os.Environ() to get the environment variables.
//
// It queries the given writer to determine if it supports ANSI escape codes.
// If it does, along with the given environment variables, it will determine
// the appropriate color profile to use for color formatting.
//
// This respects the NO_COLOR, CLICOLOR, and CLICOLOR_FORCE environment variables.
func ( io.Writer,  []string) *Writer {
	return &Writer{
		Forward: ,
		Profile: Detect(, ),
	}
}

// Writer represents a color profile writer that writes ANSI sequences to the
// underlying writer.
type Writer struct {
	Forward io.Writer
	Profile Profile
}

// Write writes the given text to the underlying writer.
func ( *Writer) ( []byte) (int, error) {
	switch {
	case .Profile == TrueColor:
		return .Forward.Write() //nolint:wrapcheck
	case .Profile <= NoTTY:
		,  := io.WriteString(.Forward, ansi.Strip(string()))
		return len(), 
	case .Profile == ASCII, .Profile == ANSI, .Profile == ANSI256:
		,  := .downsample()
		return len(), 
	default:
		return 0, fmt.Errorf("invalid profile: %v", .Profile)
	}
}

// downsample downgrades the given text to the appropriate color profile.
func ( *Writer) ( []byte) (int, error) {
	var  bytes.Buffer
	var  byte

	 := ansi.GetParser()
	defer ansi.PutParser()

	for len() > 0 {
		.Reset()
		, , ,  := ansi.DecodeSequence(, , )

		switch {
		case ansi.HasCsiPrefix() && .Command() == 'm':
			handleSgr(, , &)
		default:
			// If we're not a style SGR sequence, just write the bytes.
			if ,  := .Write();  != nil {
				return ,  //nolint:wrapcheck
			}
		}

		 = [:]
		 = 
	}

	return .Forward.Write(.Bytes()) //nolint:wrapcheck
}

// WriteString writes the given text to the underlying writer.
func ( *Writer) ( string) ( int,  error) {
	return .Write([]byte())
}

func ( *Writer,  *ansi.Parser,  *bytes.Buffer) {
	var  ansi.Style
	 := .Params()
	for  := 0;  < len(); ++ {
		 := []

		switch  := .Param(0);  {
		case 0:
			// SGR default parameter is 0. We use an empty string to reduce the
			// number of bytes written to the buffer.
			 = append(, "")
		case 30, 31, 32, 33, 34, 35, 36, 37: // 8-bit foreground color
			if .Profile < ANSI {
				continue
			}
			 = .ForegroundColor(
				.Profile.Convert(ansi.BasicColor( - 30))) //nolint:gosec
		case 38: // 16 or 24-bit foreground color
			var  color.Color
			if  := ansi.ReadStyleColor([:], &);  > 0 {
				 +=  - 1
			}
			if .Profile < ANSI {
				continue
			}
			 = .ForegroundColor(.Profile.Convert())
		case 39: // default foreground color
			if .Profile < ANSI {
				continue
			}
			 = .ForegroundColor(nil)
		case 40, 41, 42, 43, 44, 45, 46, 47: // 8-bit background color
			if .Profile < ANSI {
				continue
			}
			 = .BackgroundColor(
				.Profile.Convert(ansi.BasicColor( - 40))) //nolint:gosec
		case 48: // 16 or 24-bit background color
			var  color.Color
			if  := ansi.ReadStyleColor([:], &);  > 0 {
				 +=  - 1
			}
			if .Profile < ANSI {
				continue
			}
			 = .BackgroundColor(.Profile.Convert())
		case 49: // default background color
			if .Profile < ANSI {
				continue
			}
			 = .BackgroundColor(nil)
		case 58: // 16 or 24-bit underline color
			var  color.Color
			if  := ansi.ReadStyleColor([:], &);  > 0 {
				 +=  - 1
			}
			if .Profile < ANSI {
				continue
			}
			 = .UnderlineColor(.Profile.Convert())
		case 59: // default underline color
			if .Profile < ANSI {
				continue
			}
			 = .UnderlineColor(nil)
		case 90, 91, 92, 93, 94, 95, 96, 97: // 8-bit bright foreground color
			if .Profile < ANSI {
				continue
			}
			 = .ForegroundColor(
				.Profile.Convert(ansi.BasicColor( - 90 + 8))) //nolint:gosec
		case 100, 101, 102, 103, 104, 105, 106, 107: // 8-bit bright background color
			if .Profile < ANSI {
				continue
			}
			 = .BackgroundColor(
				.Profile.Convert(ansi.BasicColor( - 100 + 8))) //nolint:gosec
		default:
			// If this is not a color attribute, just append it to the style.
			 = append(, strconv.Itoa())
		}
	}

	_, _ = .WriteString(.String())
}