package ansi

import (
	
	
	
	

	
)

// nbsp is a non-breaking space.
const nbsp = 0xA0

// Hardwrap wraps a string or a block of text to a given line length, breaking
// word boundaries. This will preserve ANSI escape codes and will account for
// wide-characters in the string.
// When preserveSpace is true, spaces at the beginning of a line will be
// preserved.
// This treats the text as a sequence of graphemes.
func ( string,  int,  bool) string {
	return hardwrap(GraphemeWidth, , , )
}

// HardwrapWc wraps a string or a block of text to a given line length, breaking
// word boundaries. This will preserve ANSI escape codes and will account for
// wide-characters in the string.
// When preserveSpace is true, spaces at the beginning of a line will be
// preserved.
// This treats the text as a sequence of wide characters and runes.
func ( string,  int,  bool) string {
	return hardwrap(WcWidth, , , )
}

func ( Method,  string,  int,  bool) string {
	if  < 1 {
		return 
	}

	var (
		      []byte
		          bytes.Buffer
		     int
		 bool
		       = parser.GroundState // initial state
		            = []byte()
	)

	 := func() {
		.WriteByte('\n')
		 = 0
	}

	 := 0
	for  < len() {
		,  := parser.Table.Transition(, [])
		if  == parser.Utf8State {
			var  int
			,  = FirstGraphemeCluster([:], )
			 += len()

			if + >  {
				()
			}
			if ! &&  == 0 && len() <= 4 {
				// Skip spaces at the beginning of a line
				if ,  := utf8.DecodeRune();  != utf8.RuneError && unicode.IsSpace() {
					 = parser.GroundState
					continue
				}
			}

			.Write()
			 += 
			 = parser.GroundState
			continue
		}

		switch  {
		case parser.PrintAction, parser.ExecuteAction:
			if [] == '\n' {
				()
				 = false
				break
			}

			if +1 >  {
				()
				 = true
			}

			// Skip spaces at the beginning of a line
			if  == 0 {
				if ! &&  && unicode.IsSpace(rune([])) {
					break
				}
				 = false
			}

			.WriteByte([])
			if  == parser.PrintAction {
				++
			}
		default:
			.WriteByte([])
		}

		// We manage the UTF8 state separately manually above.
		if  != parser.Utf8State {
			 = 
		}
		++
	}

	return .String()
}

// Wordwrap wraps a string or a block of text to a given line length, not
// breaking word boundaries. This will preserve ANSI escape codes and will
// account for wide-characters in the string.
// The breakpoints string is a list of characters that are considered
// breakpoints for word wrapping. A hyphen (-) is always considered a
// breakpoint.
//
// Note: breakpoints must be a string of 1-cell wide rune characters.
//
// This treats the text as a sequence of graphemes.
func ( string,  int,  string) string {
	return wordwrap(GraphemeWidth, , , )
}

// WordwrapWc wraps a string or a block of text to a given line length, not
// breaking word boundaries. This will preserve ANSI escape codes and will
// account for wide-characters in the string.
// The breakpoints string is a list of characters that are considered
// breakpoints for word wrapping. A hyphen (-) is always considered a
// breakpoint.
//
// Note: breakpoints must be a string of 1-cell wide rune characters.
//
// This treats the text as a sequence of wide characters and runes.
func ( string,  int,  string) string {
	return wordwrap(WcWidth, , , )
}

func ( Method,  string,  int,  string) string {
	if  < 1 {
		return 
	}

	var (
		  []byte
		      bytes.Buffer
		     bytes.Buffer
		    bytes.Buffer
		 int
		  int
		   = parser.GroundState // initial state
		        = []byte()
	)

	 := func() {
		 += .Len()
		.Write(.Bytes())
		.Reset()
	}

	 := func() {
		if .Len() == 0 {
			return
		}

		()
		 += 
		.Write(.Bytes())
		.Reset()
		 = 0
	}

	 := func() {
		.WriteByte('\n')
		 = 0
		.Reset()
	}

	 := 0
	for  < len() {
		,  := parser.Table.Transition(, [])
		if  == parser.Utf8State { //nolint:nestif
			var  int
			,  = FirstGraphemeCluster([:], )
			 += len()

			,  := utf8.DecodeRune()
			if  != utf8.RuneError && unicode.IsSpace() &&  != nbsp {
				()
				.WriteRune()
			} else if bytes.ContainsAny(, ) {
				()
				()
				.Write()
				++
			} else {
				.Write()
				 += 
				if +.Len()+ >  &&
					 <  {
					()
				}
			}

			 = parser.GroundState
			continue
		}

		switch  {
		case parser.PrintAction, parser.ExecuteAction:
			 := rune([])
			switch {
			case  == '\n':
				if  == 0 {
					if +.Len() >  {
						 = 0
					} else {
						.Write(.Bytes())
					}
					.Reset()
				}

				()
				()
			case unicode.IsSpace():
				()
				.WriteByte([])
			case  == '-':
				fallthrough
			case runeContainsAny(, ):
				()
				()
				.WriteByte([])
				++
			default:
				.WriteByte([])
				++
				if +.Len()+ >  &&
					 <  {
					()
				}
			}

		default:
			.WriteByte([])
		}

		// We manage the UTF8 state separately manually above.
		if  != parser.Utf8State {
			 = 
		}
		++
	}

	()

	return .String()
}

