openagent set image {
    $ARGS[1] -> set file
    config get open image -> set mode

    if { = mode==`auto` } {
        switch {
            case { $TMUX }                 { set mode=compatible }
            case { $KITTY_WINDOW_ID }      { set mode=kitty }
            case { = TERM==`xterm-kitty` } { set mode=kitty }
            case { $TERMINOLOGY }          { set mode=terminology }
            case { = TERM_PROGRAM==`iTerm.app` } { set mode=iterm }
            case { $ITERM_PROFILE }        { set mode=iterm }
            catch                          { set mode=compatible }
        }
    }

    # If Kitty but running inside a screen / tmux session, fallback to compatible.
    # We do this because tmux doesn't support reporting screen sizes via the TIOCGWINSZ ioctl.
    if { and { = mode==`kitty` } { = TERM==`screen` } } {
        set mode=compatible
    }

    switch $mode {
        case compatible {
            open-image $file
        }

        case kitty {
            try {
                kitty icat $file
            }
            catch {
                err "Error running `kitty`. Please check `kitty` is installed."
            }
        }

        case iterm {
            out "{ESC}]1337;File=inline=1:${base64 $file}{^G}"
        }

        case terminology {
            try {
                tycat -c $file
            }
            catch {
                err "Error running `tycat`. Please check `terminology` is installed."
            }
        }

        case sixel {
            try {
                img2sixel $file
            }
            catch {
                err "Error running `img2sixel`. Please check `libsixel` is installed."
            }
        }

        catch {
            out "Invalid rendering method. Please define in `config set open image` - 'auto' is recommended."
        }
    }
}

if { which: microk8s } then {

    private autocomplete.microk8s {
        # Top level completion results for microk8s
        microk8s -> tabulate -> cast str
    }

    test: unit private autocomplete.microk8s {
        "StdoutType":  "str",
        "StdoutBlock": ({
            -> len -> set len;
            if { = len>0 } then {
                out "Len greater than 0"
            } else {
                err "No elements returned"
            }
        }),
        "StdoutIsArray": true
    }

    private autocomplete.microk8s.kubectl {
        # Top level completion results for microk8s kubectl
        microk8s kubectl help -> tabulate: --map
    }

    test: unit private autocomplete.microk8s.kubectl {
        "StdoutType":  "json",
        "StdoutBlock": ({
            -> len -> set len;
            if { = len>0 } then {
                out "Len greater than 0"
            } else {
                err "No elements returned"
            }
        }),
        "StdoutIsMap": true
    }

    private autocomplete.microk8s.kubectl.dyndesc {
        # Dynamic completion for microk8s kubectl
        microk8s kubectl help $ARGS[1] -> @[^Options..^Usage]re -> regexp "s/:/\t/" -> tabulate: --key-inc-hint --map --split-comma
    }

    autocomplete: set microk8s {[
        {
            "Dynamic": ({ autocomplete.microk8s }),
            "AllowMultiple": false,
            "FlagValues": {
                "kubectl": [
                    {
                        "DynamicDesc":   ({ autocomplete.microk8s.kubectl }),
                        "ListView": true,
                        "AllowMultiple": false,
                        "Optional": false
                    },
                    {
                        #"DynamicDesc":   ({ autocomplete.microk8s.kubectl.dyndesc $ARGS[2] }),
                        "Dynamic": ({ out @ARGS }),
                        "AllowMultiple": true,
                        "AnyValue":      true
                        #"ListView":      true
                    }
                ]
            }
        }
    ]}

    autocomplete: set microk8s.kubectl {[
        {
            "DynamicDesc":   ({ autocomplete.microk8s.kubectl }),
            "ListView": true,
            "AllowMultiple": false,
            "Optional": false
        },
        {
            "DynamicDesc":   ({ autocomplete.microk8s.kubectl.dyndesc $ARGS[1] }),
            #"Dynamic": ({ out @ARGS }),
            "AllowMultiple": true,
            "AnyValue":      true,
            "ListView":      true
        }
    ]}

    autocomplete: set kubectl {[
        {
            "DynamicDesc":   ({ autocomplete.microk8s.kubectl }),
            "ListView": true,
            "AllowMultiple": false,
            "Optional": false
        },
        {
            "DynamicDesc":   ({ autocomplete.microk8s.kubectl.dyndesc $ARGS[2] }),
            #"Dynamic": ({ out @ARGS }),
            "AllowMultiple": true,
            "AnyValue":      true,
            "ListView":      true
        }
    ]}
}