package lipgloss

import (
	
	

	
)

// JoinHorizontal is a utility function for horizontally joining two
// potentially multi-lined strings along a vertical axis. The first argument is
// the position, with 0 being all the way at the top and 1 being all the way
// at the bottom.
//
// If you just want to align to the top, center or bottom you may as well just
// use the helper constants Top, Center, and Bottom.
//
// Example:
//
//	blockB := "...\n...\n..."
//	blockA := "...\n...\n...\n...\n..."
//
//	// Join 20% from the top
//	str := lipgloss.JoinHorizontal(0.2, blockA, blockB)
//
//	// Join on the top edge
//	str := lipgloss.JoinHorizontal(lipgloss.Top, blockA, blockB)
func ( Position,  ...string) string {
	if len() == 0 {
		return ""
	}
	if len() == 1 {
		return [0]
	}

	var (
		// Groups of strings broken into multiple lines
		 = make([][]string, len())

		// Max line widths for the above text blocks
		 = make([]int, len())

		// Height of the tallest block
		 int
	)

	// Break text blocks into lines and get max widths for each text block
	for ,  := range  {
		[], [] = getLines()
		if len([]) >  {
			 = len([])
		}
	}

	// Add extra lines to make each side the same height
	for  := range  {
		if len([]) >=  {
			continue
		}

		 := make([]string, -len([]))

		switch  {
		case Top:
			[] = append([], ...)

		case Bottom:
			[] = append(, []...)

		default: // Somewhere in the middle
			 := len()
			 := int(math.Round(float64() * .value()))
			 :=  - 
			 :=  - 

			[] = append([:], []...)
			[] = append([], [:]...)
		}
	}

	// Merge lines
	var  strings.Builder
	for  := range [0] { // remember, all blocks have the same number of members now
		for ,  := range  {
			.WriteString([])

			// Also make lines the same length
			.WriteString(strings.Repeat(" ", []-ansi.StringWidth([])))
		}
		if  < len([0])-1 {
			.WriteRune('\n')
		}
	}

	return .String()
}

// JoinVertical is a utility function for vertically joining two potentially
// multi-lined strings along a horizontal axis. The first argument is the
// position, with 0 being all the way to the left and 1 being all the way to
// the right.
//
// If you just want to align to the left, right or center you may as well just
// use the helper constants Left, Center, and Right.
//
// Example:
//
//	blockB := "...\n...\n..."
//	blockA := "...\n...\n...\n...\n..."
//
//	// Join 20% from the top
//	str := lipgloss.JoinVertical(0.2, blockA, blockB)
//
//	// Join on the right edge
//	str := lipgloss.JoinVertical(lipgloss.Right, blockA, blockB)
func ( Position,  ...string) string {
	if len() == 0 {
		return ""
	}
	if len() == 1 {
		return [0]
	}

	var (
		   = make([][]string, len())
		 int
	)

	for  := range  {
		var  int
		[],  = getLines([])
		if  >  {
			 = 
		}
	}

	var  strings.Builder
	for ,  := range  {
		for ,  := range  {
			 :=  - ansi.StringWidth()

			switch  {
			case Left:
				.WriteString()
				.WriteString(strings.Repeat(" ", ))

			case Right:
				.WriteString(strings.Repeat(" ", ))
				.WriteString()

			default: // Somewhere in the middle
				if  < 1 {
					.WriteString()
					break
				}

				 := int(math.Round(float64() * .value()))
				 :=  - 
				 :=  - 

				.WriteString(strings.Repeat(" ", ))
				.WriteString()
				.WriteString(strings.Repeat(" ", ))
			}

			// Write a newline as long as we're not on the last line of the
			// last block.
			if !( == len()-1 &&  == len()-1) { //nolint:staticcheck
				.WriteRune('\n')
			}
		}
	}

	return .String()
}