// 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) Go Code Generator. DO NOT EDIT.

package fake

import (
	"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/hardwaresecuritymodules/armhardwaresecuritymodules/v2"
	"net/http"
	"net/url"
	"regexp"
)

// CloudHsmClusterPrivateLinkResourcesServer is a fake server for instances of the armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClient type.
type CloudHsmClusterPrivateLinkResourcesServer struct {
	// NewListByCloudHsmClusterPager is the fake for method CloudHsmClusterPrivateLinkResourcesClient.NewListByCloudHsmClusterPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListByCloudHsmClusterPager func(resourceGroupName string, cloudHsmClusterName string, options *armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClientListByCloudHsmClusterOptions) (resp azfake.PagerResponder[armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClientListByCloudHsmClusterResponse])
}

// NewCloudHsmClusterPrivateLinkResourcesServerTransport creates a new instance of CloudHsmClusterPrivateLinkResourcesServerTransport with the provided implementation.
// The returned CloudHsmClusterPrivateLinkResourcesServerTransport instance is connected to an instance of armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewCloudHsmClusterPrivateLinkResourcesServerTransport(srv *CloudHsmClusterPrivateLinkResourcesServer) *CloudHsmClusterPrivateLinkResourcesServerTransport {
	return &CloudHsmClusterPrivateLinkResourcesServerTransport{
		srv:                           srv,
		newListByCloudHsmClusterPager: newTracker[azfake.PagerResponder[armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClientListByCloudHsmClusterResponse]](),
	}
}

// CloudHsmClusterPrivateLinkResourcesServerTransport connects instances of armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClient to instances of CloudHsmClusterPrivateLinkResourcesServer.
// Don't use this type directly, use NewCloudHsmClusterPrivateLinkResourcesServerTransport instead.
type CloudHsmClusterPrivateLinkResourcesServerTransport struct {
	srv                           *CloudHsmClusterPrivateLinkResourcesServer
	newListByCloudHsmClusterPager *tracker[azfake.PagerResponder[armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClientListByCloudHsmClusterResponse]]
}

// Do implements the policy.Transporter interface for CloudHsmClusterPrivateLinkResourcesServerTransport.
func (c *CloudHsmClusterPrivateLinkResourcesServerTransport) 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")}
	}

	return c.dispatchToMethodFake(req, method)
}

func (c *CloudHsmClusterPrivateLinkResourcesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result)
	defer close(resultChan)

	go func() {
		var intercepted bool
		var res result
		if cloudHsmClusterPrivateLinkResourcesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = cloudHsmClusterPrivateLinkResourcesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "CloudHsmClusterPrivateLinkResourcesClient.NewListByCloudHsmClusterPager":
				res.resp, res.err = c.dispatchNewListByCloudHsmClusterPager(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		select {
		case resultChan <- res:
		case <-req.Context().Done():
		}
	}()

	select {
	case <-req.Context().Done():
		return nil, req.Context().Err()
	case res := <-resultChan:
		return res.resp, res.err
	}
}

func (c *CloudHsmClusterPrivateLinkResourcesServerTransport) dispatchNewListByCloudHsmClusterPager(req *http.Request) (*http.Response, error) {
	if c.srv.NewListByCloudHsmClusterPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListByCloudHsmClusterPager not implemented")}
	}
	newListByCloudHsmClusterPager := c.newListByCloudHsmClusterPager.get(req)
	if newListByCloudHsmClusterPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.HardwareSecurityModules/cloudHsmClusters/(?P<cloudHsmClusterName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/privateLinkResources`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if 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
		}
		cloudHsmClusterNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("cloudHsmClusterName")])
		if err != nil {
			return nil, err
		}
		resp := c.srv.NewListByCloudHsmClusterPager(resourceGroupNameParam, cloudHsmClusterNameParam, nil)
		newListByCloudHsmClusterPager = &resp
		c.newListByCloudHsmClusterPager.add(req, newListByCloudHsmClusterPager)
		server.PagerResponderInjectNextLinks(newListByCloudHsmClusterPager, req, func(page *armhardwaresecuritymodules.CloudHsmClusterPrivateLinkResourcesClientListByCloudHsmClusterResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListByCloudHsmClusterPager, req)
	if err != nil {
		return nil, err
	}
	if !contains([]int{http.StatusOK}, resp.StatusCode) {
		c.newListByCloudHsmClusterPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListByCloudHsmClusterPager) {
		c.newListByCloudHsmClusterPager.remove(req)
	}
	return resp, nil
}

// set this to conditionally intercept incoming requests to CloudHsmClusterPrivateLinkResourcesServerTransport
var cloudHsmClusterPrivateLinkResourcesServerTransportInterceptor interface {
	// Do returns true if the server transport should use the returned response/error
	Do(*http.Request) (*http.Response, error, bool)
}
