# shellcheck    shell=dash disable=SC2154,SC2034

# TODO: add support to run x-cmd
# x bwh sh 'eval "$(curl https://get.x-cmd.com)"'

___x_cmd_bwh_sh(){
    [ $# -gt 0 ]   ||   set -- -h

    local op="$1";  shift
    case "$op" in
        --cd)       ___x_cmd_bwh_sh___cd            "$@" ;;
        --cmd)      ___x_cmd_bwh_sh___cmd           "$@" ;;
        --script)   ___x_cmd_bwh_sh___script        "$@" ;;

        # --exec)     ___x_cmd_bwh_sh___exec          "$@" ;;

        -h|--help)  ___x_cmd_bwh_help sh                 ;;
        *)          ___x_cmd_bwh_sh___cmd "$op"     "$@" ;;
    esac
}

# Section: cd
___x_cmd_bwh_sh___cd(){
    local X_help_cmd='___x_cmd help -m bwh sh cd';   help:arg:parse
    [ $# -eq 2 ] || N=bwh M="Require 2 argument as <currentDir> and <newDir>" log:ret:64
    local error=; local message=
    local data; data="$( ___x_cmd_bwh_sh___cd___raw "$1" "$2")"
    ___x_cmd_pipevar data     ___x_cmd jo env . .error .message

    bwh:arg:judge:display:content:bat
}

___x_cmd_bwh_sh___cd___raw(){
    ___x_cmd_bwh_api basicShell/cd "currentDir=$1" "newDir=$2"
}
# EndSection

# Section: assign
___x_cmd_bwh_sh___script(){
    local X_help_cmd='___x_cmd help -m bwh sh script';   help:arg:parse
    [ $# -eq 1 ] || N=bwh M="Require 1 argument as <script>" log:ret:64
    local error=; local message=
    local data; data="$( ___x_cmd_bwh_sh___script___raw "$1" )"
    ___x_cmd_pipevar data     ___x_cmd jo env . .error .message

    bwh:arg:judge:display:content:bat
}

___x_cmd_bwh_sh___script___raw(){
    ___x_cmd_bwh_api shellScript/exec "script=$1"
}
# EndSection

# Section: assign
___x_cmd_bwh_sh___cmd(){
    local X_help_cmd='___x_cmd help -m bwh sh cmd';   help:arg:parse
    [ $# -ge 1 ] || N=bwh M="Require at least 1 argument as <command>" log:ret:64
    local error=; local message=
    local data; data="$( ___x_cmd_bwh_sh___cmd___raw "$*" )"
    ___x_cmd_pipevar data     ___x_cmd jo env . .error .message

    # bwh:arg:judge:display:content:bat
    printf "%s\n" "$message"
    return "$error"
}

___x_cmd_bwh_sh___cmd___raw(){
    local IFS=" "
    ___x_cmd_bwh_api basicShell/exec "command=$*"
}
# EndSection


# # shellcheck disable=SC2120
# ___x_cmd_bwh_sh___exec(){
#     if [ "$1" = --script ]; then
#         shift;  ___x_cmd_bwh_sh___script    "$@"
#     else
#                 ___x_cmd_bwh_sh___cmd       "$@"
#     fi
# }

# ___x_cmd_bwh_sh___main(){
#     local data; data=$(___x_cmd_bwh_sh___exec "$@") || {
#         bwh:error "Fail to execute command and receive no data. cmd ==> $*"
#         return 1
#     }

#     local error; local message
#     printf "%s\n" "$data" | (
#         x jo env . .error .message
#         printf "%s" "$message"
#         return "$error"
#     )
# }
