package bash

import (
	
	
	

	shlex 
	
	
)

var sanitizer = strings.NewReplacer(
	"\n", ``,
	"\r", ``,
	"\t", ``,
)

var escapingQuotedReplacer = strings.NewReplacer(
	`\`, `\\`,
	`"`, `\"`,
	`$`, `\$`,
	"`", "\\`",
)

var escapingReplacer = strings.NewReplacer(
	`\`, `\\`,
	`&`, `\&`,
	`<`, `\<`,
	`>`, `\>`,
	"`", "\\`",
	`'`, `\'`,
	`"`, `\"`,
	`{`, `\{`,
	`}`, `\}`,
	`$`, `\$`,
	`#`, `\#`,
	`|`, `\|`,
	`?`, `\?`,
	`(`, `\(`,
	`)`, `\)`,
	`;`, `\;`,
	` `, `\ `,
	`[`, `\[`,
	`]`, `\]`,
	`*`, `\*`,
)

var displayReplacer = strings.NewReplacer(
	`${`, `\\\${`,
)

func commonPrefix(,  string) string {
	 := 0
	for  < len() &&  < len() && [] == [] {
		++
	}
	return [0:]
}

func commonDisplayPrefix( ...common.RawValue) ( string) {
	for ,  := range  {
		if  == 0 {
			 = .Display
		} else {
			 = commonPrefix(, .Display)
		}
	}
	return
}

func commonValuePrefix( ...common.RawValue) ( string) {
	for ,  := range  {
		if  == 0 {
			 = .Value
		} else {
			 = commonPrefix(, .Value)
		}
	}
	return
}

// ActionRawValues formats values for bash.
func ( string,  common.Meta,  common.RawValues) string {
	 := shlex.Complete(env.Compline(), shlex.Bash)
	 := .QuotingState == shlex.QUOTING_STATE

	for ,  := range  {
		[].Value = strings.TrimPrefix(.Value, wordbreakPrefix)
	}

	 := strings.TrimPrefix(, wordbreakPrefix) // last segment of currentWord split by COMP_WORDBREAKS

	// bash replaces the current word with the longest common prefix of all candidates,
	// so candidates that don't extend the current word would erase typed characters
	// (e.g. `~steam/D` also matching `~steam/d1/` case-insensitively).
	 := make(common.RawValues, 0, len())
	for ,  := range  {
		if strings.HasPrefix(.Value, ) {
			 = append(, )
		}
	}
	if len() > 0 && len() < len() {
		 = 
	}

	if len() > 1 && commonDisplayPrefix(...) != "" {
		// When all display values have the same prefix bash will insert it as partial completion (which skips prefixes/formatting).
		if  := commonValuePrefix(...);  !=  && strings.HasPrefix(, ) {
			// replace values with common value prefix as it extends the current word
			 = common.RawValuesFrom()
		} else {
			// prevent insertion of partial display values by prefixing one with space
			[0].Display = " " + [0].Display
		}
		.Nospace.Add('*')
	}

	 := false
	 := make([]string, len())
	for ,  := range  {
		if len() == 1 || compType != COMP_TYPE_LIST_SUCCESSIVE_TABS {
			 =  || .Nospace.Matches(.Value)

			[] = sanitizer.Replace(.Value)
			switch {
			case :
				// Readline retains the opening single quote, so use matching quotes.
				[] = "'" + strings.ReplaceAll([], "'", `'\''`) + "'"
			case strings.HasPrefix([], "~"): // assume homedir expansion
				[] = escapingReplacer.Replace([])
			case requiresQuoting([]):
				[] = fmt.Sprintf(`"%v"`, escapingQuotedReplacer.Replace([]))
			}
		} else {
			 = true
			.Display = displayReplacer.Replace(.Display)
			.Description = displayReplacer.Replace(.Description)
			if .Description != "" {
				[] = fmt.Sprintf("%v (%v)", .Display, sanitizer.Replace(.TrimmedDescription()))
			} else {
				[] = .Display
			}
		}
	}
	return fmt.Sprintf("%v\001%v", , strings.Join(, "\n"))
}

func requiresQuoting( string) bool {
	 := " \t\r\n`" + `[]{}()<>;|$&:*#`
	 += `'"`
	 += os.Getenv("COMP_WORDBREAKS")
	 += `\`
	return strings.ContainsAny(, )

}