package keymap

import (
	
	
	
	
	

	
)

// readline global options specific to this library.
var readlineOptions = map[string]interface{}{
	// General edition
	"autopairs": false,

	// Completion
	"autocomplete":               false,
	"completion-list-separator":  "--",
	"completion-selection-style": "\x1b[1;30m",

	// Prompt & General UI
	"transient-prompt":          false,
	"usage-hint-always":         false,
	"history-autosuggest":       false,
	"multiline-column":          true,
	"multiline-column-numbered": false,
}

// ReloadConfig parses all valid .inputrc configurations and immediately
// updates/reloads all related settings (editing mode, variables behavior, etc.)
func ( *Engine) ( ...inputrc.Option) ( error) {
	// Builtin Go binds (in addition to default readline binds)
	.loadBuiltinOptions()
	.loadBuiltinBinds()

	,  := user.Current()

	// Parse library-specific configurations.
	//
	// This library implements various additional commands and keymaps.
	// Parse the configuration with a specific App name, ignoring errors.
	inputrc.UserDefault(, .config, inputrc.WithApp("go"))

	// Parse user configurations.
	//
	// Those default settings are the base options often needed
	// by /etc/inputrc on various Linux distros (for special keys).
	 := []inputrc.Option{
		inputrc.WithMode("emacs"),
		inputrc.WithTerm(os.Getenv("TERM")),
	}

	 = append(, ...)

	// This will only overwrite binds that have been
	// set in those configs, and leave the default ones
	// (those just set above), so as to keep most of the
	// default functionality working out of the box.
	 = inputrc.UserDefault(, .config, ...)
	if  != nil {
		return 
	}

	// Some configuration variables might have an
	// effect on our various keymaps and bindings.
	.overrideBindsSpecial()

	// Startup editing mode
	switch .config.GetString("editing-mode") {
	case "emacs":
		.main = Emacs
	case "vi":
		.main = ViInsert
	}

	return nil
}

// loadBuiltinOptions loads some options specific to
// this library, if they are not loaded already.
func ( *Engine) () {
	for ,  := range readlineOptions {
		if  := .config.Get();  == nil {
			.config.Set(, )
		}
	}
}

// loadBuiltinBinds adds additional command mappings that are not part
// of the standard C readline configuration: those binds therefore can
// reference commands or keymaps only implemented/used in this library.
func ( *Engine) () {
	// Emacs specials
	for ,  := range emacsKeys {
		.config.Binds[string(Emacs)][] = 
	}

	// Vim main keymaps
	for ,  := range vicmdKeys {
		.config.Binds[string(ViCommand)][] = 
		.config.Binds[string(ViMove)][] = 
		.config.Binds[string(Vi)][] = 
	}

	for ,  := range viinsKeys {
		.config.Binds[string(ViInsert)][] = 
	}

	// Vim local keymaps
	.config.Binds[string(Visual)] = visualKeys
	.config.Binds[string(ViOpp)] = vioppKeys
	.config.Binds[string(MenuSelect)] = menuselectKeys
	.config.Binds[string(Isearch)] = menuselectKeys

	// Default TTY binds
	for ,  := range .config.Binds {
		[inputrc.Unescape(`\C-C`)] = inputrc.Bind{Action: "abort"}
	}
}

// overrideBindsSpecial overwrites some binds as dictated by the configuration variables.
func ( *Engine) () {
	// Disable completion functions if required
	if .config.GetBool("disable-completion") {
		for ,  := range .config.Binds {
			for ,  := range  {
				switch .Action {
				case "complete", "menu-complete", "possible-completions":
					[] = inputrc.Bind{Action: "self-insert"}
				}
			}
		}
	}
}

func printBindsReadable( []string,  map[string][]string) {
	for ,  := range  {
		 := []
		sort.Strings()

		switch {
		case len() == 0:
		case len() > 5:
			var  []string

			for  := 0;  < 5; ++ {
				 = append(, "\""+[]+"\"")
			}

			 := strings.Join(, ", ")
			fmt.Printf("%s can be found on %s ...\n", , )

		default:
			var  []string

			for ,  := range  {
				 = append(, "\""++"\"")
			}

			 := strings.Join(, ", ")
			fmt.Printf("%s can be found on %s\n", , )
		}
	}
}

func printBindsInputrc( []string,  map[string][]string) {
	for ,  := range  {
		 := []
		sort.Strings()

		if len() > 0 {
			for ,  := range  {
				fmt.Printf("\"%s\": %s\n", , )
			}
		}
	}
}