package console

import (
	

	
	
	completer 
	

	
	
)

func ( *Console) ( []rune,  int) readline.Completions {
	 := .activeMenu()

	// Ensure the carapace library is called so that the function
	// completer.Complete() variable is correctly initialized before use.
	carapace.Gen(.Command)

	// Split the line as shell words, only using
	// what the right buffer (up to the cursor)
	, ,  := completion.SplitArgs(, )

	// Prepare arguments for the carapace completer
	// (we currently need those two dummies for avoiding a panic).
	 = append([]string{.name, "_carapace"}, ...)

	// Call the completer with our current command context.
	,  := completer.Complete(.Command, ...)

	// The completions are never nil: fill out our own object
	// with everything it contains, regardless of errors.
	 := make([]readline.Completion, len(.Values))

	for ,  := range .Values {
		[] = readline.Completion{
			Value:       line.UnescapeValue(, , .Value),
			Display:     .Display,
			Description: .Description,
			Style:       style.SGR(.Style),
			Tag:         .Tag,
		}

		if !.Nospace.Matches(.Value) {
			[].Value = .Value + " "
		}

		// Remove short/long flags grouping
		// join to single tag group for classic zsh side-by-side view
		switch .Tag {
		case "shorthand flags", "longhand flags":
			[].Tag = "flags"
		}
	}

	// Assign both completions and command/flags/args usage strings.
	 := readline.CompleteRaw()
	 = .Usage("%s", .Usage)
	 = .justifyCommandComps()

	// If any errors arose from the completion call itself.
	if  != nil {
		 = readline.CompleteMessage("failed to load config: " + .Error())
	}

	// Completion status/errors
	for ,  := range .Messages.Get() {
		 = .Merge(readline.CompleteMessage())
	}

	// Suffix matchers for the completions if any.
	,  := .Nospace.MarshalJSON()
	if len() > 0 &&  == nil {
		 = .NoSpace([]rune(string())...)
	}

	// If we have a quote/escape sequence unaccounted
	// for in our completions, add it to all of them.
	 = .Prefix()
	.PREFIX = 

	// Finally, reset our command tree for the next call.
	completer.ClearStorage()
	.resetPreRun()
	.hideFilteredCommands(.Command)

	return 
}

// justifyCommandComps justifies the descriptions for all commands in all groups
// to the same level, for prettiness. Also, removes any coloring from them, as currently,
// the carapace engine does add coloring to each group, and we don't want this.
func ( *Console) ( readline.Completions) readline.Completions {
	 := []string{}

	.EachValue(func( readline.Completion) readline.Completion {
		if !strings.HasSuffix(.Tag, "commands") {
			return 
		}

		 = append(, .Tag)
		.Style = "" // Remove command coloring

		return 
	})

	if len() > 0 {
		return .JustifyDescriptions(...)
	}

	return 
}

// highlightSyntax - Entrypoint to all input syntax highlighting in the Wiregost console.
func ( *Console) ( []rune) string {
	// Split the line as shellwords
	, ,  := line.Split(string(), true)
	if  != nil {
		 = append(, )
	}

	 := make([]string, 0)          // List of processed words, append to
	 :=                      // List of words to process, draw from
	 := line.TrimSpaces() // Match stuff against trimmed words

	// Highlight the root command when found.
	, ,  := .activeMenu().Find()
	if  != nil {
		,  = line.HighlightCommand(, , .activeMenu().Command, .cmdHighlight)
	}

	// Highlight command flags
	,  = line.HighlightCommandFlags(, , .flagHighlight)

	// Done with everything, add remainind, non-processed words
	 = append(, ...)

	// Join all words.
	 := strings.Join(, "")

	return 
}