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

        get|post|put|patch|del)
            _curl_url="${___X_CMD_API_GL_ENDPOINT:-"https://gitlab.com/api/v4"}$2" ; shift 2
            ___x_cmd_api_gl_"$op" "$@"
            ;;
        *)
            _curl_url="${___X_CMD_API_GL_ENDPOINT:-"https://gitlab.com/api/v4"}$op"; shift 1
            ___x_cmd_api_gl_get "$@"
            ;;
    esac
}

___x_cmd_api_gl_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_gl_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_gl_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_gl_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_gl_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\""
}
