# shellcheck shell=dash
___x_cmd_hn_user(){
    local X_help_cmd="___x_cmd help -m hn user"; help:arg-null:parse
    ourl:arg:format:all
    local user="$1";    [ -n "$user" ] ||   N=user M="Please provide the user name" log:ret:64

    ___x_cmd_hn_user___"$format" "$@"
}

___x_cmd_hn_user___app(){
    local x_
    ___x_cmd fifo consumer_ ___x_cmd_hn_user___parse_json2app
    ___x_cmd_hn_user___json "$@" >"$x_"
}

___x_cmd_hn_user___csv(){
    local x_
    ___x_cmd fifo consumer_ ___x_cmd_hn_user___parse_json2csv
    ___x_cmd_hn_user___json "$@" >"$x_"
}

___x_cmd_hn_user___tsv(){
    local x_
    ___x_cmd fifo consumer_ ___x_cmd_hn_user___parse_json2tsv
    ___x_cmd_hn_user___json "$@" >"$x_"
}

___x_cmd_hn_user___json(){
    # while [ $# -gt 0 ]; do
    #     ___x_cmd_hn_user___raw "$1" | ___x_cmd_hn_user___parse_resp2json 1 || return $?
    #     shift
    # done
    ___x_cmd_hn_user___raw "$@" | ___x_cmd_hn_user___parse_resp2json "$#"
}

___x_cmd_hn_user___raw(){
    local IFS=","
    local userlist="{$*}"

    local temp; temp="$___X_CMD_ROOT_TMP/hn/user/$(___x_cmd rand uuid)..."
    ___x_cmd mkdirp "$temp"

    set -- "https://hacker-news.firebaseio.com/v0/user/$userlist.json?print=pretty" --fail-early --fail-with-body
    set -- "$@" -m 5 -L -sS -w "$___X_CMD_OURL_WRITEOUT" --output "${temp}#1"

    ___x_cmd curl "$@" 2>&1
}

# Section
___x_cmd_hn_user___parse_resp2json(){
    local n=$1
    ourl:writeout:init
    while ourl:writeout:read; do
        n="$((n-1))"
        case "$XOW_code" in
            20*)    ___x_cmd_cmds cat ;;
            *)      hn:error "[Http_code=$XOW_code] Get data failed"
                    ___x_cmd_ourl_json2errorlog ;  return "$XOW_exitcode"
        esac <"${XOW_fp}"
        ___x_cmd rmrf "$XOW_fp"
    done
    [ "${n}" -le 0 ]
}

___x_cmd_hn_user___parse_json2csv(){
    printf "%s\n" "id,created,karma,about,submitted"
    ___x_cmd ja jl2c .id .created .karma .about .submitted
}

___x_cmd_hn_user___parse_json2tsv(){
    ___x_cmd_hn_user___parse_json2csv | ___x_cmd csv 2tsv
}

___x_cmd_hn_user___parse_json2app(){
    ___x_cmd j2y
}
# EndSection
