# shellcheck shell=dash

___x_cmd_luarocks_lsremote(){
    [ $# -gt 0 ]    ||  set - --nv

    local op="$1";   shift
    case "$op" in
        --nv)       ___x_cmd_luarocks_lsremote_nv               ;;
        *)          ___x_cmd_luarocks_lsremote_grep "$op"  "$@" ;;
    esac
}

___x_cmd_luarocks_lsremote_grep(){
    ___x_cmd_luarocks_lsremote_nv___ls | ___x_cmd_cmds grep "$@"
}

___x_cmd_luarocks_lsremote_nv(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
        ___x_cmd_luarocks_lsremote_nv___app
    else
        ___x_cmd_luarocks_lsremote_nv___ls
    fi
}

___x_cmd_luarocks_lsremote_nv___ls(){
    [ -f "$___X_CMD_LUAROCKS_MANIFEST_LIST_FP" ] || return 1
    ___x_cmd_cmds cat "$___X_CMD_LUAROCKS_MANIFEST_LIST_FP"
}

___x_cmd_luarocks_lsremote_nv___app(){
    [ -f "$___X_CMD_LUAROCKS_MANIFEST_LIST_FP" ] || {
        luarocks:info "The first use of luarocks is detected and the local software list is updated."
        ___x_cmd pkg update
    }

    luarocks:info "Use 'x luarocks -h' for help."
    luarocks:info "Select a package to install, or view information."
    # TODO: ___X_CMD_LUAROCKS_MANIFEST_LIST_FP need pkg-mod
    local x_="";
    ___X_CMD_PICK_SHBIN_CODE="xrc pkg luarocks;" \
        ___x_cmd storeread_  x pick --width 40 --col 10 ___x_cmd_luarocks_lsremote_nv___ls
    local software="$x_"
    [ -n "$software" ] || return 1

    local id=; local cmd=
    ___x_cmd ui select id,cmd  "What to do NEXT" \
        "x luarocks search  $software"    \
        "x luarocks install $software"    \
        "ABORT"
    case "$id" in
        3)      return 1    ;;
        *)      eval "$cmd" ;;
    esac
}

# Put it in to pkg ... luarocks/manifest.list
___x_cmd_luarocks_lsremote_gen(){
    ___x_cmd ccmd 1d -- ___x_cmd curl https://luarocks.org/manifest.json | x jq -r '.repository | keys | .[]'
}
