// Copyright 2020-2021 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Code generated by protoc-gen-go-apiclienttwirp. DO NOT EDIT.

package registryv1alpha1apiclienttwirp

import (
	context "context"
	registryv1alpha1api "github.com/bufbuild/buf/internal/gen/proto/api/buf/alpha/registry/v1alpha1/registryv1alpha1api"
	registryv1alpha1apiclient "github.com/bufbuild/buf/internal/gen/proto/apiclient/buf/alpha/registry/v1alpha1/registryv1alpha1apiclient"
	v1alpha1 "github.com/bufbuild/buf/internal/gen/proto/go/buf/alpha/registry/v1alpha1"
	httpclient "github.com/bufbuild/buf/internal/pkg/transport/http/httpclient"
	twirpclient "github.com/bufbuild/buf/internal/pkg/transport/twirp/twirpclient"
	zap "go.uber.org/zap"
)

// NewProvider returns a new Provider.
func NewProvider(
	logger *zap.Logger,
	httpClient httpclient.Client,
	options ...ProviderOption,
) registryv1alpha1apiclient.Provider {
	provider := &provider{
		logger:     logger,
		httpClient: httpClient,
	}
	for _, option := range options {
		option(provider)
	}
	return provider
}

type provider struct {
	logger        *zap.Logger
	httpClient    httpclient.Client
	addressMapper func(string) string
}

// ProviderOption is an option for a new Provider.
type ProviderOption func(*provider)

// WithAddressMapper maps the address with the given function.
func WithAddressMapper(addressMapper func(string) string) ProviderOption {
	return func(provider *provider) {
		provider.addressMapper = addressMapper
	}
}

func (p *provider) NewDownloadService(ctx context.Context, address string) (registryv1alpha1api.DownloadService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &downloadService{
		logger: p.logger,
		client: v1alpha1.NewDownloadServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewOrganizationService(ctx context.Context, address string) (registryv1alpha1api.OrganizationService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &organizationService{
		logger: p.logger,
		client: v1alpha1.NewOrganizationServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewPushService(ctx context.Context, address string) (registryv1alpha1api.PushService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &pushService{
		logger: p.logger,
		client: v1alpha1.NewPushServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewRepositoryBranchService(ctx context.Context, address string) (registryv1alpha1api.RepositoryBranchService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &repositoryBranchService{
		logger: p.logger,
		client: v1alpha1.NewRepositoryBranchServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewRepositoryCommitService(ctx context.Context, address string) (registryv1alpha1api.RepositoryCommitService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &repositoryCommitService{
		logger: p.logger,
		client: v1alpha1.NewRepositoryCommitServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewRepositoryService(ctx context.Context, address string) (registryv1alpha1api.RepositoryService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &repositoryService{
		logger: p.logger,
		client: v1alpha1.NewRepositoryServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewResolveService(ctx context.Context, address string) (registryv1alpha1api.ResolveService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &resolveService{
		logger: p.logger,
		client: v1alpha1.NewResolveServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}

func (p *provider) NewUserService(ctx context.Context, address string) (registryv1alpha1api.UserService, error) {
	if p.addressMapper != nil {
		address = p.addressMapper(address)
	}
	return &userService{
		logger: p.logger,
		client: v1alpha1.NewUserServiceProtobufClient(
			p.httpClient.ParseAddress(address),
			p.httpClient,
			twirpclient.NewClientOptions()...,
		),
	}, nil
}
