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

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"
)

// BookshelfPrivateLinkResourcesClient contains the methods for the BookshelfPrivateLinkResources group.
// Don't use this type directly, use NewBookshelfPrivateLinkResourcesClient() instead.
//
// Generated from API version 2026-06-01
type BookshelfPrivateLinkResourcesClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewBookshelfPrivateLinkResourcesClient creates a new instance of BookshelfPrivateLinkResourcesClient 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 - Contains optional client configuration. Pass nil to accept the default values.
func NewBookshelfPrivateLinkResourcesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*BookshelfPrivateLinkResourcesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &BookshelfPrivateLinkResourcesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// Get - Gets the specified private link resource for the bookshelf.
// If the operation fails it returns an *azcore.ResponseError type.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - bookshelfName - The name of the Bookshelf
//   - privateLinkResourceName - The name of the private link associated with the Azure resource.
//   - options - BookshelfPrivateLinkResourcesClientGetOptions contains the optional parameters for the BookshelfPrivateLinkResourcesClient.Get
//     method.
func (client *BookshelfPrivateLinkResourcesClient) Get(ctx context.Context, resourceGroupName string, bookshelfName string, privateLinkResourceName string, options *BookshelfPrivateLinkResourcesClientGetOptions) (BookshelfPrivateLinkResourcesClientGetResponse, error) {
	var err error
	const operationName = "BookshelfPrivateLinkResourcesClient.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, bookshelfName, privateLinkResourceName, options)
	if err != nil {
		return BookshelfPrivateLinkResourcesClientGetResponse{}, err
	}
	httpResp, err := client.internal.Pipeline().Do(req)
	if err != nil {
		return BookshelfPrivateLinkResourcesClientGetResponse{}, err
	}
	if !runtime.HasStatusCode(httpResp, http.StatusOK) {
		err = runtime.NewResponseError(httpResp)
		return BookshelfPrivateLinkResourcesClientGetResponse{}, err
	}
	resp, err := client.getHandleResponse(httpResp)
	return resp, err
}

// getCreateRequest creates the Get request.
func (client *BookshelfPrivateLinkResourcesClient) getCreateRequest(ctx context.Context, resourceGroupName string, bookshelfName string, privateLinkResourceName string, _ *BookshelfPrivateLinkResourcesClientGetOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Discovery/bookshelves/{bookshelfName}/privateLinkResources/{privateLinkResourceName}"
	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 bookshelfName == "" {
		return nil, errors.New("parameter bookshelfName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{bookshelfName}", url.PathEscape(bookshelfName))
	if privateLinkResourceName == "" {
		return nil, errors.New("parameter privateLinkResourceName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{privateLinkResourceName}", url.PathEscape(privateLinkResourceName))
	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", version20260601)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

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

// NewListByBookshelfPager - Lists all private link resources for the bookshelf.
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - bookshelfName - The name of the Bookshelf
//   - options - BookshelfPrivateLinkResourcesClientListByBookshelfOptions contains the optional parameters for the BookshelfPrivateLinkResourcesClient.NewListByBookshelfPager
//     method.
func (client *BookshelfPrivateLinkResourcesClient) NewListByBookshelfPager(resourceGroupName string, bookshelfName string, options *BookshelfPrivateLinkResourcesClientListByBookshelfOptions) *runtime.Pager[BookshelfPrivateLinkResourcesClientListByBookshelfResponse] {
	return runtime.NewPager(runtime.PagingHandler[BookshelfPrivateLinkResourcesClientListByBookshelfResponse]{
		More: func(page BookshelfPrivateLinkResourcesClientListByBookshelfResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *BookshelfPrivateLinkResourcesClientListByBookshelfResponse) (BookshelfPrivateLinkResourcesClientListByBookshelfResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "BookshelfPrivateLinkResourcesClient.NewListByBookshelfPager")
			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.listByBookshelfCreateRequest(ctx, resourceGroupName, bookshelfName, options)
			}, nil)
			if err != nil {
				return BookshelfPrivateLinkResourcesClientListByBookshelfResponse{}, err
			}
			return client.listByBookshelfHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listByBookshelfCreateRequest creates the ListByBookshelf request.
func (client *BookshelfPrivateLinkResourcesClient) listByBookshelfCreateRequest(ctx context.Context, resourceGroupName string, bookshelfName string, _ *BookshelfPrivateLinkResourcesClientListByBookshelfOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Discovery/bookshelves/{bookshelfName}/privateLinkResources"
	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 bookshelfName == "" {
		return nil, errors.New("parameter bookshelfName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{bookshelfName}", url.PathEscape(bookshelfName))
	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", version20260601)
	req.Raw().URL.RawQuery = strings.ReplaceAll(reqQP.Encode(), "+", "%20")
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listByBookshelfHandleResponse handles the ListByBookshelf response.
func (client *BookshelfPrivateLinkResourcesClient) listByBookshelfHandleResponse(resp *http.Response) (BookshelfPrivateLinkResourcesClientListByBookshelfResponse, error) {
	result := BookshelfPrivateLinkResourcesClientListByBookshelfResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.BookshelfPrivateLinkResourceListResult); err != nil {
		return BookshelfPrivateLinkResourcesClientListByBookshelfResponse{}, err
	}
	return result, nil
}
