//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/recoveryservices/armrecoveryservicessiterecovery/v2"
	"net/http"
	"net/url"
	"regexp"
)

// ReplicationStorageClassificationMappingsServer is a fake server for instances of the armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClient type.
type ReplicationStorageClassificationMappingsServer struct {
	// BeginCreate is the fake for method ReplicationStorageClassificationMappingsClient.BeginCreate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted
	BeginCreate func(ctx context.Context, resourceName string, resourceGroupName string, fabricName string, storageClassificationName string, storageClassificationMappingName string, pairingInput armrecoveryservicessiterecovery.StorageClassificationMappingInput, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientBeginCreateOptions) (resp azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientCreateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method ReplicationStorageClassificationMappingsClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceName string, resourceGroupName string, fabricName string, storageClassificationName string, storageClassificationMappingName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientBeginDeleteOptions) (resp azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method ReplicationStorageClassificationMappingsClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceName string, resourceGroupName string, fabricName string, storageClassificationName string, storageClassificationMappingName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientGetOptions) (resp azfake.Responder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method ReplicationStorageClassificationMappingsClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceName string, resourceGroupName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListOptions) (resp azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse])

	// NewListByReplicationStorageClassificationsPager is the fake for method ReplicationStorageClassificationMappingsClient.NewListByReplicationStorageClassificationsPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByReplicationStorageClassificationsPager func(resourceName string, resourceGroupName string, fabricName string, storageClassificationName string, options *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsOptions) (resp azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse])
}

// NewReplicationStorageClassificationMappingsServerTransport creates a new instance of ReplicationStorageClassificationMappingsServerTransport with the provided implementation.
// The returned ReplicationStorageClassificationMappingsServerTransport instance is connected to an instance of armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewReplicationStorageClassificationMappingsServerTransport(srv *ReplicationStorageClassificationMappingsServer) *ReplicationStorageClassificationMappingsServerTransport {
	return &ReplicationStorageClassificationMappingsServerTransport{
		srv:          srv,
		beginCreate:  newTracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientCreateResponse]](),
		beginDelete:  newTracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientDeleteResponse]](),
		newListPager: newTracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse]](),
		newListByReplicationStorageClassificationsPager: newTracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse]](),
	}
}

// ReplicationStorageClassificationMappingsServerTransport connects instances of armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClient to instances of ReplicationStorageClassificationMappingsServer.
// Don't use this type directly, use NewReplicationStorageClassificationMappingsServerTransport instead.
type ReplicationStorageClassificationMappingsServerTransport struct {
	srv                                             *ReplicationStorageClassificationMappingsServer
	beginCreate                                     *tracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientCreateResponse]]
	beginDelete                                     *tracker[azfake.PollerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientDeleteResponse]]
	newListPager                                    *tracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse]]
	newListByReplicationStorageClassificationsPager *tracker[azfake.PagerResponder[armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse]]
}

