package tui

import (
	"strings"

	"github.com/charmbracelet/lipgloss"
)

// DefaultGradientColors is the default color palette for ASCII art gradients.
// Purple → blue → cyan → red, applied line by line.
var DefaultGradientColors = []lipgloss.Color{
	lipgloss.Color("#9b59b6"),
	lipgloss.Color("#8e44ad"),
	lipgloss.Color("#3498db"),
	lipgloss.Color("#2980b9"),
	lipgloss.Color("#1abc9c"),
	lipgloss.Color("#16a085"),
	lipgloss.Color("#e74c3c"),
	lipgloss.Color("#c0392b"),
}

// ApplyGradient applies a color gradient to ASCII art lines.
// Each line gets a color from the palette, distributed proportionally.
// Pass nil for colors to use DefaultGradientColors.
func ApplyGradient(lines []string, colors []lipgloss.Color) string {
	if len(lines) == 0 {
		return ""
	}
	if len(colors) == 0 {
		colors = DefaultGradientColors
	}

	var result strings.Builder
	totalLines := len(lines)

	for i, line := range lines {
		colorIndex := (i * len(colors)) / totalLines
		if colorIndex >= len(colors) {
			colorIndex = len(colors) - 1
		}
		styledLine := lipgloss.NewStyle().Foreground(colors[colorIndex]).Render(line)
		result.WriteString(styledLine)
		result.WriteString("\n")
	}

	result.WriteString("\n")
	return result.String()
}
