// 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 (
	"errors"
	"fmt"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"strings"
	"sync"
)

// ServerFactory is a fake server for instances of the armcognitiveservices.ClientFactory type.
type ServerFactory struct {
	// AccountCapabilityHostsServer contains the fakes for client AccountCapabilityHostsClient
	AccountCapabilityHostsServer AccountCapabilityHostsServer

	// AccountConnectionsServer contains the fakes for client AccountConnectionsClient
	AccountConnectionsServer AccountConnectionsServer

	// AccountsServer contains the fakes for client AccountsClient
	AccountsServer AccountsServer

	// CommitmentPlansServer contains the fakes for client CommitmentPlansClient
	CommitmentPlansServer CommitmentPlansServer

	// CommitmentTiersServer contains the fakes for client CommitmentTiersClient
	CommitmentTiersServer CommitmentTiersServer

	// DefenderForAISettingsServer contains the fakes for client DefenderForAISettingsClient
	DefenderForAISettingsServer DefenderForAISettingsServer

	// DeletedAccountsServer contains the fakes for client DeletedAccountsClient
	DeletedAccountsServer DeletedAccountsServer

	// DeploymentsServer contains the fakes for client DeploymentsClient
	DeploymentsServer DeploymentsServer

	// EncryptionScopesServer contains the fakes for client EncryptionScopesClient
	EncryptionScopesServer EncryptionScopesServer

	// LocationBasedModelCapacitiesServer contains the fakes for client LocationBasedModelCapacitiesClient
	LocationBasedModelCapacitiesServer LocationBasedModelCapacitiesServer

	// ManagementServer contains the fakes for client ManagementClient
	ManagementServer ManagementServer

	// ModelCapacitiesServer contains the fakes for client ModelCapacitiesClient
	ModelCapacitiesServer ModelCapacitiesServer

	// ModelsServer contains the fakes for client ModelsClient
	ModelsServer ModelsServer

	// NetworkSecurityPerimeterConfigurationsServer contains the fakes for client NetworkSecurityPerimeterConfigurationsClient
	NetworkSecurityPerimeterConfigurationsServer NetworkSecurityPerimeterConfigurationsServer

	// OperationsServer contains the fakes for client OperationsClient
	OperationsServer OperationsServer

	// PrivateEndpointConnectionsServer contains the fakes for client PrivateEndpointConnectionsClient
	PrivateEndpointConnectionsServer PrivateEndpointConnectionsServer

	// PrivateLinkResourcesServer contains the fakes for client PrivateLinkResourcesClient
	PrivateLinkResourcesServer PrivateLinkResourcesServer

	// ProjectCapabilityHostsServer contains the fakes for client ProjectCapabilityHostsClient
	ProjectCapabilityHostsServer ProjectCapabilityHostsServer

	// ProjectConnectionsServer contains the fakes for client ProjectConnectionsClient
	ProjectConnectionsServer ProjectConnectionsServer

	// ProjectsServer contains the fakes for client ProjectsClient
	ProjectsServer ProjectsServer

	// RaiBlocklistItemsServer contains the fakes for client RaiBlocklistItemsClient
	RaiBlocklistItemsServer RaiBlocklistItemsServer

	// RaiBlocklistsServer contains the fakes for client RaiBlocklistsClient
	RaiBlocklistsServer RaiBlocklistsServer

	// RaiContentFiltersServer contains the fakes for client RaiContentFiltersClient
	RaiContentFiltersServer RaiContentFiltersServer

	// RaiPoliciesServer contains the fakes for client RaiPoliciesClient
	RaiPoliciesServer RaiPoliciesServer

	// ResourceSKUsServer contains the fakes for client ResourceSKUsClient
	ResourceSKUsServer ResourceSKUsServer

	// UsagesServer contains the fakes for client UsagesClient
	UsagesServer UsagesServer
}

// NewServerFactoryTransport creates a new instance of ServerFactoryTransport with the provided implementation.
// The returned ServerFactoryTransport instance is connected to an instance of armcognitiveservices.ClientFactory via the
// azcore.ClientOptions.Transporter field in the client's constructor parameters.
func NewServerFactoryTransport(srv *ServerFactory) *ServerFactoryTransport {
	return &ServerFactoryTransport{
		srv: srv,
	}
}

