# shellcheck shell=dash disable=SC2016
___x_cmd_asdf_ls(){
    [ "$#" -gt 0 ] ||       set -- --app

    local op="$1"; shift
    case "$op" in
        -i|--installed)     ___x_cmd_asdf_ls___install         ;;
        -a|--all)           ___x_cmd_asdf_ls___all             ;;

        --app)              ___x_cmd_asdf_ls___app             ;;
        -h|--help)          ___x_cmd help -m asdf ls           ;;
        *)                  ___x_cmd_asdf___runmain list "$op" "$@" ;;
    esac
}

___x_cmd_asdf_ls___all___raw(){
    ___x_cmd ccmd 3d -- ___x_cmd asdf plugin list all
}

___x_cmd_asdf___plugin_lsname(){
    ___x_cmd_asdf_ls___all___raw | ___x_cmd_cmds_awk '{print $1}'
}

___x_cmd_asdf___plugin_url(){
    ___x_cmd_asdf_ls___all___raw | ___x_cmd_cmds_awk -v software="$1" '$1 == software { print $2; exit }'
}

___x_cmd_asdf___plugin_info(){
    ___x_cmd_asdf___plugin_url "$@"
}

___x_cmd_asdf_ls___install(){
    ___x_cmd_asdf___runmain list 2>&1 | ___x_cmd_cmds awk '$0 !~ "^[ ]"{ print $1 }'  | ___x_cmd_cmds sort
}

___x_cmd_asdf_ls___all(){
    if ___x_cmd_is_stdout2tty; then
        ___x_cmd_asdf_ls___app
    else
        ___x_cmd_asdf_ls___all___raw
    fi
}

___x_cmd_asdf_ls_fz_data(){
    ___x_cmd_asdf___plugin_lsname | {
        ___x_cmd fzf --ansi \
            --height 90% --layout=reverse \
            --preview='___x_cmdexe asdf --plugininfo {1}'
    }
}

___x_cmd_asdf_ls___app(){
    local data; data="$( ___X_CMD_RUNMODE="$___X_CMD_RUNMODE" ___x_cmdexe asdf --fzdata )" || return $?
    asdf:info "Package selected ->   $data"

    local software="$data"
    [ -n "$software" ] || {
        asdf:info "Exit because no software is selected."
        return 0
    }

    local url
    url="$(___x_cmd_asdf___plugin_url "$software")"
    [ -n "$url" ] || {
        asdf:info "Exit because no url is found."
        return 0
    }

    local id=
    ___x_cmd ui select id "What to do NEXT"     \
        "x asdf use       $software"     \
        "x asdf unuse     $software"     \
        "x browse         $url"          \
        "EXIT"     || return $?

    case "$id" in
        1)      ___x_cmd asdf use   "$software"    ;;
        2)      ___x_cmd asdf unuse "$software"    ;;
        3)      ___x_cmd browse     "$url"         ;;
        *)      return 0                           ;;
    esac
}

___x_cmd_asdf_la(){
    case "$1" in
        -h|--help) ___x_cmd help -m asdf la ; return 0 ;;
    esac
    ___x_cmd_asdf_ls___all
}



# ___x_cmd_asdf_ls___local(){
#     if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
#         asdf:info "Display current packages"
#         ___x_cmd_asdf___runmain current  2>&1  | ___x_cmd_cmds awk '{ print $1 }' | ___x_cmd pick --width 40 --col 10
#     else
#         ___x_cmd_asdf___runmain current  2>&1
#     fi
# }


# ___x_cmd_asdf_ls___available(){
#     if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
#         asdf:info "Find installable software"
#         ___X_CMD_PICK_SHBIN_CODE="xrc asdf" \
#             ___x_cmd pick --width 40 --col 10 ___x_cmd_asdf_ls___all
#         # ___x_cmd_asdf_install___app "$@"
#     else
#         ___x_cmd_asdf_ls___all
#     fi
# }
