package displaywidth

import (
	

	
)

// TruncateString truncates a string to the given maxWidth, and appends the
// given tail if the string is truncated.
//
// It ensures the visible width, including the width of the tail, is less than or
// equal to maxWidth.
//
// When [Options.ControlSequences] is true, 7-bit ANSI escape sequences that
// appear after the truncation point are preserved in the output. This ensures
// that escape sequences such as SGR resets are not lost, preventing color
// bleed in terminal output.
//
// [Options.ControlSequences8Bit] is ignored by truncation. 8-bit C1 byte values
// (0x80-0x9F) overlap with UTF-8 multi-byte encoding, so manipulating them
// during truncation can shift byte boundaries and form unintended visible
// characters. Use [Options.String] or [Options.Bytes] for 8-bit-aware width
// measurement.
func ( Options) ( string,  int,  string) string {
	// We deliberately ignore ControlSequences8Bit for truncation, see above.
	.ControlSequences8Bit = false

	 :=  - .String()

	var ,  int
	 := graphemes.FromString()
	.AnsiEscapeSequences = .ControlSequences

	for .Next() {
		 := graphemeWidth(.Value(), )
		if + <=  {
			 = .End()
		}
		 += 
		if  >  {
			if .ControlSequences {
				// Build result with trailing 7-bit ANSI escape sequences preserved
				var  strings.Builder
				.Grow(len() + len()) // at most original + tail
				.WriteString([:])
				.WriteString()

				 := graphemes.FromString([:])
				.AnsiEscapeSequences = .ControlSequences

				for .Next() {
					 := .Value()
					// Only preserve 7-bit escapes (ESC = 0x1B) that measure
					// as zero-width on their own; some sequences (e.g. SOS)
					// are only valid in their original context.
					if len() > 0 && [0] == 0x1B && .String() == 0 {
						.WriteString()
					}
				}
				return .String()
			}
			return [:] + 
		}
	}
	// No truncation
	return 
}

// TruncateString truncates a string to the given maxWidth, and appends the
// given tail if the string is truncated.
//
// It ensures the total width, including the width of the tail, is less than or
// equal to maxWidth.
func ( string,  int,  string) string {
	return DefaultOptions.TruncateString(, , )
}

// TruncateBytes truncates a []byte to the given maxWidth, and appends the
// given tail if the []byte is truncated.
//
// It ensures the visible width, including the width of the tail, is less than or
// equal to maxWidth.
//
// When [Options.ControlSequences] is true, 7-bit ANSI escape sequences that
// appear after the truncation point are preserved in the output. This ensures
// that escape sequences such as SGR resets are not lost, preventing color
// bleed in terminal output.
//
// [Options.ControlSequences8Bit] is ignored by truncation. 8-bit C1 byte values
// (0x80-0x9F) overlap with UTF-8 multi-byte encoding, so manipulating them
// during truncation can shift byte boundaries and form unintended visible
// characters. Use [Options.String] or [Options.Bytes] for 8-bit-aware width
// measurement.
func ( Options) ( []byte,  int,  []byte) []byte {
	// We deliberately ignore ControlSequences8Bit for truncation, see above.
	.ControlSequences8Bit = false

	 :=  - .Bytes()

	var ,  int
	 := graphemes.FromBytes()
	.AnsiEscapeSequences = .ControlSequences

	for .Next() {
		 := graphemeWidth(.Value(), )
		if + <=  {
			 = .End()
		}
		 += 
		if  >  {
			if .ControlSequences {
				// Build result with trailing 7-bit ANSI escape sequences preserved
				 := make([]byte, 0, len()+len()) // at most original + tail
				 = append(, [:]...)
				 = append(, ...)

				 := graphemes.FromBytes([:])
				.AnsiEscapeSequences = .ControlSequences

				for .Next() {
					 := .Value()
					// Only preserve 7-bit escapes (ESC = 0x1B) that measure
					// as zero-width on their own; some sequences (e.g. SOS)
					// are only valid in their original context.
					if len() > 0 && [0] == 0x1B && .Bytes() == 0 {
						 = append(, ...)
					}
				}
				return 
			}
			 := make([]byte, 0, +len())
			 = append(, [:]...)
			 = append(, ...)
			return 
		}
	}
	// No truncation
	return 
}

// TruncateBytes truncates a []byte to the given maxWidth, and appends the
// given tail if the []byte is truncated.
//
// It ensures the total width, including the width of the tail, is less than or
// equal to maxWidth.
func ( []byte,  int,  []byte) []byte {
	return DefaultOptions.TruncateBytes(, , )
}