// Do implements the policy.Transporter interface for ReplicationStorageClassificationMappingsServerTransport.
func (r *ReplicationStorageClassificationMappingsServerTransport) 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 "ReplicationStorageClassificationMappingsClient.BeginCreate":
		resp, err = r.dispatchBeginCreate(req)
	case "ReplicationStorageClassificationMappingsClient.BeginDelete":
		resp, err = r.dispatchBeginDelete(req)
	case "ReplicationStorageClassificationMappingsClient.Get":
		resp, err = r.dispatchGet(req)
	case "ReplicationStorageClassificationMappingsClient.NewListPager":
		resp, err = r.dispatchNewListPager(req)
	case "ReplicationStorageClassificationMappingsClient.NewListByReplicationStorageClassificationsPager":
		resp, err = r.dispatchNewListByReplicationStorageClassificationsPager(req)
	default:
		err = fmt.Errorf("unhandled API %s", method)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
	if r.srv.BeginCreate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
	}
	beginCreate := r.beginCreate.get(req)
	if beginCreate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings/(?P<storageClassificationMappingName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armrecoveryservicessiterecovery.StorageClassificationMappingInput](req)
		if err != nil {
			return nil, err
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
		if err != nil {
			return nil, err
		}
		storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
		if err != nil {
			return nil, err
		}
		storageClassificationMappingNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationMappingName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := r.srv.BeginCreate(req.Context(), resourceNameParam, resourceGroupNameParam, fabricNameParam, storageClassificationNameParam, storageClassificationMappingNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreate = &respr
		r.beginCreate.add(req, beginCreate)
	}

	resp, err := server.PollerResponderNext(beginCreate, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusOK, http.StatusAccepted}, resp.StatusCode) {
		r.beginCreate.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK, http.StatusAccepted", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginCreate) {
		r.beginCreate.remove(req)
	}

	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if r.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := r.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings/(?P<storageClassificationMappingName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
		if err != nil {
			return nil, err
		}
		storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
		if err != nil {
			return nil, err
		}
		storageClassificationMappingNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationMappingName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := r.srv.BeginDelete(req.Context(), resourceNameParam, resourceGroupNameParam, fabricNameParam, storageClassificationNameParam, storageClassificationMappingNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		r.beginDelete.add(req, beginDelete)
	}

	resp, err := server.PollerResponderNext(beginDelete, req)
	if err != nil {
		return nil, err
	}

	if !contains([]int{http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		r.beginDelete.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusAccepted, http.StatusNoContent", resp.StatusCode)}
	}
	if !server.PollerResponderMore(beginDelete) {
		r.beginDelete.remove(req)
	}

	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if r.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\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings/(?P<storageClassificationMappingName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if matches == nil || len(matches) < 6 {
		return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
	}
	resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
	if err != nil {
		return nil, err
	}
	resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
	if err != nil {
		return nil, err
	}
	fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
	if err != nil {
		return nil, err
	}
	storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
	if err != nil {
		return nil, err
	}
	storageClassificationMappingNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationMappingName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := r.srv.Get(req.Context(), resourceNameParam, resourceGroupNameParam, fabricNameParam, storageClassificationNameParam, storageClassificationMappingNameParam, 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).StorageClassificationMapping, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if r.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := r.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings`
		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)
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		resp := r.srv.NewListPager(resourceNameParam, resourceGroupNameParam, nil)
		newListPager = &resp
		r.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListResponse, 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) {
		r.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		r.newListPager.remove(req)
	}
	return resp, nil
}

func (r *ReplicationStorageClassificationMappingsServerTransport) dispatchNewListByReplicationStorageClassificationsPager(req *http.Request) (*http.Response, error) {
	if r.srv.NewListByReplicationStorageClassificationsPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByReplicationStorageClassificationsPager not implemented")}
	}
	newListByReplicationStorageClassificationsPager := r.newListByReplicationStorageClassificationsPager.get(req)
	if newListByReplicationStorageClassificationsPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.RecoveryServices/vaults/(?P<resourceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationFabrics/(?P<fabricName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassifications/(?P<storageClassificationName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/replicationStorageClassificationMappings`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if matches == nil || len(matches) < 5 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		resourceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceName")])
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		fabricNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fabricName")])
		if err != nil {
			return nil, err
		}
		storageClassificationNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("storageClassificationName")])
		if err != nil {
			return nil, err
		}
		resp := r.srv.NewListByReplicationStorageClassificationsPager(resourceNameParam, resourceGroupNameParam, fabricNameParam, storageClassificationNameParam, nil)
		newListByReplicationStorageClassificationsPager = &resp
		r.newListByReplicationStorageClassificationsPager.add(req, newListByReplicationStorageClassificationsPager)
		server.PagerResponderInjectNextLinks(newListByReplicationStorageClassificationsPager, req, func(page *armrecoveryservicessiterecovery.ReplicationStorageClassificationMappingsClientListByReplicationStorageClassificationsResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByReplicationStorageClassificationsPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		r.newListByReplicationStorageClassificationsPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByReplicationStorageClassificationsPager) {
		r.newListByReplicationStorageClassificationsPager.remove(req)
	}
	return resp, nil
}
