// 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 (
	"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/resourcemanager/compute/armcompute/v8"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// TenantLevelSharedGalleryInvitesServer is a fake server for instances of the armcompute.TenantLevelSharedGalleryInvitesClient type.
type TenantLevelSharedGalleryInvitesServer struct {
	// BeginTenantLevelGallerySharingAccept is the fake for method TenantLevelSharedGalleryInvitesClient.BeginTenantLevelGallerySharingAccept
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginTenantLevelGallerySharingAccept func(ctx context.Context, location string, sharedGallerySubscriptionID string, sharedGalleryName string, options *armcompute.TenantLevelSharedGalleryInvitesClientBeginTenantLevelGallerySharingAcceptOptions) (resp azfake.PollerResponder[armcompute.TenantLevelSharedGalleryInvitesClientTenantLevelGallerySharingAcceptResponse], errResp azfake.ErrorResponder)

	// BeginTenantLevelGallerySharingReject is the fake for method TenantLevelSharedGalleryInvitesClient.BeginTenantLevelGallerySharingReject
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginTenantLevelGallerySharingReject func(ctx context.Context, location string, sharedGallerySubscriptionID string, sharedGalleryName string, options *armcompute.TenantLevelSharedGalleryInvitesClientBeginTenantLevelGallerySharingRejectOptions) (resp azfake.PollerResponder[armcompute.TenantLevelSharedGalleryInvitesClientTenantLevelGallerySharingRejectResponse], errResp azfake.ErrorResponder)
}

// NewTenantLevelSharedGalleryInvitesServerTransport creates a new instance of TenantLevelSharedGalleryInvitesServerTransport with the provided implementation.
// The returned TenantLevelSharedGalleryInvitesServerTransport instance is connected to an instance of armcompute.TenantLevelSharedGalleryInvitesClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewTenantLevelSharedGalleryInvitesServerTransport(srv *TenantLevelSharedGalleryInvitesServer) *TenantLevelSharedGalleryInvitesServerTransport {
	return &TenantLevelSharedGalleryInvitesServerTransport{
		srv:                                  srv,
		beginTenantLevelGallerySharingAccept: newTracker[azfake.PollerResponder[armcompute.TenantLevelSharedGalleryInvitesClientTenantLevelGallerySharingAcceptResponse]](),
		beginTenantLevelGallerySharingReject: newTracker[azfake.PollerResponder[armcompute.TenantLevelSharedGalleryInvitesClientTenantLevelGallerySharingRejectResponse]](),
	}
}

// TenantLevelSharedGalleryInvitesServerTransport connects instances of armcompute.TenantLevelSharedGalleryInvitesClient to instances of TenantLevelSharedGalleryInvitesServer.
// Don't use this type directly, use NewTenantLevelSharedGalleryInvitesServerTransport instead.
type TenantLevelSharedGalleryInvitesServerTransport struct {
	srv                                  *TenantLevelSharedGalleryInvitesServer
	beginTenantLevelGallerySharingAccept *tracker[azfake.PollerResponder[armcompute.TenantLevelSharedGalleryInvitesClientTenantLevelGallerySharingAcceptResponse]]
	beginTenantLevelGallerySharingReject *tracker[azfake.PollerResponder[armcompute.TenantLevelSharedGalleryInvitesClientTenantLevelGallerySharingRejectResponse]]
}

// Do implements the policy.Transporter interface for TenantLevelSharedGalleryInvitesServerTransport.
func (t *TenantLevelSharedGalleryInvitesServerTransport) 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 t.dispatchToMethodFake(req, method)
}

func (t *TenantLevelSharedGalleryInvitesServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if tenantLevelSharedGalleryInvitesServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = tenantLevelSharedGalleryInvitesServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "TenantLevelSharedGalleryInvitesClient.BeginTenantLevelGallerySharingAccept":
				res.resp, res.err = t.dispatchBeginTenantLevelGallerySharingAccept(req)
			case "TenantLevelSharedGalleryInvitesClient.BeginTenantLevelGallerySharingReject":
				res.resp, res.err = t.dispatchBeginTenantLevelGallerySharingReject(req)
			default:
				res.err = fmt.Errorf("unhandled API %s", method)
			}

		}
		resultChan <- res
	}()

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

func (t *TenantLevelSharedGalleryInvitesServerTransport) dispatchBeginTenantLevelGallerySharingAccept(req *http.Request) (*http.Response, error) {
	if t.srv.BeginTenantLevelGallerySharingAccept == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginTenantLevelGallerySharingAccept not implemented")}
	}
	beginTenantLevelGallerySharingAccept := t.beginTenantLevelGallerySharingAccept.get(req)
	if beginTenantLevelGallerySharingAccept == nil {
		const regexStr = `/providers/Microsoft\.Compute/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/tenantLevelSharedGallerySubscriptions/(?P<sharedGallerySubscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/sharedGalleries/(?P<sharedGalleryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/accept`
		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)
		}
		locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
		if err != nil {
			return nil, err
		}
		sharedGallerySubscriptionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("sharedGallerySubscriptionId")])
		if err != nil {
			return nil, err
		}
		sharedGalleryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("sharedGalleryName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := t.srv.BeginTenantLevelGallerySharingAccept(req.Context(), locationParam, sharedGallerySubscriptionIDParam, sharedGalleryNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginTenantLevelGallerySharingAccept = &respr
		t.beginTenantLevelGallerySharingAccept.add(req, beginTenantLevelGallerySharingAccept)
	}

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

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

	return resp, nil
}

func (t *TenantLevelSharedGalleryInvitesServerTransport) dispatchBeginTenantLevelGallerySharingReject(req *http.Request) (*http.Response, error) {
	if t.srv.BeginTenantLevelGallerySharingReject == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginTenantLevelGallerySharingReject not implemented")}
	}
	beginTenantLevelGallerySharingReject := t.beginTenantLevelGallerySharingReject.get(req)
	if beginTenantLevelGallerySharingReject == nil {
		const regexStr = `/providers/Microsoft\.Compute/locations/(?P<location>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/tenantLevelSharedGallerySubscriptions/(?P<sharedGallerySubscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/sharedGalleries/(?P<sharedGalleryName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/reject`
		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)
		}
		locationParam, err := url.PathUnescape(matches[regex.SubexpIndex("location")])
		if err != nil {
			return nil, err
		}
		sharedGallerySubscriptionIDParam, err := url.PathUnescape(matches[regex.SubexpIndex("sharedGallerySubscriptionId")])
		if err != nil {
			return nil, err
		}
		sharedGalleryNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("sharedGalleryName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := t.srv.BeginTenantLevelGallerySharingReject(req.Context(), locationParam, sharedGallerySubscriptionIDParam, sharedGalleryNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginTenantLevelGallerySharingReject = &respr
		t.beginTenantLevelGallerySharingReject.add(req, beginTenantLevelGallerySharingReject)
	}

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

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

	return resp, nil
}

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