package shlex

import (
	
)

type TokenSlice []Token

func ( TokenSlice) () []string {
	 := make([]string, 0, len())
	for ,  := range  {
		 = append(, .Value)
	}
	return 
}

func ( TokenSlice) () []TokenSlice {
	 := make([]TokenSlice, 0)

	 := make(TokenSlice, 0)
	 := 0
	for ,  := range  {
		switch {
		case .WordbreakType == WORDBREAK_SUBSTITUTION_OPEN:
			++
			 = append(, )
		case .WordbreakType == WORDBREAK_SUBSTITUTION_CLOSE:
			if  > 0 {
				--
			}
			 = append(, )
		case  > 0:
			 = append(, )
		case .Type == WORDBREAK_TOKEN && .WordbreakType.IsPipelineDelimiter():
			 = append(, )
			 = make(TokenSlice, 0)
		default:
			 = append(, )
		}
	}
	return append(, )
}

func ( TokenSlice) () TokenSlice {
	 := .pipelines()
	return [len()-1]
}

func ( TokenSlice) () TokenSlice {
	 := make(TokenSlice, 0)
	for ,  := range  {
		switch {
		case  == 0:
			 = append(, )
		case [-1].adjoins():
			[len()-1].Value += .Value
			[len()-1].RawValue += .RawValue
			[len()-1].Span.End = .Span.End
			[len()-1].State = .State
		default:
			 = append(, )
		}
	}
	return 
}

func ( TokenSlice) () TokenSlice {
	 := make(TokenSlice, 0)
	for ,  := range  {
		switch .Type {
		case WORDBREAK_TOKEN:
			if .WordbreakType.IsRedirect() {
				continue
			}
		}

		if  > 0 {
			if [-1].WordbreakType.IsRedirect() {
				continue
			}
		}

		if  < len()-1 {
			 := [+1]
			if .adjoins() {
				if ,  := strconv.Atoi(.RawValue);  == nil {
					if [+1].WordbreakType.IsRedirect() {
						continue
					}
				}
			}
		}

		 = append(, )
	}
	return 
}

// wordsWithSubstitutions merges tokens into words, treating closed
// substitution scopes as single words. When a WORDBREAK_SUBSTITUTION_OPEN
// is encountered, all tokens until the matching WORDBREAK_SUBSTITUTION_CLOSE
// are merged into one word. Unclosed substitution scopes (cursor inside)
// are left as separate tokens — the caller should use the inner tokens
// to build a separate completion context.
func ( TokenSlice) () TokenSlice {
	 := make(TokenSlice, 0)
	 := 0
	var  *Token

	for ,  := range  {
		switch {
		case .WordbreakType == WORDBREAK_SUBSTITUTION_OPEN:
			if  == 0 {
				 = &Token{
					Type: WORD_TOKEN,
					Span: .Span,
				}
			}
			++
			if  != nil {
				if .Span.End != .Span.Start && .RawValue != "" {
					.RawValue += " "
					.Value += " "
				}
				.RawValue += .RawValue
				.Value += .RawValue
				.Span.End = .Span.End
				.State = .State
			}

		case .WordbreakType == WORDBREAK_SUBSTITUTION_CLOSE:
			if  > 0 {
				if  != nil {
					if .Span.End != .Span.Start && .RawValue != "" {
						.RawValue += " "
						.Value += " "
					}
					.RawValue += .RawValue
					.Value += .RawValue
					.Span.End = .Span.End
					.State = .State
				}
				--
				if  == 0 {
					 = append(, *)
					 = nil
				}
			}

		case  > 0:
			if  != nil {
				// Insert a space when tokens don't adjoin (gap = space
				// tokens that the lexer skipped). The substitution word
				// uses RawValue for both Value and RawValue since the
				// inner content is opaque to the outer command.
				if .Span.End != .Span.Start && .RawValue != "" {
					.RawValue += " "
					.Value += " "
				}
				.RawValue += .RawValue
				.Value += .RawValue
				.Span.End = .Span.End
				.State = .State
			}

		default:
			if  == 0 {
				 = append(, )
			} else if [-1].adjoins() {
				[len()-1].Value += .Value
				[len()-1].RawValue += .RawValue
				[len()-1].Span.End = .Span.End
				[len()-1].State = .State
			} else {
				 = append(, )
			}
		}
	}

	if  > 0 {
		// Unclosed substitution — don't append the partial word;
		// the inner context will be built separately.
		return 
	}
	return 
}

func ( TokenSlice) () ( Token) {
	if len() > 0 {
		 = [len()-1]
	}
	return
}

func ( TokenSlice) () string {
	if len() == 0 {
		return ""
	}
	 := false
	 := ""

	 := [len()-1]
	switch .State {
	case QUOTING_STATE, QUOTING_ESCAPING_STATE, ESCAPING_QUOTED_STATE,
		QUOTING_TRIPLE_STATE, QUOTING_TRIPLE_ESCAPING_STATE:
		// Seems bash handles the last opening quote as wordbreak when in quoting state.
		// So add value up to last opening quote to prefix.
		 = true
		 = .Value[:.WordbreakIndex]
	}

	for  := len() - 2;  >= 0; -- {
		 := []
		if !.adjoins([+1]) {
			break
		}

		if .Type == WORDBREAK_TOKEN && .Value == "@" {
			if ! {
				// @ at the boundary is always skipped
				 = true
				continue
			}
			// @ after boundary: skip if adjacent to another wordbreak,
			// include if between two WORD tokens (e.g. user@host)
			 :=  > 0 && [-1].Type == WORDBREAK_TOKEN
			 := +1 < len() && [+1].Type == WORDBREAK_TOKEN
			if  ||  {
				continue
			}
		}

		if ! && .Type == WORDBREAK_TOKEN {
			 = true
		}

		if  {
			 = .Value + 
		}
	}
	return 
}