# shellcheck shell=dash

___x_cmd log init npm
xrc:mod:lib     pkg     envvar
xrc:mod:lib     npm     nv    install   ddgo

___x_cmd_npm___main(){
    [ "$#" -gt 0 ] ||   set -- install --nv

    local op="$1";      shift
    case "$op" in
        info)           ___x_cmd_npm___info             "$@" ;;
        i|install)      ___x_cmd_npm___install          "$@" ;;
        :|ddgo)         ___x_cmd_npm_ddgo               "$@" ;;
        ::|ddgoai)      ___x_cmd_npm_ddgoai             "$@" ;;
        *)              ___x_cmd_npm___cmd npm "$op"    "$@" ;;
    esac
}

___x_cmd_npm___cmd(){
    ___x_cmd_hasbin "$1" || {
        if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
            npm:warn "npm command not found."
            local cmd=
            ___x_cmd ui select ,cmd  "What's next:"    \
                "x env use node"                \
                "x env try node"                \
                "x install node"                \
                "return 1"                      || return 1
            eval "$cmd"                         || return
        else
            npm:error --tip1 'x env use node' --tip2 'x env try node' "Npm not found."
            return 1
        fi
    }

    "$@"
}

___x_cmd_npm___help(){
    ___x_cmd help -m npm "$@" 1>&2
    return 1
}
