/*
Configuration API

API version: 2
*/

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package balanceplatform

import (
	"context"
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/adyen/adyen-go-api-library/v21/src/common"
)

// TransferRoutesApi service
type TransferRoutesApi common.Service

// All parameters accepted by TransferRoutesApi.CalculateTransferRoutes
type TransferRoutesApiCalculateTransferRoutesInput struct {
	transferRouteRequest *TransferRouteRequest
}

func (r TransferRoutesApiCalculateTransferRoutesInput) TransferRouteRequest(transferRouteRequest TransferRouteRequest) TransferRoutesApiCalculateTransferRoutesInput {
	r.transferRouteRequest = &transferRouteRequest
	return r
}

/*
Prepare a request for CalculateTransferRoutes

@return TransferRoutesApiCalculateTransferRoutesInput
*/
func (a *TransferRoutesApi) CalculateTransferRoutesInput() TransferRoutesApiCalculateTransferRoutesInput {
	return TransferRoutesApiCalculateTransferRoutesInput{}
}

/*
CalculateTransferRoutes Calculate transfer routes

Returns available transfer routes based on a combination of transfer `country`, `currency`, `counterparty`, and `priorities`. Use this endpoint to find optimal transfer priorities and associated requirements before you [make a transfer](https://docs.adyen.com/api-explorer/transfers/latest/post/transfers).

@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
@param r TransferRoutesApiCalculateTransferRoutesInput - Request parameters, see CalculateTransferRoutesInput
@return TransferRouteResponse, *http.Response, error
*/
func (a *TransferRoutesApi) CalculateTransferRoutes(ctx context.Context, r TransferRoutesApiCalculateTransferRoutesInput) (TransferRouteResponse, *http.Response, error) {
	res := &TransferRouteResponse{}
	path := "/transferRoutes/calculate"
	queryParams := url.Values{}
	headerParams := make(map[string]string)
	httpRes, err := common.SendAPIRequest(
		ctx,
		a.Client,
		r.transferRouteRequest,
		res,
		http.MethodPost,
		a.BasePath()+path,
		queryParams,
		headerParams,
	)

	if httpRes == nil {
		return *res, httpRes, err
	}

	var serviceError common.RestServiceError
	if httpRes.StatusCode == 401 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 403 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 422 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}
	if httpRes.StatusCode == 500 {
		body, _ := ioutil.ReadAll(httpRes.Body)
		decodeError := json.Unmarshal([]byte(body), &serviceError)
		if decodeError != nil {
			return *res, httpRes, decodeError
		}
		return *res, httpRes, serviceError
	}

	return *res, httpRes, err
}
