package lipgloss

import (
	
	
	
	

	
	
	
)

// Border contains a series of values which comprise the various parts of a
// border.
type Border struct {
	Top          string
	Bottom       string
	Left         string
	Right        string
	TopLeft      string
	TopRight     string
	BottomLeft   string
	BottomRight  string
	MiddleLeft   string
	MiddleRight  string
	Middle       string
	MiddleTop    string
	MiddleBottom string
}

// GetTopSize returns the width of the top border. If borders contain runes of
// varying widths, the widest rune is returned. If no border exists on the top
// edge, 0 is returned.
func ( Border) () int {
	return getBorderEdgeWidth(.TopLeft, .Top, .TopRight)
}

// GetRightSize returns the width of the right border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the right edge, 0 is returned.
func ( Border) () int {
	return getBorderEdgeWidth(.TopRight, .Right, .BottomRight)
}

// GetBottomSize returns the width of the bottom border. If borders contain
// runes of varying widths, the widest rune is returned. If no border exists on
// the bottom edge, 0 is returned.
func ( Border) () int {
	return getBorderEdgeWidth(.BottomLeft, .Bottom, .BottomRight)
}

// GetLeftSize returns the width of the left border. If borders contain runes
// of varying widths, the widest rune is returned. If no border exists on the
// left edge, 0 is returned.
func ( Border) () int {
	return getBorderEdgeWidth(.TopLeft, .Left, .BottomLeft)
}

func ( ...string) ( int) {
	for ,  := range  {
		 = max(, maxRuneWidth())
	}
	return 
}

var (
	noBorder = Border{}

	normalBorder = Border{
		Top:          "─",
		Bottom:       "─",
		Left:         "│",
		Right:        "│",
		TopLeft:      "┌",
		TopRight:     "┐",
		BottomLeft:   "└",
		BottomRight:  "┘",
		MiddleLeft:   "├",
		MiddleRight:  "┤",
		Middle:       "┼",
		MiddleTop:    "┬",
		MiddleBottom: "┴",
	}

	roundedBorder = Border{
		Top:          "─",
		Bottom:       "─",
		Left:         "│",
		Right:        "│",
		TopLeft:      "╭",
		TopRight:     "╮",
		BottomLeft:   "╰",
		BottomRight:  "╯",
		MiddleLeft:   "├",
		MiddleRight:  "┤",
		Middle:       "┼",
		MiddleTop:    "┬",
		MiddleBottom: "┴",
	}

	blockBorder = Border{
		Top:          "█",
		Bottom:       "█",
		Left:         "█",
		Right:        "█",
		TopLeft:      "█",
		TopRight:     "█",
		BottomLeft:   "█",
		BottomRight:  "█",
		MiddleLeft:   "█",
		MiddleRight:  "█",
		Middle:       "█",
		MiddleTop:    "█",
		MiddleBottom: "█",
	}

	outerHalfBlockBorder = Border{
		Top:         "▀",
		Bottom:      "▄",
		Left:        "▌",
		Right:       "▐",
		TopLeft:     "▛",
		TopRight:    "▜",
		BottomLeft:  "▙",
		BottomRight: "▟",
	}

	innerHalfBlockBorder = Border{
		Top:         "▄",
		Bottom:      "▀",
		Left:        "▐",
		Right:       "▌",
		TopLeft:     "▗",
		TopRight:    "▖",
		BottomLeft:  "▝",
		BottomRight: "▘",
	}

	thickBorder = Border{
		Top:          "━",
		Bottom:       "━",
		Left:         "┃",
		Right:        "┃",
		TopLeft:      "┏",
		TopRight:     "┓",
		BottomLeft:   "┗",
		BottomRight:  "┛",
		MiddleLeft:   "┣",
		MiddleRight:  "┫",
		Middle:       "╋",
		MiddleTop:    "┳",
		MiddleBottom: "┻",
	}

	doubleBorder = Border{
		Top:          "═",
		Bottom:       "═",
		Left:         "║",
		Right:        "║",
		TopLeft:      "╔",
		TopRight:     "╗",
		BottomLeft:   "╚",
		BottomRight:  "╝",
		MiddleLeft:   "╠",
		MiddleRight:  "╣",
		Middle:       "╬",
		MiddleTop:    "╦",
		MiddleBottom: "╩",
	}

	hiddenBorder = Border{
		Top:          " ",
		Bottom:       " ",
		Left:         " ",
		Right:        " ",
		TopLeft:      " ",
		TopRight:     " ",
		BottomLeft:   " ",
		BottomRight:  " ",
		MiddleLeft:   " ",
		MiddleRight:  " ",
		Middle:       " ",
		MiddleTop:    " ",
		MiddleBottom: " ",
	}

	markdownBorder = Border{
		Top:          "-",
		Bottom:       "-",
		Left:         "|",
		Right:        "|",
		TopLeft:      "|",
		TopRight:     "|",
		BottomLeft:   "|",
		BottomRight:  "|",
		MiddleLeft:   "|",
		MiddleRight:  "|",
		Middle:       "|",
		MiddleTop:    "|",
		MiddleBottom: "|",
	}

	asciiBorder = Border{
		Top:          "-",
		Bottom:       "-",
		Left:         "|",
		Right:        "|",
		TopLeft:      "+",
		TopRight:     "+",
		BottomLeft:   "+",
		BottomRight:  "+",
		MiddleLeft:   "+",
		MiddleRight:  "+",
		Middle:       "+",
		MiddleTop:    "+",
		MiddleBottom: "+",
	}
)