// ServerFactoryTransport connects instances of armcognitiveservices.ClientFactory to instances of ServerFactory.
// Don't use this type directly, use NewServerFactoryTransport instead.
type ServerFactoryTransport struct {
	srv                                            *ServerFactory
	trMu                                           sync.Mutex
	trAccountCapabilityHostsServer                 *AccountCapabilityHostsServerTransport
	trAccountConnectionsServer                     *AccountConnectionsServerTransport
	trAccountsServer                               *AccountsServerTransport
	trCommitmentPlansServer                        *CommitmentPlansServerTransport
	trCommitmentTiersServer                        *CommitmentTiersServerTransport
	trDefenderForAISettingsServer                  *DefenderForAISettingsServerTransport
	trDeletedAccountsServer                        *DeletedAccountsServerTransport
	trDeploymentsServer                            *DeploymentsServerTransport
	trEncryptionScopesServer                       *EncryptionScopesServerTransport
	trLocationBasedModelCapacitiesServer           *LocationBasedModelCapacitiesServerTransport
	trManagementServer                             *ManagementServerTransport
	trModelCapacitiesServer                        *ModelCapacitiesServerTransport
	trModelsServer                                 *ModelsServerTransport
	trNetworkSecurityPerimeterConfigurationsServer *NetworkSecurityPerimeterConfigurationsServerTransport
	trOperationsServer                             *OperationsServerTransport
	trPrivateEndpointConnectionsServer             *PrivateEndpointConnectionsServerTransport
	trPrivateLinkResourcesServer                   *PrivateLinkResourcesServerTransport
	trProjectCapabilityHostsServer                 *ProjectCapabilityHostsServerTransport
	trProjectConnectionsServer                     *ProjectConnectionsServerTransport
	trProjectsServer                               *ProjectsServerTransport
	trRaiBlocklistItemsServer                      *RaiBlocklistItemsServerTransport
	trRaiBlocklistsServer                          *RaiBlocklistsServerTransport
	trRaiContentFiltersServer                      *RaiContentFiltersServerTransport
	trRaiPoliciesServer                            *RaiPoliciesServerTransport
	trResourceSKUsServer                           *ResourceSKUsServerTransport
	trUsagesServer                                 *UsagesServerTransport
}

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

	client := method[:strings.Index(method, ".")]
	var resp *http.Response
	var err error

	switch client {
	case "AccountCapabilityHostsClient":
		initServer(s, &s.trAccountCapabilityHostsServer, func() *AccountCapabilityHostsServerTransport {
			return NewAccountCapabilityHostsServerTransport(&s.srv.AccountCapabilityHostsServer)
		})
		resp, err = s.trAccountCapabilityHostsServer.Do(req)
	case "AccountConnectionsClient":
		initServer(s, &s.trAccountConnectionsServer, func() *AccountConnectionsServerTransport {
			return NewAccountConnectionsServerTransport(&s.srv.AccountConnectionsServer)
		})
		resp, err = s.trAccountConnectionsServer.Do(req)
	case "AccountsClient":
		initServer(s, &s.trAccountsServer, func() *AccountsServerTransport { return NewAccountsServerTransport(&s.srv.AccountsServer) })
		resp, err = s.trAccountsServer.Do(req)
	case "CommitmentPlansClient":
		initServer(s, &s.trCommitmentPlansServer, func() *CommitmentPlansServerTransport {
			return NewCommitmentPlansServerTransport(&s.srv.CommitmentPlansServer)
		})
		resp, err = s.trCommitmentPlansServer.Do(req)
	case "CommitmentTiersClient":
		initServer(s, &s.trCommitmentTiersServer, func() *CommitmentTiersServerTransport {
			return NewCommitmentTiersServerTransport(&s.srv.CommitmentTiersServer)
		})
		resp, err = s.trCommitmentTiersServer.Do(req)
	case "DefenderForAISettingsClient":
		initServer(s, &s.trDefenderForAISettingsServer, func() *DefenderForAISettingsServerTransport {
			return NewDefenderForAISettingsServerTransport(&s.srv.DefenderForAISettingsServer)
		})
		resp, err = s.trDefenderForAISettingsServer.Do(req)
	case "DeletedAccountsClient":
		initServer(s, &s.trDeletedAccountsServer, func() *DeletedAccountsServerTransport {
			return NewDeletedAccountsServerTransport(&s.srv.DeletedAccountsServer)
		})
		resp, err = s.trDeletedAccountsServer.Do(req)
	case "DeploymentsClient":
		initServer(s, &s.trDeploymentsServer, func() *DeploymentsServerTransport { return NewDeploymentsServerTransport(&s.srv.DeploymentsServer) })
		resp, err = s.trDeploymentsServer.Do(req)
	case "EncryptionScopesClient":
		initServer(s, &s.trEncryptionScopesServer, func() *EncryptionScopesServerTransport {
			return NewEncryptionScopesServerTransport(&s.srv.EncryptionScopesServer)
		})
		resp, err = s.trEncryptionScopesServer.Do(req)
	case "LocationBasedModelCapacitiesClient":
		initServer(s, &s.trLocationBasedModelCapacitiesServer, func() *LocationBasedModelCapacitiesServerTransport {
			return NewLocationBasedModelCapacitiesServerTransport(&s.srv.LocationBasedModelCapacitiesServer)
		})
		resp, err = s.trLocationBasedModelCapacitiesServer.Do(req)
	case "ManagementClient":
		initServer(s, &s.trManagementServer, func() *ManagementServerTransport { return NewManagementServerTransport(&s.srv.ManagementServer) })
		resp, err = s.trManagementServer.Do(req)
	case "ModelCapacitiesClient":
		initServer(s, &s.trModelCapacitiesServer, func() *ModelCapacitiesServerTransport {
			return NewModelCapacitiesServerTransport(&s.srv.ModelCapacitiesServer)
		})
		resp, err = s.trModelCapacitiesServer.Do(req)
	case "ModelsClient":
		initServer(s, &s.trModelsServer, func() *ModelsServerTransport { return NewModelsServerTransport(&s.srv.ModelsServer) })
		resp, err = s.trModelsServer.Do(req)
	case "NetworkSecurityPerimeterConfigurationsClient":
		initServer(s, &s.trNetworkSecurityPerimeterConfigurationsServer, func() *NetworkSecurityPerimeterConfigurationsServerTransport {
			return NewNetworkSecurityPerimeterConfigurationsServerTransport(&s.srv.NetworkSecurityPerimeterConfigurationsServer)
		})
		resp, err = s.trNetworkSecurityPerimeterConfigurationsServer.Do(req)
	case "OperationsClient":
		initServer(s, &s.trOperationsServer, func() *OperationsServerTransport { return NewOperationsServerTransport(&s.srv.OperationsServer) })
		resp, err = s.trOperationsServer.Do(req)
	case "PrivateEndpointConnectionsClient":
		initServer(s, &s.trPrivateEndpointConnectionsServer, func() *PrivateEndpointConnectionsServerTransport {
			return NewPrivateEndpointConnectionsServerTransport(&s.srv.PrivateEndpointConnectionsServer)
		})
		resp, err = s.trPrivateEndpointConnectionsServer.Do(req)
	case "PrivateLinkResourcesClient":
		initServer(s, &s.trPrivateLinkResourcesServer, func() *PrivateLinkResourcesServerTransport {
			return NewPrivateLinkResourcesServerTransport(&s.srv.PrivateLinkResourcesServer)
		})
		resp, err = s.trPrivateLinkResourcesServer.Do(req)
	case "ProjectCapabilityHostsClient":
		initServer(s, &s.trProjectCapabilityHostsServer, func() *ProjectCapabilityHostsServerTransport {
			return NewProjectCapabilityHostsServerTransport(&s.srv.ProjectCapabilityHostsServer)
		})
		resp, err = s.trProjectCapabilityHostsServer.Do(req)
	case "ProjectConnectionsClient":
		initServer(s, &s.trProjectConnectionsServer, func() *ProjectConnectionsServerTransport {
			return NewProjectConnectionsServerTransport(&s.srv.ProjectConnectionsServer)
		})
		resp, err = s.trProjectConnectionsServer.Do(req)
	case "ProjectsClient":
		initServer(s, &s.trProjectsServer, func() *ProjectsServerTransport { return NewProjectsServerTransport(&s.srv.ProjectsServer) })
		resp, err = s.trProjectsServer.Do(req)
	case "RaiBlocklistItemsClient":
		initServer(s, &s.trRaiBlocklistItemsServer, func() *RaiBlocklistItemsServerTransport {
			return NewRaiBlocklistItemsServerTransport(&s.srv.RaiBlocklistItemsServer)
		})
		resp, err = s.trRaiBlocklistItemsServer.Do(req)
	case "RaiBlocklistsClient":
		initServer(s, &s.trRaiBlocklistsServer, func() *RaiBlocklistsServerTransport {
			return NewRaiBlocklistsServerTransport(&s.srv.RaiBlocklistsServer)
		})
		resp, err = s.trRaiBlocklistsServer.Do(req)
	case "RaiContentFiltersClient":
		initServer(s, &s.trRaiContentFiltersServer, func() *RaiContentFiltersServerTransport {
			return NewRaiContentFiltersServerTransport(&s.srv.RaiContentFiltersServer)
		})
		resp, err = s.trRaiContentFiltersServer.Do(req)
	case "RaiPoliciesClient":
		initServer(s, &s.trRaiPoliciesServer, func() *RaiPoliciesServerTransport { return NewRaiPoliciesServerTransport(&s.srv.RaiPoliciesServer) })
		resp, err = s.trRaiPoliciesServer.Do(req)
	case "ResourceSKUsClient":
		initServer(s, &s.trResourceSKUsServer, func() *ResourceSKUsServerTransport { return NewResourceSKUsServerTransport(&s.srv.ResourceSKUsServer) })
		resp, err = s.trResourceSKUsServer.Do(req)
	case "UsagesClient":
		initServer(s, &s.trUsagesServer, func() *UsagesServerTransport { return NewUsagesServerTransport(&s.srv.UsagesServer) })
		resp, err = s.trUsagesServer.Do(req)
	default:
		err = fmt.Errorf("unhandled client %s", client)
	}

	if err != nil {
		return nil, err
	}

	return resp, nil
}

func initServer[T any](s *ServerFactoryTransport, dst **T, src func() *T) {
	s.trMu.Lock()
	if *dst == nil {
		*dst = src()
	}
	s.trMu.Unlock()
}
