# shellcheck shell=dash
x log init nu
xrc:mod:lib nu      __setup ddgo    __binexec

___x_cmd_nu___main(){
    [ "$#" -gt 0 ]  || {
        ___x_cmd_nu___binexec nu
        return
    }

    local op="$1"; shift
    case "$op" in
        -h|--help)          ___x_cmd help -m nu                 "$@" ;;
        :|ddgo)             ___x_cmd_nu_ddgo                    "$@" ;;
        setup|--setup)      ___x_cmd_nu___setup                 "$@" ;;

        --sysco)            ___x_cmd_nu___sysco                 "$@" ;;
        --setup-is-required)
                            ___x_cmd_nu___setup_is_required     "$@" ;;

        # <inner function>
        --binexec)          ___x_cmd_nu___binexec       "$op"   "$@" ;;
        --|--exec)          ___x_cmd_nu___binexec nu            "$@" ;;

        # TODO: experimental, don't expose
        ---|--runcode)      ___x_cmd_nu___runcode               "$@" ;;
        select|slct)        ___x_cmd_nu___runcode select        "$@" ;;
        # <inner function>

        *)                  ___x_cmd_nu___binexec nu    "$op"   "$@" ;;
    esac
}

___x_cmd_nu___sysco(){
    ___X_CMD_CO_EXEC_SHELL=nushell ___x_cmd co --exec "$@"
}

# x nu -c 'ls | where name == latest'       # still the best
# x nu --- ls -- where name == latest       # poxis
# x nu --- ls -- where name \== latest      # zsh

___x_cmd_nu___runcode(){
    local codestr=

    while [ $# -gt 0 ]; do
        case "$1" in
            --|---)         codestr="$codestr | " ;;
            *)              codestr="$codestr $1"
        esac
        shift
    done

    ___x_cmd_nu___binexec nu -c "$codestr"
}