// NormalBorder returns a standard-type border with a normal weight and 90
// degree corners.
func () Border {
	return normalBorder
}

// RoundedBorder returns a border with rounded corners.
func () Border {
	return roundedBorder
}

// BlockBorder returns a border that takes the whole block.
func () Border {
	return blockBorder
}

// OuterHalfBlockBorder returns a half-block border that sits outside the frame.
func () Border {
	return outerHalfBlockBorder
}

// InnerHalfBlockBorder returns a half-block border that sits inside the frame.
func () Border {
	return innerHalfBlockBorder
}

// ThickBorder returns a border that's thicker than the one returned by
// NormalBorder.
func () Border {
	return thickBorder
}

// DoubleBorder returns a border comprised of two thin strokes.
func () Border {
	return doubleBorder
}

// HiddenBorder returns a border that renders as a series of single-cell
// spaces. It's useful for cases when you want to remove a standard border but
// maintain layout positioning. This said, you can still apply a background
// color to a hidden border.
func () Border {
	return hiddenBorder
}

// MarkdownBorder return a table border in markdown style.
//
// Make sure to disable top and bottom border for the best result. This will
// ensure that the output is valid markdown.
//
//	table.New().Border(lipgloss.MarkdownBorder()).BorderTop(false).BorderBottom(false)
func () Border {
	return markdownBorder
}

// ASCIIBorder returns a table border with ASCII characters.
func () Border {
	return asciiBorder
}

type borderBlend struct {
	topGradient    []color.Color
	rightGradient  []color.Color
	bottomGradient []color.Color
	leftGradient   []color.Color
}

func ( Style) (,  int,  ...color.Color) *borderBlend {
	 := Blend1D(
		(++2)*2,
		...,
	)

	// Rotate array forward or reverse based on the offset if provided.
	if  := -.getAsInt(borderForegroundBlendOffsetKey);  != 0 {
		 := len()
		 %= 
		if  < 0 {
			 += 
		}
		slices.Reverse([:])
		slices.Reverse([:])
		slices.Reverse()
	}

	 := 0
	 := func( int) ( []color.Color) {
		 = [ : +]
		 += 
		return 
	}

	 := &borderBlend{
		topGradient:    ( + 2),
		rightGradient:  (),
		bottomGradient: ( + 2),
		leftGradient:   (),
	}

	// bottom and left gradients are reversed because they are drawn in reverse order.
	slices.Reverse(.bottomGradient)
	slices.Reverse(.leftGradient)

	return 
}

