// 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 armdependencymap

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// DiscoverySourcesClient contains the methods for the DiscoverySources group.
// Don't use this type directly, use NewDiscoverySourcesClient() instead.
type DiscoverySourcesClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewDiscoverySourcesClient creates a new instance of DiscoverySourcesClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewDiscoverySourcesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*DiscoverySourcesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &DiscoverySourcesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// BeginCreateOrUpdate - Create a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - mapName - Maps resource name
//   - sourceName - discovery source resource
//   - resource - Resource create parameters.
//   - options - DiscoverySourcesClientBeginCreateOrUpdateOptions contains the optional parameters for the DiscoverySourcesClient.BeginCreateOrUpdate
//     method.
func (client *DiscoverySourcesClient) BeginCreateOrUpdate(ctx context.Context, resourceGroupName string, mapName string, sourceName string, resource DiscoverySourceResource, options *DiscoverySourcesClientBeginCreateOrUpdateOptions) (*runtime.Poller[DiscoverySourcesClientCreateOrUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.createOrUpdate(ctx, resourceGroupName, mapName, sourceName, resource, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DiscoverySourcesClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DiscoverySourcesClientCreateOrUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// CreateOrUpdate - Create a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
func (client *DiscoverySourcesClient) createOrUpdate(ctx context.Context, resourceGroupName string, mapName string, sourceName string, resource DiscoverySourceResource, options *DiscoverySourcesClientBeginCreateOrUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "DiscoverySourcesClient.BeginCreateOrUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.createOrUpdateCreateRequest(ctx, resourceGroupName, mapName, sourceName, resource, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusCreated) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// createOrUpdateCreateRequest creates the CreateOrUpdate request.
func (client *DiscoverySourcesClient) createOrUpdateCreateRequest(ctx context.Context, resourceGroupName string, mapName string, sourceName string, resource DiscoverySourceResource, _ *DiscoverySourcesClientBeginCreateOrUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DependencyMap/maps/{mapName}/discoverySources/{sourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if mapName == "" {
		return nil, errors.New("parameter mapName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{mapName}", url.PathEscape(mapName))
	if sourceName == "" {
		return nil, errors.New("parameter sourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{sourceName}", url.PathEscape(sourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPut, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-31-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, resource); err != nil {
		return nil, err
	}
	return req, nil
}

// BeginDelete - Delete a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - mapName - Maps resource name
//   - sourceName - discovery source resource
//   - options - DiscoverySourcesClientBeginDeleteOptions contains the optional parameters for the DiscoverySourcesClient.BeginDelete
//     method.
func (client *DiscoverySourcesClient) BeginDelete(ctx context.Context, resourceGroupName string, mapName string, sourceName string, options *DiscoverySourcesClientBeginDeleteOptions) (*runtime.Poller[DiscoverySourcesClientDeleteResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.deleteOperation(ctx, resourceGroupName, mapName, sourceName, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DiscoverySourcesClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DiscoverySourcesClientDeleteResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Delete - Delete a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
func (client *DiscoverySourcesClient) deleteOperation(ctx context.Context, resourceGroupName string, mapName string, sourceName string, options *DiscoverySourcesClientBeginDeleteOptions) (*http.Response, error) {
	var err error
	const operationName = "DiscoverySourcesClient.BeginDelete"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.deleteCreateRequest(ctx, resourceGroupName, mapName, sourceName, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusAccepted, http.StatusNoContent) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// deleteCreateRequest creates the Delete request.
func (client *DiscoverySourcesClient) deleteCreateRequest(ctx context.Context, resourceGroupName string, mapName string, sourceName string, _ *DiscoverySourcesClientBeginDeleteOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DependencyMap/maps/{mapName}/discoverySources/{sourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if mapName == "" {
		return nil, errors.New("parameter mapName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{mapName}", url.PathEscape(mapName))
	if sourceName == "" {
		return nil, errors.New("parameter sourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{sourceName}", url.PathEscape(sourceName))
	req, err := runtime.NewRequest(ctx, http.MethodDelete, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-31-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// Get - Get a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - mapName - Maps resource name
//   - sourceName - discovery source resource
//   - options - DiscoverySourcesClientGetOptions contains the optional parameters for the DiscoverySourcesClient.Get method.
func (client *DiscoverySourcesClient) Get(ctx context.Context, resourceGroupName string, mapName string, sourceName string, options *DiscoverySourcesClientGetOptions) (DiscoverySourcesClientGetResponse, error) {
	var err error
	const operationName = "DiscoverySourcesClient.Get"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.getCreateRequest(ctx, resourceGroupName, mapName, sourceName, options)
	if err != nil {
		return DiscoverySourcesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return DiscoverySourcesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return DiscoverySourcesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *DiscoverySourcesClient) getCreateRequest(ctx context.Context, resourceGroupName string, mapName string, sourceName string, _ *DiscoverySourcesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DependencyMap/maps/{mapName}/discoverySources/{sourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if mapName == "" {
		return nil, errors.New("parameter mapName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{mapName}", url.PathEscape(mapName))
	if sourceName == "" {
		return nil, errors.New("parameter sourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{sourceName}", url.PathEscape(sourceName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-31-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// getHandleResponse handles the Get response.
func (client *DiscoverySourcesClient) getHandleResponse(resp *http.Response) (DiscoverySourcesClientGetResponse, error) {
	result := DiscoverySourcesClientGetResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DiscoverySourceResource); err != nil {
		return DiscoverySourcesClientGetResponse{}, err
	}
	return result, nil
}

// NewListByMapsResourcePager - List DiscoverySourceResource resources by MapsResource
//
// Generated from API version 2025-01-31-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - mapName - Maps resource name
//   - options - DiscoverySourcesClientListByMapsResourceOptions contains the optional parameters for the DiscoverySourcesClient.NewListByMapsResourcePager
//     method.
func (client *DiscoverySourcesClient) NewListByMapsResourcePager(resourceGroupName string, mapName string, options *DiscoverySourcesClientListByMapsResourceOptions) *runtime.Pager[DiscoverySourcesClientListByMapsResourceResponse] {
	return runtime.NewPager(runtime.PagingHandler[DiscoverySourcesClientListByMapsResourceResponse]{
		More: func(page DiscoverySourcesClientListByMapsResourceResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *DiscoverySourcesClientListByMapsResourceResponse) (DiscoverySourcesClientListByMapsResourceResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "DiscoverySourcesClient.NewListByMapsResourcePager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listByMapsResourceCreateRequest(ctx, resourceGroupName, mapName, options)
			}, nil)
			if err != nil {
				return DiscoverySourcesClientListByMapsResourceResponse{}, err
			}
			return client.listByMapsResourceHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByMapsResourceCreateRequest creates the ListByMapsResource request.
func (client *DiscoverySourcesClient) listByMapsResourceCreateRequest(ctx context.Context, resourceGroupName string, mapName string, _ *DiscoverySourcesClientListByMapsResourceOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DependencyMap/maps/{mapName}/discoverySources"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if mapName == "" {
		return nil, errors.New("parameter mapName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{mapName}", url.PathEscape(mapName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-31-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByMapsResourceHandleResponse handles the ListByMapsResource response.
func (client *DiscoverySourcesClient) listByMapsResourceHandleResponse(resp *http.Response) (DiscoverySourcesClientListByMapsResourceResponse, error) {
	result := DiscoverySourcesClientListByMapsResourceResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.DiscoverySourceResourceListResult); err != nil {
		return DiscoverySourcesClientListByMapsResourceResponse{}, err
	}
	return result, nil
}

// BeginUpdate - Update a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - mapName - Maps resource name
//   - sourceName - discovery source resource
//   - properties - The resource properties to be updated.
//   - options - DiscoverySourcesClientBeginUpdateOptions contains the optional parameters for the DiscoverySourcesClient.BeginUpdate
//     method.
func (client *DiscoverySourcesClient) BeginUpdate(ctx context.Context, resourceGroupName string, mapName string, sourceName string, properties DiscoverySourceResourceTagsUpdate, options *DiscoverySourcesClientBeginUpdateOptions) (*runtime.Poller[DiscoverySourcesClientUpdateResponse], error) {
	if options == nil || options.ResumeToken == "" {
		resp, err := client.update(ctx, resourceGroupName, mapName, sourceName, properties, options)
		if err != nil {
			return nil, err
		}
		poller, err := runtime.NewPoller(resp, client.internal.Pipeline(), &runtime.NewPollerOptions[DiscoverySourcesClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
		return poller, err
	} else {
		return runtime.NewPollerFromResumeToken(options.ResumeToken, client.internal.Pipeline(), &runtime.NewPollerFromResumeTokenOptions[DiscoverySourcesClientUpdateResponse]{
			Tracer: client.internal.Tracer(),
		})
	}
}

// Update - Update a DiscoverySourceResource
// If the operation fails it returns an *azcore.ResponseError type.
//
// Generated from API version 2025-01-31-preview
func (client *DiscoverySourcesClient) update(ctx context.Context, resourceGroupName string, mapName string, sourceName string, properties DiscoverySourceResourceTagsUpdate, options *DiscoverySourcesClientBeginUpdateOptions) (*http.Response, error) {
	var err error
	const operationName = "DiscoverySourcesClient.BeginUpdate"
	ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, operationName)
	ctx, endSpan := runtime.StartSpan(ctx, operationName, client.internal.Tracer(), nil)
	defer func() { endSpan(err) }()
	req, err := client.updateCreateRequest(ctx, resourceGroupName, mapName, sourceName, properties, options)
	if err != nil {
		return nil, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return nil, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK, http.StatusAccepted) {
		err = runtime.NewResponseError(httpResp)
		return nil, err
	}
	return httpResp, nil
}

// updateCreateRequest creates the Update request.
func (client *DiscoverySourcesClient) updateCreateRequest(ctx context.Context, resourceGroupName string, mapName string, sourceName string, properties DiscoverySourceResourceTagsUpdate, _ *DiscoverySourcesClientBeginUpdateOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.DependencyMap/maps/{mapName}/discoverySources/{sourceName}"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if mapName == "" {
		return nil, errors.New("parameter mapName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{mapName}", url.PathEscape(mapName))
	if sourceName == "" {
		return nil, errors.New("parameter sourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{sourceName}", url.PathEscape(sourceName))
	req, err := runtime.NewRequest(ctx, http.MethodPatch, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-01-31-preview")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	req.Raw().Header["Content-Type"] = []string{"application/json"}
	if err := runtime.MarshalAsJSON(req, properties); err != nil {
		return nil, err
	}
	return req, nil
}
