# shellcheck shell=sh
# shellcheck disable=SC3043,SC2294
___x_cmd_api_tea(){
    local op="$1"
    local _curl_url=""
    local _auth_token="${___X_CMD_API_TEA_TOKEN:-$(___x_cmd tea --cfg get token 2>/dev/null)}"
    local _auth_header="${_auth_token:+"-H \"Authorization:token ${_auth_token}\""}"
    local _curl_header="${___X_CMD_API_TEA_HEADER:-"-s -H \"accept: application/json\" -H \"Content-Type: application/json\""}"
    case "${op}" in
        "")                             api:error -h 'x api tea --help' -c 'x api tea <get|post|put|patch|del> <API shortcut endpoint> [extends "curl" options]' "tea - accepts 1 arg(s), received 0"; return 1 ;;
        -h|--help)                      _____x_cmd_api_help tea; return    ;;

        get|post|put|patch|del)
            _curl_url="${___X_CMD_API_TEA_ENDPOINT:-"https://gitea.com/api/v1"}$2" ; shift 2
            ___x_cmd_api_tea_"$op" "$@"
            ;;
        *)
            _curl_url="${___X_CMD_API_TEA_ENDPOINT:-"https://gitea.com/api/v1"}$op"; shift 1
            ___x_cmd_api_tea_get "$@"
            ;;
    esac
}

___x_cmd_api_tea_get(){
    api:debug \'___x_cmd curl -X GET "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""\'
    eval        ___x_cmd curl -X GET "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""
}

___x_cmd_api_tea_post(){
    api:debug \'___x_cmd curl -X POST "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""\'
    eval        ___x_cmd curl -X POST "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""
}

___x_cmd_api_tea_put(){
    api:debug \'___x_cmd curl -X PUT "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""\'
    eval        ___x_cmd curl -X PUT "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""
}

___x_cmd_api_tea_patch(){
    api:debug \'___x_cmd curl -X PATCH "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""\'
    eval        ___x_cmd curl -X PATCH "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""
}

___x_cmd_api_tea_del(){
    api:debug \'___x_cmd curl -X DELETE "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""\'
    eval        ___x_cmd curl -X DELETE "$_curl_header" "$_auth_header" "$@" "\"$_curl_url\""
}
