# shellcheck shell=dash
xrc ourl

___x_cmd_siliconflow_user(){
    local format=yml
    local op="$1";
    case "$op" in
        -j|--json)  shift; format=json      ;;
        -y|--yml)   shift; format=yml       ;;
        -h|--help)  shift; ___x_cmd help -m siliconflow user "$@"; return ;;
    esac

    ___x_cmd_siliconflow_user_"${format}" "$@"
}

___x_cmd_siliconflow_user_yml(){
    local data; data="$( ___x_cmd_siliconflow_user_raw )" || ourl:data:ret
    ___x_cmd_pipevar data ___x_cmd j2y
}

___x_cmd_siliconflow_user_json(){
    local data; data="$( ___x_cmd_siliconflow_user_raw )" || ourl:data:ret
    ___x_cmd_pipevar data ___x_cmd jo fmt
}

___x_cmd_siliconflow_user_raw(){
    ___x_cmd_siliconflow_has_apikey || return $?

    local apikey=
    ___x_cmd_siliconflow_cur apikey:=  2>/dev/null
    local data=; data="$(
        ___x_cmd curl \
            -sS  "$___X_CMD_SILICONFLOW_DEFAULT_ENDPOINT/v1/user/info"          \
            --header 'Content-Type: application/json'        \
            --header "Authorization: Bearer $apikey"
    )"

    [ "$(printf "%s\n" "$data" | ___x_cmd jo .code)" = "20000" ] || {
        siliconflow:error -m "$data"
        return 1
    }

    printf "%s\n" "$data"
}
