package colorprofile

import (
	
	

	
)

// Profile is a color profile: NoTTY, Ascii, ANSI, ANSI256, or TrueColor.
type Profile byte

const (
	// Unknown is a profile that represents the absence of a profile.
	Unknown Profile = iota
	// NoTTY is a profile with no terminal support.
	NoTTY
	// ASCII is a profile with no color support.
	ASCII
	// ANSI is a profile with 16 colors (4-bit).
	ANSI
	// ANSI256 is a profile with 256 colors (8-bit).
	ANSI256
	// TrueColor is a profile with 16 million colors (24-bit).
	TrueColor
)

// Ascii is an alias for the [ASCII] profile for backwards compatibility.
const Ascii = ASCII //nolint:revive

// String returns the string representation of a Profile.
func ( Profile) () string {
	switch  {
	case TrueColor:
		return "TrueColor"
	case ANSI256:
		return "ANSI256"
	case ANSI:
		return "ANSI"
	case ASCII:
		return "Ascii"
	case NoTTY:
		return "NoTTY"
	default:
		return "Unknown"
	}
}

var (
	cache = map[Profile]map[color.Color]color.Color{
		ANSI256: {},
		ANSI:    {},
	}
	mu sync.RWMutex
)

// Convert transforms a given Color to a Color supported within the Profile.
func ( Profile) ( color.Color) ( color.Color) {
	if  <= ASCII {
		return nil
	}
	if  == TrueColor {
		// TrueColor is a passthrough.
		return 
	}

	// Do we have a cached color for this profile and color?
	mu.RLock()
	if  != nil && cache[] != nil {
		if ,  := cache[][];  {
			mu.RUnlock()
			return 
		}
	}
	mu.RUnlock()

	// If we don't have a cached color, we need to convert it and cache it.
	defer func() {
		mu.Lock()
		if  != nil && cache[] != nil {
			if ,  := cache[][]; ! {
				cache[][] = 
			}
		}
		mu.Unlock()
	}()

	switch .(type) {
	case ansi.BasicColor:
		return 

	case ansi.IndexedColor:
		if  == ANSI {
			return ansi.Convert16()
		}
		return 

	default:
		switch  {
		case ANSI256:
			return ansi.Convert256()
		case ANSI:
			return ansi.Convert16()
		default:
			return 
		}
	}
}