package graphemes

import 

// FromString returns an iterator for the grapheme clusters in the input string.
// Iterate while Next() is true, and access the grapheme via Value().
func ( string) *Iterator[string] {
	return &Iterator[string]{
		split: splitFuncString,
		data:  ,
	}
}

// FromBytes returns an iterator for the grapheme clusters in the input bytes.
// Iterate while Next() is true, and access the grapheme via Value().
func ( []byte) *Iterator[[]byte] {
	return &Iterator[[]byte]{
		split: splitFuncBytes,
		data:  ,
	}
}

// Iterator is a generic iterator for grapheme clusters in strings or byte slices,
// with an ASCII hot path optimization.
type Iterator[ ~string | ~[]byte] struct {
	split func(, bool) (int, , error)
	data  
	pos   int
	start int
	// AnsiEscapeSequences treats 7-bit ANSI escape sequences (ECMA-48) as
	// single grapheme clusters when true. The default is false.
	//
	// 8-bit controls are not enabled by this option. See [AnsiEscapeSequences8Bit].
	AnsiEscapeSequences bool
	// AnsiEscapeSequences8Bit treats 8-bit C1 ANSI escape sequences (ECMA-48) as single
	// grapheme clusters when true. The default is false.
	//
	// 8-bit control bytes are not UTF-8 encoded, i.e. not valid UTF-8. If you
	// choose this option, you are choosing to interpret non-UTF-8 data, caveat
	// emptor.
	AnsiEscapeSequences8Bit bool
}

var (
	splitFuncString = splitFunc[string]
	splitFuncBytes  = splitFunc[[]byte]
)

const (
	esc = 0x1B
	cr  = 0x0D
	bel = 0x07
	can = 0x18
	sub = 0x1A
	st  = 0x9C
)

// Next advances the iterator to the next grapheme cluster.
// Returns false when there are no more grapheme clusters.
func ( *Iterator[]) () bool {
	if .pos >= len(.data) {
		return false
	}
	.start = .pos

	 := .data[.pos]
	if .AnsiEscapeSequences &&  == esc {
		if  := ansiEscapeLength(.data[.pos:]);  > 0 {
			.pos += 
			return true
		}
	}
	if .AnsiEscapeSequences8Bit &&  >= 0x80 &&  <= 0x9F {
		if  := ansiEscapeLength8Bit(.data[.pos:]);  > 0 {
			.pos += 
			return true
		}
	}

	// ASCII hot path: any ASCII is one grapheme when next byte is ASCII or end.
	if  < utf8.RuneSelf &&  != cr {
		if .pos+1 >= len(.data) || .data[.pos+1] < utf8.RuneSelf {
			.pos++
			return true
		}
	}

	// Fall back to UAX29 grapheme parsing
	 := .data[.pos:]
	, ,  := .split(, true)
	if  != nil {
		panic()
	}
	if  <= 0 {
		panic("splitFunc returned a zero or negative advance")
	}
	.pos += 
	if .pos > len(.data) {
		panic("splitFunc advanced beyond end of data")
	}
	return true
}

// Value returns the current grapheme cluster.
func ( *Iterator[]) ()  {
	return .data[.start:.pos]
}

// Start returns the byte position of the current grapheme in the original data.
func ( *Iterator[]) () int {
	return .start
}

// End returns the byte position after the current grapheme in the original data.
func ( *Iterator[]) () int {
	return .pos
}

// Reset resets the iterator to the beginning of the data.
func ( *Iterator[]) () {
	.start = 0
	.pos = 0
}

// SetText sets the data for the iterator to operate on, and resets all state.
func ( *Iterator[]) ( ) {
	.data = 
	.start = 0
	.pos = 0
}

// First returns the first grapheme cluster without advancing the iterator.
func ( *Iterator[]) ()  {
	if len(.data) == 0 {
		return .data
	}

	// Use a copy to leverage Next()'s ASCII optimization
	 := *
	.pos = 0
	.start = 0
	.Next()
	return .Value()
}