func ( Style) ( string) string {
	var (
		    = .getBorderStyle()
		    = .getAsBool(borderTopKey, false)
		  = .getAsBool(borderRightKey, false)
		 = .getAsBool(borderBottomKey, false)
		   = .getAsBool(borderLeftKey, false)
	)

	// If a border is set and no sides have been specifically turned on or off
	// render borders on all sides.
	if .isBorderStyleSetWithoutSides() {
		 = true
		 = true
		 = true
		 = true
	}

	// If no border is set or all borders are been disabled, abort.
	if  == noBorder || (! && ! && ! && !) {
		return 
	}

	,  := getLines()

	if  {
		if .Left == "" {
			.Left = " "
		}
		 += maxRuneWidth(.Left)
	}

	if  {
		if .Right == "" {
			.Right = " "
		}
		 += maxRuneWidth(.Right)
	}

	// If corners should be rendered but are set with the empty string, fill them
	// with a single space.
	if  &&  && .TopLeft == "" {
		.TopLeft = " "
	}
	if  &&  && .TopRight == "" {
		.TopRight = " "
	}
	if  &&  && .BottomLeft == "" {
		.BottomLeft = " "
	}
	if  &&  && .BottomRight == "" {
		.BottomRight = " "
	}

	// Figure out which corners we should actually be using based on which
	// sides are set to show.
	if  {
		switch {
		case ! && !:
			.TopLeft = ""
			.TopRight = ""
		case !:
			.TopLeft = ""
		case !:
			.TopRight = ""
		}
	}
	if  {
		switch {
		case ! && !:
			.BottomLeft = ""
			.BottomRight = ""
		case !:
			.BottomLeft = ""
		case !:
			.BottomRight = ""
		}
	}

	// For now, limit corners to one rune.
	.TopLeft = getFirstRuneAsString(.TopLeft)
	.TopRight = getFirstRuneAsString(.TopRight)
	.BottomRight = getFirstRuneAsString(.BottomRight)
	.BottomLeft = getFirstRuneAsString(.BottomLeft)

	var , , ,  color.Color
	var (
		  = .getAsColors(borderForegroundBlendKey)
		    = .getAsColor(borderTopBackgroundKey)
		  = .getAsColor(borderRightBackgroundKey)
		 = .getAsColor(borderBottomBackgroundKey)
		   = .getAsColor(borderLeftBackgroundKey)
	)

	var  *borderBlend
	if len() > 0 {
		 = .borderBlend(, len(), ...)
	} else {
		 = .getAsColor(borderTopForegroundKey)
		 = .getAsColor(borderRightForegroundKey)
		 = .getAsColor(borderBottomForegroundKey)
		 = .getAsColor(borderLeftForegroundKey)
	}

	var  strings.Builder

	// Render top
	if  {
		 := renderHorizontalEdge(.TopLeft, .Top, .TopRight, )
		if  != nil {
			.WriteString(.styleBorderBlend(, .topGradient, ))
		} else {
			.WriteString(.styleBorder(, , ))
		}
		.WriteRune('\n')
	}

	 := []rune(.Left)
	 := 0

	 := []rune(.Right)
	 := 0

	// Render sides
	var  string
	for ,  := range  {
		if  {
			 = string([])
			++
			if  >= len() {
				 = 0
			}
			if  != nil {
				.WriteString(.styleBorder(, .leftGradient[], ))
			} else {
				.WriteString(.styleBorder(, , ))
			}
		}
		.WriteString()
		if  {
			 = string([])
			++
			if  >= len() {
				 = 0
			}
			if  != nil {
				.WriteString(.styleBorder(, .rightGradient[], ))
			} else {
				.WriteString(.styleBorder(, , ))
			}
		}
		if  < len()-1 {
			.WriteRune('\n')
		}
	}

	// Render bottom
	if  {
		 := renderHorizontalEdge(.BottomLeft, .Bottom, .BottomRight, )
		.WriteRune('\n')
		if  != nil {
			.WriteString(.styleBorderBlend(, .bottomGradient, ))
		} else {
			.WriteString(.styleBorder(, , ))
		}
	}

	return .String()
}

// Render the horizontal (top or bottom) portion of a border.
func (, ,  string,  int) string {
	if  == "" {
		 = " "
	}

	 := ansi.StringWidth()
	 := ansi.StringWidth()

	 := []rune()
	 := 0

	 := strings.Builder{}
	.WriteString()

	for  := 0;  < --; {
		 := []
		.WriteRune()
		 := ansi.StringWidth(string())
		if  < 1 {
			 = 1
		}
		 += 
		++
		if  >= len() {
			 = 0
		}
	}

	.WriteString()
	return .String()
}

// styleBorder applies foreground and background styling to a border.
func ( Style) ( string, ,  color.Color) string {
	if  == noColor &&  == noColor {
		return 
	}
	var  ansi.Style
	if  != noColor {
		 = .ForegroundColor()
	}
	if  != noColor {
		 = .BackgroundColor()
	}
	return .Styled()
}

// styleBorderBlend applies foreground and background styling to a border, using blending.
func ( Style) ( string,  []color.Color,  color.Color) string {
	var  strings.Builder
	var  ansi.Style
	var  int

	 := uniseg.NewGraphemes()
	for .Next() {
		 = [:0]
		if [] != noColor {
			 = .ForegroundColor([])
		}
		if  != noColor {
			 = .BackgroundColor()
		}
		_, _ = .WriteString(.String())
		_, _ = .Write(.Bytes())
		++
	}
	_, _ = .WriteString(ansi.ResetStyle)
	return .String()
}

func ( string) int {
	switch len() {
	case 0:
		return 0
	case 1:
		return displaywidth.String()
	}

	var  int

	 := displaywidth.StringGraphemes()
	for .Next() {
		 = max(, .Width())
	}
	return 
}

func ( string) string {
	if  == "" {
		return 
	}
	,  := utf8.DecodeRuneInString()
	return [:]
}