package shlex

// posixSubstitutionPostProcess reclassifies ( and ) as substitution
// delimiters and merges $ + ( adjacency into a single opener token.
//
// In POSIX shells (bash, zsh, oil, tcsh), ( and ) are in BASH_WORDBREAKS
// (or TCSH_WORDBREAKS) and classified as WORDBREAK_UNKNOWN by the operator
// grammar. This post-pass:
//
//  1. Merges a $ WORD_TOKEN immediately followed by an adjacent ( WORDBREAK
//     into a single WORDBREAK_SUBSTITUTION_OPEN token with RawValue "$(".
//  2. Detects $(( for arithmetic by checking if the next token after $(
//     is another ( — if so, merges both into a single opener "$((".
//  3. Reclassifies standalone ( as WORDBREAK_SUBSTITUTION_OPEN (for
//     process substitution <(, >, and bare () in csh).
//  4. Reclassifies ) as WORDBREAK_SUBSTITUTION_CLOSE.
//     For arithmetic $((...)), the first ) closes arithmetic, the second
//     closes the command substitution — detected by tracking depth and
//     the opener's RawValue.
func posixSubstitutionPostProcess( TokenSlice) TokenSlice {
	 := make(TokenSlice, 0, len())
	 := 0
	 := 0 // tracks how many openers were arithmetic ($(( )

	 := 0
	for  < len() {
		 := []

		// Detect $ + ( adjacency → merge into WORDBREAK_SUBSTITUTION_OPEN
		if .Type == WORD_TOKEN && .Value == "$" && +1 < len() {
			 := [+1]
			if .Type == WORDBREAK_TOKEN && .Value == "(" && .adjoins() {
				// Check for $(( arithmetic
				if +2 < len() && [+2].Type == WORDBREAK_TOKEN &&
					[+2].Value == "(" && .adjoins([+2]) {
					 := Token{
						Type:          WORDBREAK_TOKEN,
						Value:         "$(",
						RawValue:      .RawValue + .RawValue + [+2].RawValue,
						Span:          Span{Start: .Span.Start, End: [+2].Span.End},
						State:         [+2].State,
						WordbreakType: WORDBREAK_SUBSTITUTION_OPEN,
					}
					 = append(, )
					++
					++
					 += 3
					continue
				}
				// Regular $( command substitution
				 := Token{
					Type:          WORDBREAK_TOKEN,
					Value:         "$(",
					RawValue:      .RawValue + .RawValue,
					Span:          Span{Start: .Span.Start, End: .Span.End},
					State:         .State,
					WordbreakType: WORDBREAK_SUBSTITUTION_OPEN,
				}
				 = append(, )
				++
				 += 2
				continue
			}
		}

		// Detect process substitution: <(, >(, or >>( — merge redirect
		// operator + ( into opener. Only single-char redirect values
		// (<, >) or append (>>) qualify for process substitution.
		if .Type == WORDBREAK_TOKEN && .WordbreakType.IsRedirect() &&
			(.Value == "<" || .Value == ">" || .Value == ">>") && +1 < len() {
			 := [+1]
			if .Type == WORDBREAK_TOKEN && .Value == "(" && .adjoins() {
				 := Token{
					Type:          WORDBREAK_TOKEN,
					Value:         .Value + "(",
					RawValue:      .RawValue + .RawValue,
					Span:          Span{Start: .Span.Start, End: .Span.End},
					State:         .State,
					WordbreakType: WORDBREAK_SUBSTITUTION_OPEN,
				}
				 = append(, )
				++
				 += 2
				continue
			}
		}

		// Reclassify standalone ( as WORDBREAK_SUBSTITUTION_OPEN
		// (for bare () in csh)
		if .Type == WORDBREAK_TOKEN && .Value == "(" {
			 := Token{
				Type:          .Type,
				Value:         .Value,
				RawValue:      .RawValue,
				Span:          .Span,
				State:         .State,
				WordbreakType: WORDBREAK_SUBSTITUTION_OPEN,
			}
			 = append(, )
			++
			++
			continue
		}

		// Reclassify ) as WORDBREAK_SUBSTITUTION_CLOSE
		if .Type == WORDBREAK_TOKEN && .Value == ")" {
			// When arithmetic is open, try to merge two adjacent ) into ))
			// to close the arithmetic scope $((...)). We don't require
			// depth == arithmeticDepth because a command substitution
			// like $(echo $((1+2)) has depth=2 but arith=1 at the ),
			// and the two ) are still adjacent.
			if  > 0 {
				if +1 < len() && [+1].Type == WORDBREAK_TOKEN &&
					[+1].Value == ")" && .adjoins([+1]) {
					// Merge both ) into a single closer for arithmetic
					 := Token{
						Type:          WORDBREAK_TOKEN,
						Value:         "))",
						RawValue:      .RawValue + [+1].RawValue,
						Span:          Span{Start: .Span.Start, End: [+1].Span.End},
						State:         [+1].State,
						WordbreakType: WORDBREAK_SUBSTITUTION_CLOSE,
					}
					 = append(, )
					--
					--
					 += 2
					continue
				}
			}
			// Regular close — decrement depth only (clamped at 0)
			 := Token{
				Type:          .Type,
				Value:         .Value,
				RawValue:      .RawValue,
				Span:          .Span,
				State:         .State,
				WordbreakType: WORDBREAK_SUBSTITUTION_CLOSE,
			}
			 = append(, )
			if  > 0 {
				--
			}
			++
			continue
		}

		// Pass through everything else
		 = append(, )
		++
	}

	return 
}