// 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/azcore/to"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/cosmos/armcosmos/v4"
	"net/http"
	"net/url"
	"regexp"
	"slices"
)

// FleetspaceAccountServer is a fake server for instances of the armcosmos.FleetspaceAccountClient type.
type FleetspaceAccountServer struct {
	// BeginCreate is the fake for method FleetspaceAccountClient.BeginCreate
	// HTTP status codes to indicate success: http.StatusOK, http.StatusCreated
	BeginCreate func(ctx context.Context, resourceGroupName string, fleetName string, fleetspaceName string, fleetspaceAccountName string, body armcosmos.FleetspaceAccountResource, options *armcosmos.FleetspaceAccountClientBeginCreateOptions) (resp azfake.PollerResponder[armcosmos.FleetspaceAccountClientCreateResponse], errResp azfake.ErrorResponder)

	// BeginDelete is the fake for method FleetspaceAccountClient.BeginDelete
	// HTTP status codes to indicate success: http.StatusOK, http.StatusAccepted, http.StatusNoContent
	BeginDelete func(ctx context.Context, resourceGroupName string, fleetName string, fleetspaceName string, fleetspaceAccountName string, options *armcosmos.FleetspaceAccountClientBeginDeleteOptions) (resp azfake.PollerResponder[armcosmos.FleetspaceAccountClientDeleteResponse], errResp azfake.ErrorResponder)

	// Get is the fake for method FleetspaceAccountClient.Get
	// HTTP status codes to indicate success: http.StatusOK
	Get func(ctx context.Context, resourceGroupName string, fleetName string, fleetspaceName string, fleetspaceAccountName string, options *armcosmos.FleetspaceAccountClientGetOptions) (resp azfake.Responder[armcosmos.FleetspaceAccountClientGetResponse], errResp azfake.ErrorResponder)

	// NewListPager is the fake for method FleetspaceAccountClient.NewListPager
	// HTTP status codes to indicate success: http.StatusOK
	NewListPager func(resourceGroupName string, fleetName string, fleetspaceName string, options *armcosmos.FleetspaceAccountClientListOptions) (resp azfake.PagerResponder[armcosmos.FleetspaceAccountClientListResponse])
}

// NewFleetspaceAccountServerTransport creates a new instance of FleetspaceAccountServerTransport with the provided implementation.
// The returned FleetspaceAccountServerTransport instance is connected to an instance of armcosmos.FleetspaceAccountClient via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewFleetspaceAccountServerTransport(srv *FleetspaceAccountServer) *FleetspaceAccountServerTransport {
	return &FleetspaceAccountServerTransport{
		srv:          srv,
		beginCreate:  newTracker[azfake.PollerResponder[armcosmos.FleetspaceAccountClientCreateResponse]](),
		beginDelete:  newTracker[azfake.PollerResponder[armcosmos.FleetspaceAccountClientDeleteResponse]](),
		newListPager: newTracker[azfake.PagerResponder[armcosmos.FleetspaceAccountClientListResponse]](),
	}
}

// FleetspaceAccountServerTransport connects instances of armcosmos.FleetspaceAccountClient to instances of FleetspaceAccountServer.
// Don't use this type directly, use NewFleetspaceAccountServerTransport instead.
type FleetspaceAccountServerTransport struct {
	srv          *FleetspaceAccountServer
	beginCreate  *tracker[azfake.PollerResponder[armcosmos.FleetspaceAccountClientCreateResponse]]
	beginDelete  *tracker[azfake.PollerResponder[armcosmos.FleetspaceAccountClientDeleteResponse]]
	newListPager *tracker[azfake.PagerResponder[armcosmos.FleetspaceAccountClientListResponse]]
}

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

