// Copyright 2016-2023, Pulumi Corporation.
//
// 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 gen-pux-applyn; DO NOT EDIT.

//nolint:lll
package pulumix

import (
	"context"

	"github.com/pulumi/pulumi/sdk/v3/go/internal"
)

{{ range seq .Start .Stop -}}
	{{ template "applyn" . }}
{{- end }}

{{/* applyn(int):
	Generates ApplyN functions for a function with N parameters.
*/}}
{{ define "applyn" }}

{{ $name := printf "Apply%d" . -}}
{{ $inputs := printf "%d inputs" . }}
{{ if eq . 1 -}}
	{{ $name = "Apply" -}}
	{{ $inputs = "an input" -}}
{{ end -}}

// {{ $name }}ContextErr applies a function to {{ $inputs }},
// returning an Output holding the result of the function.
//
// If the function returns an error, the Output will be in an error state.
func {{ $name }}ContextErr[{{ template "typeParameters" . }}](
	ctx context.Context,
	{{ template "inputParameters" . }}
	fn func({{ range seq 1 . }} A{{ . }}, {{ end }}) (B, error),
) Output[B] {
	{{ range seq 1 . -}}
		o{{ . }} := i{{ . }}.ToOutput(ctx);
	{{- end }}

	var allDeps []internal.Resource
	{{ range seq 1 . -}}
		allDeps = append(allDeps, internal.OutputDependencies(o{{ . }})...);
	{{- end }}

	var wg internal.WorkGroup
	state := internal.NewOutputState(&wg, typeOf[B](), allDeps...)
	go func() {
		{{ range seq 1 . -}}
			var a{{ . }} A{{ . }}
		{{ end }}

		applier := newApplyNState[B](state)
		{{ range seq 1 . -}}
			applyNStep[A{{ . }}](ctx, &applier, o{{ . }}, &a{{ . }})
		{{ end }}
		if applier.ok {
			b, err := fn({{ range seq 1 . }}a{{ . }}, {{ end }})
			applier.finish(b, err)
		}
	}()

	return Output[B]{OutputState: state}
}

// {{ $name }}Context applies a function to {{ $inputs }},
// returning an Output holding the result of the function.
//
// This is a variant of {{ $name }}ContextErr
// that does not allow the function to return an error.
func {{ $name }}Context[{{ template "typeParameters" . }}](
	ctx context.Context,
	{{ template "inputParameters" . }}
	fn func({{ range seq 1 . }} A{{ . }}, {{ end }}) B,
) Output[B] {
	return {{ $name }}ContextErr(
		ctx,
		{{ range seq 1 . }}i{{ . }},{{ end }}
		func({{ range seq 1 . }} a{{ . }} A{{ . }}, {{ end }}) (B, error) {
			return fn({{ range seq 1 . }} a{{ . }}, {{ end }}), nil
		},
	)
}

// {{ $name }}Err applies a function to {{ $inputs }},
// returning an Output holding the result of the function.
//
// If the function returns an error, the Output will be in an error state.
//
// This is a variant of {{ $name }}ContextErr
// that uses the background context.
func {{ $name }}Err[{{ template "typeParameters" . }}](
	{{ template "inputParameters" . }}
	fn func({{ range seq 1 . }} A{{ . }}, {{ end }}) (B, error),
) Output[B] {
	ctx := context.Background()
	return {{ $name }}ContextErr(ctx, {{ range seq 1 . }}i{{ . }},{{ end }} fn)
}


// {{ $name }} applies a function to {{ $inputs }},
// returning an Output holding the result of the function.
//
// This is a variant of {{ $name }}ContextErr
// that does not allow the function to return an error,
// and uses the background context.
func {{ $name }}[{{ template "typeParameters" . }}](
	{{ template "inputParameters" . }}
	fn func({{ range seq 1 . }} A{{ . }}, {{ end }}) B,
) Output[B] {
	ctx := context.Background()
	return {{ $name }}Context(ctx, {{ range seq 1 . }}i{{ . }},{{ end }} fn)
}

{{ end }}

{{/* typeParameters(int):
	Generates type parameters for a function with N parameters.
	Use inside an existing '[...]'.
	Includes a trailing comma.
*/}}
{{ define "typeParameters" }}
	{{- range seq 1 . -}}
		A{{ . }},
	{{- end }} B any,
{{- end -}}

{{/* inputParameters(int):
	Generates named input parameters for a function with N parameters.
	Use inside an existing '(...)'.
	Type parameters are expected to be named A1, A2, ..., AN.
	Includes a trailing comma.
*/}}
{{ define "inputParameters" }}
	{{- range seq 1 . -}}
		i{{ . }} Input[A{{ . }}],
	{{- end }}
{{- end -}}