//go:build go1.18
// +build go1.18

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

package fake

import (
	"context"
	"errors"
	"fmt"
	azfake "github.com/Azure/azure-sdk-for-go/sdk/azcore/fake"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/fake/server"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armtemplatespecs"
	"net/http"
	"net/url"
	"reflect"
	"regexp"
)

// TemplateSpecVersionsServer is a fake server for instances of the armtemplatespecs.TemplateSpecVersionsClient type.
type TemplateSpecVersionsServer struct {
	// CreateOrUpdate is the fake for method TemplateSpecVersionsClient.CreateOrUpdate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	CreateOrUpdate func(ctx context.Context, resourceGroupName string, templateSpecName string, templateSpecVersion string, templateSpecVersionModel armtemplatespecs.TemplateSpecVersion, options *armtemplatespecs.TemplateSpecVersionsClientCreateOrUpdateOptions) (resp azfake.Responder[armtemplatespecs.TemplateSpecVersionsClientCreateOrUpdateResponse], errResp azfake.ErrorResponder)

	// Delete is the fake for method TemplateSpecVersionsClient.Delete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusNoContent
	Delete func(ctx context.Context, resourceGroupName string, templateSpecName string, templateSpecVersion string, options *armtemplatespecs.TemplateSpecVersionsClientDeleteOptions) (resp azfake.Responder[armtemplatespecs.TemplateSpecVersionsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method TemplateSpecVersionsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, templateSpecName string, templateSpecVersion string, options *armtemplatespecs.TemplateSpecVersionsClientGetOptions) (resp azfake.Responder[armtemplatespecs.TemplateSpecVersionsClientGetResponse], errResp azfake.ErrorResponder)

	// GetBuiltIn is the fake for method TemplateSpecVersionsClient.GetBuiltIn
	// HTTP status codes to indicate success: http.StatusOK
	GetBuiltIn func(ctx context.Context, templateSpecName string, templateSpecVersion string, options *armtemplatespecs.TemplateSpecVersionsClientGetBuiltInOptions) (resp azfake.Responder[armtemplatespecs.TemplateSpecVersionsClientGetBuiltInResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method TemplateSpecVersionsClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceGroupName string, templateSpecName string, options *armtemplatespecs.TemplateSpecVersionsClientListOptions) (resp azfake.PagerResponder[armtemplatespecs.TemplateSpecVersionsClientListResponse])

	// NewListBuiltInsPager is the fake for method TemplateSpecVersionsClient.NewListBuiltInsPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListBuiltInsPager func(templateSpecName string, options *armtemplatespecs.TemplateSpecVersionsClientListBuiltInsOptions) (resp azfake.PagerResponder[armtemplatespecs.TemplateSpecVersionsClientListBuiltInsResponse])

	// Update is the fake for method TemplateSpecVersionsClient.Update
	// HTTP status codes to indicate success: http.StatusOK
	Update func(ctx context.Context, resourceGroupName string, templateSpecName string, templateSpecVersion string, options *armtemplatespecs.TemplateSpecVersionsClientUpdateOptions) (resp azfake.Responder[armtemplatespecs.TemplateSpecVersionsClientUpdateResponse], errResp azfake.ErrorResponder)
}

// NewTemplateSpecVersionsServerTransport creates a new instance of TemplateSpecVersionsServerTransport with the provided implementation.
// The returned TemplateSpecVersionsServerTransport instance is connected to an instance of armtemplatespecs.TemplateSpecVersionsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewTemplateSpecVersionsServerTransport(srv *TemplateSpecVersionsServer) *TemplateSpecVersionsServerTransport {
	return &TemplateSpecVersionsServerTransport{
		srv:                  srv,
		newListPager:         newTracker[azfake.PagerResponder[armtemplatespecs.TemplateSpecVersionsClientListResponse]](),
		newListBuiltInsPager: newTracker[azfake.PagerResponder[armtemplatespecs.TemplateSpecVersionsClientListBuiltInsResponse]](),
	}
}

// TemplateSpecVersionsServerTransport connects instances of armtemplatespecs.TemplateSpecVersionsClient to instances of TemplateSpecVersionsServer.
// Don't use this type directly, use NewTemplateSpecVersionsServerTransport instead.
type TemplateSpecVersionsServerTransport struct {
	srv                  *TemplateSpecVersionsServer
	newListPager         *tracker[azfake.PagerResponder[armtemplatespecs.TemplateSpecVersionsClientListResponse]]
	newListBuiltInsPager *tracker[azfake.PagerResponder[armtemplatespecs.TemplateSpecVersionsClientListBuiltInsResponse]]
}

// Do implements the policy.Transporter interface for TemplateSpecVersionsServerTransport.
func (t *TemplateSpecVersionsServerTransport) Do(req *http.Request) (*http.Response, error) {
	rawMethod := req.Context().Value(runtime.CtxAPINameKey{})
	method, ok := rawMethod.(string)
	if !ok {
		return nil, nonRetriableError{errors.New("unable to dispatch request, missing value for CtxAPINameKey")}
	}

	var resp *http.Response
	var err error

	switch method {
	case "TemplateSpecVersionsClient.CreateOrUpdate":
		resp, err = t.dispatchCreateOrUpdate(req)
	case "TemplateSpecVersionsClient.Delete":
		resp, err = t.dispatchDelete(req)
	case "TemplateSpecVersionsClient.Get":
		resp, err = t.dispatchGet(req)
	case "TemplateSpecVersionsClient.GetBuiltIn":
		resp, err = t.dispatchGetBuiltIn(req)
	case "TemplateSpecVersionsClient.NewListPager":
		resp, err = t.dispatchNewListPager(req)
	case "TemplateSpecVersionsClient.NewListBuiltInsPager":
		resp, err = t.dispatchNewListBuiltInsPager(req)
	case "TemplateSpecVersionsClient.Update":
		resp, err = t.dispatchUpdate(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchCreateOrUpdate(req *http.Request) (*http.Response, error) {
	if t.srv.CreateOrUpdate == nil {
		return nil, &nonRetriableError{errors.New("fake for method CreateOrUpdate not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Resources/templateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<templateSpecVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armtemplatespecs.TemplateSpecVersion](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
	if err != nil {
		return nil, err
	}
	templateSpecVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecVersion")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.CreateOrUpdate(req.Context(), resourceGroupNameParam, templateSpecNameParam, templateSpecVersionParam, body, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusCreated}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusCreated", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).TemplateSpecVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchDelete(req *http.Request) (*http.Response, error) {
	if t.srv.Delete == nil {
		return nil, &nonRetriableError{errors.New("fake for method Delete not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Resources/templateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<templateSpecVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
	if err != nil {
		return nil, err
	}
	templateSpecVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecVersion")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Delete(req.Context(), resourceGroupNameParam, templateSpecNameParam, templateSpecVersionParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK, http.StatusNoContent}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusNoContent", respContent.HTTPStatus)}
	}
	resp, err := server.NewResponse(respContent, req, nil)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if t.srv.Get == nil {
		return nil, &nonRetriableError{errors.New("fake for method Get not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Resources/templateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<templateSpecVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
	if err != nil {
		return nil, err
	}
	templateSpecVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecVersion")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.Get(req.Context(), resourceGroupNameParam, templateSpecNameParam, templateSpecVersionParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).TemplateSpecVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchGetBuiltIn(req *http.Request) (*http.Response, error) {
	if t.srv.GetBuiltIn == nil {
		return nil, &nonRetriableError{errors.New("fake for method GetBuiltIn not implemented")}
	}
	const regexStr = `/providers/Microsoft\.Resources/builtInTemplateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<templateSpecVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 2 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
	if err != nil {
		return nil, err
	}
	templateSpecVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecVersion")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := t.srv.GetBuiltIn(req.Context(), templateSpecNameParam, templateSpecVersionParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).TemplateSpecVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if t.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := t.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Resources/templateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 3 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
		if err != nil {
			return nil, err
		}
		resp := t.srv.NewListPager(resourceGroupNameParam, templateSpecNameParam, nil)
		newListPager = &resp
		t.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armtemplatespecs.TemplateSpecVersionsClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		t.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		t.newListPager.remove(req)
	}
	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchNewListBuiltInsPager(req *http.Request) (*http.Response, error) {
	if t.srv.NewListBuiltInsPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListBuiltInsPager not implemented")}
	}
	newListBuiltInsPager := t.newListBuiltInsPager.get(req)
	if newListBuiltInsPager == nil {
		const regexStr = `/providers/Microsoft\.Resources/builtInTemplateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 1 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
		if err != nil {
			return nil, err
		}
		resp := t.srv.NewListBuiltInsPager(templateSpecNameParam, nil)
		newListBuiltInsPager = &resp
		t.newListBuiltInsPager.add(req, newListBuiltInsPager)
		server.PagerResponderInjectNextLinks(newListBuiltInsPager, req, func(page *armtemplatespecs.TemplateSpecVersionsClientListBuiltInsResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListBuiltInsPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		t.newListBuiltInsPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListBuiltInsPager) {
		t.newListBuiltInsPager.remove(req)
	}
	return resp, nil
}

func (t *TemplateSpecVersionsServerTransport) dispatchUpdate(req *http.Request) (*http.Response, error) {
	if t.srv.Update == nil {
		return nil, &nonRetriableError{errors.New("fake for method Update not implemented")}
	}
	const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.Resources/templateSpecs/(?P<templateSpecName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/versions/(?P<templateSpecVersion>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 4 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	body, err := server.UnmarshalRequestAsJSON[armtemplatespecs.TemplateSpecVersionUpdateModel](req)
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	templateSpecNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecName")])
	if err != nil {
		return nil, err
	}
	templateSpecVersionParam, err := url.PathUnescape(matches[regex.SubexpIndex("templateSpecVersion")])
	if err != nil {
		return nil, err
	}
	var options *armtemplatespecs.TemplateSpecVersionsClientUpdateOptions
	if !reflect.ValueOf(body).IsZero() {
		options = &armtemplatespecs.TemplateSpecVersionsClientUpdateOptions{
			TemplateSpecVersionUpdateModel: &body,
		}
	}
	respr, errRespr := t.srv.Update(req.Context(), resourceGroupNameParam, templateSpecNameParam, templateSpecVersionParam, options)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !contains([]int{http.StatusOK}, respContent.HTTPStatus) {
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", respContent.HTTPStatus)}
	}
	resp, err := server.MarshalResponseAsJSON(respContent, server.GetResponse(respr).TemplateSpecVersion, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}