func (f *FleetspaceAccountServerTransport) dispatchToMethodFake(req *http.Request, method string) (*http.Response, error) {
	resultChan := make(chan result, 1)
	go func() {
		var intercepted bool
		var res result
		if fleetspaceAccountServerTransportInterceptor != nil {
			res.resp, res.err, intercepted = fleetspaceAccountServerTransportInterceptor.Do(req)
		}
		if !intercepted {
			switch method {
			case "FleetspaceAccountClient.BeginCreate":
				res.resp, res.err = f.dispatchBeginCreate(req)
			case "FleetspaceAccountClient.BeginDelete":
				res.resp, res.err = f.dispatchBeginDelete(req)
			case "FleetspaceAccountClient.Get":
				res.resp, res.err = f.dispatchGet(req)
			case "FleetspaceAccountClient.NewListPager":
				res.resp, res.err = f.dispatchNewListPager(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 (f *FleetspaceAccountServerTransport) dispatchBeginCreate(req *http.Request) (*http.Response, error) {
	if f.srv.BeginCreate == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginCreate not implemented")}
	}
	beginCreate := f.beginCreate.get(req)
	if beginCreate == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DocumentDB/fleets/(?P<fleetName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaces/(?P<fleetspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaceAccounts/(?P<fleetspaceAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			return nil, fmt.Errorf("failed to parse path %s", req.URL.Path)
		}
		body, err := server.UnmarshalRequestAsJSON[armcosmos.FleetspaceAccountResource](req)
		if err != nil {
			return nil, err
		}
		resourceGroupNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("resourceGroupName")])
		if err != nil {
			return nil, err
		}
		fleetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetName")])
		if err != nil {
			return nil, err
		}
		fleetspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceName")])
		if err != nil {
			return nil, err
		}
		fleetspaceAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceAccountName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := f.srv.BeginCreate(req.Context(), resourceGroupNameParam, fleetNameParam, fleetspaceNameParam, fleetspaceAccountNameParam, body, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginCreate = &respr
		f.beginCreate.add(req, beginCreate)
	}

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

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

	return resp, nil
}

func (f *FleetspaceAccountServerTransport) dispatchBeginDelete(req *http.Request) (*http.Response, error) {
	if f.srv.BeginDelete == nil {
		return nil, &nonRetriableError{errors.New("fake for method BeginDelete not implemented")}
	}
	beginDelete := f.beginDelete.get(req)
	if beginDelete == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DocumentDB/fleets/(?P<fleetName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaces/(?P<fleetspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaceAccounts/(?P<fleetspaceAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 6 {
			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
		}
		fleetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetName")])
		if err != nil {
			return nil, err
		}
		fleetspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceName")])
		if err != nil {
			return nil, err
		}
		fleetspaceAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceAccountName")])
		if err != nil {
			return nil, err
		}
		respr, errRespr := f.srv.BeginDelete(req.Context(), resourceGroupNameParam, fleetNameParam, fleetspaceNameParam, fleetspaceAccountNameParam, nil)
		if respErr := server.GetError(errRespr, req); respErr != nil {
			return nil, respErr
		}
		beginDelete = &respr
		f.beginDelete.add(req, beginDelete)
	}

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

	if !slices.Contains([]int{http.StatusOK, http.StatusAccepted, http.StatusNoContent}, resp.StatusCode) {
		f.beginDelete.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(beginDelete) {
		f.beginDelete.remove(req)
	}

	return resp, nil
}

func (f *FleetspaceAccountServerTransport) dispatchGet(req *http.Request) (*http.Response, error) {
	if f.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\.DocumentDB/fleets/(?P<fleetName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaces/(?P<fleetspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaceAccounts/(?P<fleetspaceAccountName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)`
	regex := regexp.MustCompile(regexStr)
	matches := regex.FindStringSubmatch(req.URL.EscapedPath())
	if len(matches) < 6 {
		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
	}
	fleetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetName")])
	if err != nil {
		return nil, err
	}
	fleetspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceName")])
	if err != nil {
		return nil, err
	}
	fleetspaceAccountNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceAccountName")])
	if err != nil {
		return nil, err
	}
	respr, errRespr := f.srv.Get(req.Context(), resourceGroupNameParam, fleetNameParam, fleetspaceNameParam, fleetspaceAccountNameParam, nil)
	if respErr := server.GetError(errRespr, req); respErr != nil {
		return nil, respErr
	}
	respContent := server.GetResponseContent(respr)
	if !slices.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).FleetspaceAccountResource, req)
	if err != nil {
		return nil, err
	}
	return resp, nil
}

func (f *FleetspaceAccountServerTransport) dispatchNewListPager(req *http.Request) (*http.Response, error) {
	if f.srv.NewListPager == nil {
		return nil, &nonRetriableError{errors.New("fake for method NewListPager not implemented")}
	}
	newListPager := f.newListPager.get(req)
	if newListPager == nil {
		const regexStr = `/subscriptions/(?P<subscriptionId>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/resourceGroups/(?P<resourceGroupName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/providers/Microsoft\.DocumentDB/fleets/(?P<fleetName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaces/(?P<fleetspaceName>[!#&$-;=?-\[\]_a-zA-Z0-9~%@]+)/fleetspaceAccounts`
		regex := regexp.MustCompile(regexStr)
		matches := regex.FindStringSubmatch(req.URL.EscapedPath())
		if len(matches) < 5 {
			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
		}
		fleetNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetName")])
		if err != nil {
			return nil, err
		}
		fleetspaceNameParam, err := url.PathUnescape(matches[regex.SubexpIndex("fleetspaceName")])
		if err != nil {
			return nil, err
		}
		resp := f.srv.NewListPager(resourceGroupNameParam, fleetNameParam, fleetspaceNameParam, nil)
		newListPager = &resp
		f.newListPager.add(req, newListPager)
		server.PagerResponderInjectNextLinks(newListPager, req, func(page *armcosmos.FleetspaceAccountClientListResponse, createLink func() string) {
			page.NextLink = to.Ptr(createLink())
		})
	}
	resp, err := server.PagerResponderNext(newListPager, req)
	if err != nil {
		return nil, err
	}
	if !slices.Contains([]int{http.StatusOK}, resp.StatusCode) {
		f.newListPager.remove(req)
		return nil, &nonRetriableError{fmt.Errorf("unexpected status code %d. acceptable values are http.StatusOK", resp.StatusCode)}
	}
	if !server.PagerResponderMore(newListPager) {
		f.newListPager.remove(req)
	}
	return resp, nil
}

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