// Wrap wraps a string or a block of text to a given line length, breaking word
// boundaries if necessary. This will preserve ANSI escape codes and will
// account for wide-characters in the string. The breakpoints string is a list
// of characters that are considered breakpoints for word wrapping. A hyphen
// (-) is always considered a breakpoint.
//
// Note: breakpoints must be a string of 1-cell wide rune characters.
//
// This treats the text as a sequence of graphemes.
func ( string,  int,  string) string {
	return wrap(GraphemeWidth, , , )
}

// WrapWc wraps a string or a block of text to a given line length, breaking word
// boundaries if necessary. This will preserve ANSI escape codes and will
// account for wide-characters in the string. The breakpoints string is a list
// of characters that are considered breakpoints for word wrapping. A hyphen
// (-) is always considered a breakpoint.
//
// Note: breakpoints must be a string of 1-cell wide rune characters.
//
// This treats the text as a sequence of wide characters and runes.
func ( string,  int,  string) string {
	return wrap(WcWidth, , , )
}

func ( Method,  string,  int,  string) string {
	if  < 1 {
		return 
	}

	var (
		    string
		        bytes.Buffer
		       bytes.Buffer
		      bytes.Buffer
		 int                  // width of the space buffer
		   int                  // written width of the line
		    int                  // word buffer len without ANSI escape codes
		     = parser.GroundState // initial state
	)

	 := func() {
		if  == 0 && .Len() == 0 {
			return
		}
		 += 
		.Write(.Bytes())
		.Reset()
		 = 0
	}

	 := func() {
		if .Len() == 0 {
			return
		}

		()
		 += 
		.Write(.Bytes())
		.Reset()
		 = 0
	}

	 := func() {
		.WriteByte('\n')
		 = 0
		.Reset()
		 = 0
	}

	 := 0
	for  < len() {
		,  := parser.Table.Transition(, [])
		if  == parser.Utf8State { //nolint:nestif
			var  int
			,  = FirstGraphemeCluster([:], )
			 += len()

			,  := utf8.DecodeRuneInString()
			switch {
			case  != utf8.RuneError && unicode.IsSpace() &&  != nbsp: // nbsp is a non-breaking space
				()
				.WriteRune()
				 += 
			case strings.ContainsAny(, ):
				()
				if ++ >  {
					.WriteString()
					 += 
				} else {
					()
					.WriteString()
					 += 
				}
			default:
				if + >  {
					// Hardwrap the word if it's too long
					()
				}

				.WriteString()
				 += 

				if ++ >  {
					()
				}

				if  ==  {
					// Hardwrap the word if it's too long
					()
				}
			}

			 = parser.GroundState
			continue
		}

		switch  {
		case parser.PrintAction, parser.ExecuteAction:
			switch  := rune([]); {
			case  == '\n':
				if  == 0 {
					if + >  {
						 = 0
					} else {
						// preserve whitespaces
						.Write(.Bytes())
					}
					.Reset()
					 = 0
				}

				()
				()
			case unicode.IsSpace():
				()
				.WriteRune()
				++
			case  == '-':
				fallthrough
			case runeContainsAny(, ):
				()
				if + >=  {
					// We can't fit the breakpoint in the current line, treat
					// it as part of the word.
					.WriteRune()
					++
				} else {
					()
					.WriteRune()
					++
				}
			default:
				if  ==  {
					()
				}

				.WriteRune()
				++

				if  ==  {
					// Hardwrap the word if it's too long
					()
				}

				if ++ >  {
					()
				}
			}

		default:
			.WriteByte([])
		}

		// We manage the UTF8 state separately manually above.
		if  != parser.Utf8State {
			 = 
		}
		++
	}

	if  == 0 {
		if + >  {
			 = 0
		} else {
			// preserve whitespaces
			.Write(.Bytes())
		}
		.Reset()
		 = 0
	}

	()

	return .String()
}

func ( rune,  string) bool {
	for ,  := range  {
		if  ==  {
			return true
		}
	}
	return false
}