package ansi

import (
	

	
	
	
)

// Cut the string, without adding any prefix or tail strings. This function is
// aware of ANSI escape codes and will not break them, and accounts for
// wide-characters (such as East-Asian characters and emojis).
// This treats the text as a sequence of graphemes.
func ( string, ,  int) string {
	return cut(GraphemeWidth, , , )
}

// CutWc the string, without adding any prefix or tail strings. This function is
// aware of ANSI escape codes and will not break them, and accounts for
// wide-characters (such as East-Asian characters and emojis).
// Note that the [left] parameter is inclusive, while [right] isn't,
// which is to say it'll return `[left, right)`.
//
// This treats the text as a sequence of wide characters and runes.
func ( string, ,  int) string {
	return cut(WcWidth, , , )
}

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

	 := Truncate
	 := TruncateLeft
	if  == WcWidth {
		 = TruncateWc
		 = TruncateWc
	}

	if  == 0 {
		return (, , "")
	}
	return ((, , ""), , "")
}

// Truncate truncates a string to a given length, adding a tail to the end if
// the string is longer than the given length. This function is aware of ANSI
// escape codes and will not break them, and accounts for wide-characters (such
// as East-Asian characters and emojis).
// This treats the text as a sequence of graphemes.
func ( string,  int,  string) string {
	return truncate(GraphemeWidth, , , )
}

// TruncateWc truncates a string to a given length, adding a tail to the end if
// the string is longer than the given length. This function is aware of ANSI
// escape codes and will not break them, and accounts for wide-characters (such
// as East-Asian characters and emojis).
// This treats the text as a sequence of wide characters and runes.
func ( string,  int,  string) string {
	return truncate(WcWidth, , , )
}

func ( Method,  string,  int,  string) string {
	if  := StringWidth();  <=  {
		return 
	}

	 := StringWidth()
	 -= 
	if  < 0 {
		return ""
	}

	var  string
	var  strings.Builder
	 := 0
	 := false
	 := parser.GroundState // initial state
	 := 0

	// Here we iterate over the bytes of the string and collect printable
	// characters and runes. We also keep track of the width of the string
	// in cells.
	//
	// Once we reach the given length, we start ignoring characters and only
	// collect ANSI escape codes until we reach the end of string.
	for  < len() {
		,  := parser.Table.Transition(, [])
		if  == parser.Utf8State {
			// This action happens when we transition to the Utf8State.
			var  int
			,  = FirstGraphemeCluster([:], )
			// increment the index by the length of the cluster
			 += len()
			 += 

			// Are we ignoring? Skip to the next byte
			if  {
				continue
			}

			// Is this gonna be too wide?
			// If so write the tail and stop collecting.
			if  >  && ! {
				 = true
				.WriteString()
			}

			if  >  {
				continue
			}

			.WriteString()

			// Done collecting, now we're back in the ground state.
			 = parser.GroundState
			continue
		}

		switch  {
		case parser.PrintAction:
			// Is this gonna be too wide?
			// If so write the tail and stop collecting.
			if  >=  && ! {
				 = true
				.WriteString()
			}

			// Skip to the next byte if we're ignoring
			if  {
				++
				continue
			}

			// collects printable ASCII
			++
			fallthrough
		case parser.ExecuteAction:
			// execute action will be things like \n, which, if outside the cut,
			// should be ignored.
			if  {
				++
				continue
			}
			fallthrough
		default:
			.WriteByte([])
			++
		}

		// Transition to the next state.
		 = 

		// Once we reach the given length, we start ignoring runes and write
		// the tail to the buffer.
		if  >  && ! {
			 = true
			.WriteString()
		}
	}

	return .String()
}

// TruncateLeft truncates a string from the left side by removing n characters,
// adding a prefix to the beginning if the string is longer than n.
// This function is aware of ANSI escape codes and will not break them, and
// accounts for wide-characters (such as East-Asian characters and emojis).
// This treats the text as a sequence of graphemes.
func ( string,  int,  string) string {
	return truncateLeft(GraphemeWidth, , , )
}

// TruncateLeftWc truncates a string from the left side by removing n characters,
// adding a prefix to the beginning if the string is longer than n.
// This function is aware of ANSI escape codes and will not break them, and
// accounts for wide-characters (such as East-Asian characters and emojis).
// This treats the text as a sequence of wide characters and runes.
func ( string,  int,  string) string {
	return truncateLeft(WcWidth, , , )
}

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

	var  string
	var  strings.Builder
	 := 0
	 := true
	 := parser.GroundState
	 := 0

	for  < len() {
		if ! {
			.WriteString([:])
			break
		}

		,  := parser.Table.Transition(, [])
		if  == parser.Utf8State {
			var  int
			,  = FirstGraphemeCluster([:], )

			 += len()
			 += 

			if  >  &&  {
				 = false
				.WriteString()
			}

			if  >  {
				.WriteString()
			}

			if  {
				continue
			}

			 = parser.GroundState
			continue
		}

		switch  {
		case parser.PrintAction:
			++

			if  >  &&  {
				 = false
				.WriteString()
			}

			if  {
				++
				continue
			}

			fallthrough
		case parser.ExecuteAction:
			// execute action will be things like \n, which, if outside the cut,
			// should be ignored.
			if  {
				++
				continue
			}
			fallthrough
		default:
			.WriteByte([])
			++
		}

		 = 
		if  >  &&  {
			 = false
			.WriteString()
		}
	}

	return .String()
}

// ByteToGraphemeRange takes start and stop byte positions and converts them to
// grapheme-aware char positions.
// You can use this with [Truncate], [TruncateLeft], and [Cut].
func ( string, ,  int) (,  int) {
	,  := 0, 0
	 := graphemes.FromString()
	for  >  {
		if !.Next() {
			break
		}
		 += len(.Value())
		 += max(1, displaywidth.String(.Value()))
	}
	 = 
	for  >  {
		if !.Next() {
			break
		}
		 += len(.Value())
		 += max(1, displaywidth.String(.Value()))
	}
	 = 
	return , 
}