package carapace

import (
	
	
	
	

	
	
	
)

func addCompletionCommand( *cobra.Command) {
	for ,  := range .Commands() {
		if .Name() == "_carapace" {
			return
		}
	}

	 := &cobra.Command{
		Use:    "_carapace",
		Hidden: true,
		Run: func( *cobra.Command,  []string) {
			LOG.Print(strings.Repeat("-", 80))
			LOG.Printf("%#v", os.Args)

			if len() > 2 && strings.HasPrefix([2], "_") {
				.Hidden = false
			}

			if !.HasParent() {
				panic("missing parent command") // this should never happen
			}

			 := .Parent()
			if .Annotations[annotation_standalone] == "true" {
				// TODO how to handle an explicit `_carapace` command?
				.RemoveCommand() // don't complete local `_carapace` in standalone mode
			}

			if ,  := complete(, );  != nil {
				fmt.Fprintln(io.MultiWriter(.OutOrStderr(), LOG.Writer()), .Error())
			} else {
				fmt.Fprintln(io.MultiWriter(.OutOrStdout(), LOG.Writer()), )
			}
		},
		FParseErrWhitelist: cobra.FParseErrWhitelist{
			UnknownFlags: true,
		},
		DisableFlagParsing: true,
	}

	.AddCommand()

	Carapace{}.PositionalCompletion(
		ActionStyledValues(
			"bash", "#d35673",
			"bash-ble", "#c2039a",
			"cmd-clink", "#2B3436",
			"elvish", "#ffd6c9",
			"export", style.Default,
			"fish", "#7ea8fc",
			"ion", "#0e5d6d",
			"nushell", "#29d866",
			"oil", "#373a36",
			"powershell", "#e8a16f",
			"tcsh", "#412f09",
			"xonsh", "#a8ffa9",
			"zsh", "#efda53",
		),
		ActionValues(.Root().Name()),
	)
	Carapace{}.PositionalAnyCompletion(
		ActionCallback(func( Context) Action {
			 := []string{"_carapace", "export", ""}
			 = append(, .Args[2:]...)
			 = append(, .Value)

			,  := os.Executable()
			if  != nil {
				return ActionMessage(.Error())
			}
			return ActionExecCommand(, ...)(func( []byte) Action { // TODO does not work with sandbox tests for `example _carapace ...`
				if string() == "" {
					return ActionValues()
				}
				return ActionImport()
			})
		}),
	)

	 := &cobra.Command{
		Use: "spec",
		Run: func( *cobra.Command,  []string) {
			fmt.Fprint(.OutOrStdout(), spec.Spec())
		},
	}
	.AddCommand()

	 := &cobra.Command{
		Use:  "style",
		Args: cobra.ExactArgs(1),
		Run:  func( *cobra.Command,  []string) {},
	}
	.AddCommand()

	 := &cobra.Command{
		Use:  "set",
		Args: cobra.MinimumNArgs(1),
		Run: func( *cobra.Command,  []string) {
			for ,  := range  {
				if  := strings.SplitN(, "=", 2); len() == 2 {
					if  := style.Set([0], [1]);  != nil {
						fmt.Fprint(.ErrOrStderr(), .Error())
					}
				} else {
					fmt.Fprintf(.ErrOrStderr(), "invalid format: '%v'", )
				}
			}
		},
	}
	.AddCommand()
	Carapace{}.PositionalAnyCompletion(
		ActionStyleConfig(),
	)
}