{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}

{{/* gotype: entgo.io/ent/entc/gen.Type */}}

{{ define "setter" }}
{{ $builder := pascal $.Scope.Builder }}
{{ $receiver := receiver $builder }}
{{ $fields := $.Fields }}
{{ $updater := false }}
{{- if or (hasSuffix $builder "Update") (hasSuffix $builder "UpdateOne") }}
	{{ $updater = true }}
	{{ $fields = $.MutableFields }}
{{- else if $.HasCompositeID }}
{{- else if $.ID.UserDefined }}
	{{ $fields = append $fields $.ID }}
{{- end }}

{{ range $f := $fields }}
	{{ $p := receiver $f.Type.String }}{{ if eq $p $receiver }} {{ $p = "value" }} {{ end }}
	{{ $func := print "Set" $f.StructField }}
	// {{ $func }} sets the "{{ $f.Name }}" field.
	func ({{ $receiver }} *{{ $builder }}) {{ $func }}({{ $p }} {{ $f.Type }}) *{{ $builder }} {
		{{- /* setting numeric type override previous calls to Add. */}}
		{{- if and $updater $f.SupportsMutationAdd }}
			{{ $receiver }}.mutation.{{ $f.MutationReset }}()
		{{- end }}
		{{ $receiver }}.mutation.{{ $f.MutationSet }}({{ $p }})
		return {{ $receiver }}
	}

	{{/* Avoid generating nillable setters for nillable types. */}}
	{{ if and (not $f.Type.Nillable) (or $f.Optional $f.Default) (not (and $updater $f.UpdateDefault)) }}
		{{ $nillableFunc := print "SetNillable" $f.StructField }}
		// {{ $nillableFunc }} sets the "{{ $f.Name }}" field if the given value is not nil.
		func ({{ $receiver }} *{{ $builder }}) {{ $nillableFunc }}({{ $p }} *{{ $f.Type }}) *{{ $builder }} {
			if {{ $p }} != nil {
				{{ $receiver }}.{{ $func }}(*{{ $p }})
			}
			return {{ $receiver }}
		}
	{{ end }}

	{{ if and $updater $f.SupportsMutationAdd }}
		// {{ $f.MutationAdd }} adds {{ $p }} to the "{{ $f.Name }}" field.
		func ({{ $receiver }} *{{ $builder }}) {{ $f.MutationAdd }}({{ $p }} {{ $f.SignedType }}) *{{ $builder }} {
			{{ $receiver }}.mutation.{{ $f.MutationAdd }}({{ $p }})
			return {{ $receiver }}
		}
	{{ end }}

	{{ if and $updater $f.SupportsMutationAppend }}
		// {{ $f.MutationAppend }} appends {{ $p }} to the "{{ $f.Name }}" field.
		func ({{ $receiver }} *{{ $builder }}) {{ $f.MutationAppend }}({{ $p }} {{ $f.Type }}) *{{ $builder }} {
			{{ $receiver }}.mutation.{{ $f.MutationAppend }}({{ $p }})
			return {{ $receiver }}
		}
	{{ end }}

	{{ if and $f.Optional $updater }}
		{{ $func := print "Clear" $f.StructField }}
		// {{ $func }} clears the value of the "{{ $f.Name }}" field.
		func ({{ $receiver }} *{{ $builder }}) {{ $func }}() *{{ $builder }} {
			{{ $receiver }}.mutation.{{ $func }}()
			return {{ $receiver }}
		}
	{{ end }}
{{ end }}

{{ range $e := $.EdgesWithID }}
	{{ if and $updater $e.Immutable }}
		{{/* Skip to the next one as immutable edges cannot be updated. */}}
		{{continue}}
	{{ end }}
	{{ $op := "add" }}{{ $idsFunc := $e.MutationAdd }}{{ if $e.Unique }}{{ $op = "set" }}{{ $idsFunc = $e.MutationSet }}{{ end }}
	{{/* Check if this setter was already defined by the field-setters (e.g. edge-field with the same name). */}}
	{{ $withSetter := not $e.HasFieldSetter }}
	{{ if $withSetter }}
		// {{ $idsFunc }} {{ $op }}s the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity by ID{{ if not $e.Unique }}s{{ end }}.
		func ({{ $receiver }} *{{ $builder }}) {{ $idsFunc }}({{ if $e.Unique }}id{{ else }}ids ...{{ end }} {{ $e.Type.ID.Type }}) *{{ $builder }} {
			{{ $receiver }}.mutation.{{ $idsFunc }}({{ if $e.Unique }}id{{ else }}ids ...{{ end }})
			return {{ $receiver }}
		}
	{{ end }}
	{{ if and $e.Unique $e.Optional $withSetter }}
		{{ $nillableIDsFunc := print "SetNillable" $e.StructField "ID" }}
		// {{ $nillableIDsFunc }} sets the "{{ $e.Name }}" edge to the {{ $e.Type.Name }} entity by ID if the given value is not nil.
		func ({{ $receiver }} *{{ $builder }}) {{ $nillableIDsFunc }}(id *{{ $e.Type.ID.Type }}) *{{ $builder }} {
			if id != nil {
				{{ $receiver}} = {{ $receiver }}.{{ $idsFunc }}(*id)
			}
			return {{ $receiver }}
		}
	{{ end }}
	{{ $p := lower (printf "%.1s" $e.Type.Name) }}
	{{ if eq $p $receiver }} {{ $p = "v" }} {{ end }}
	{{ $func := print (pascal $op) $e.StructField }}
	// {{ $func }} {{ $op }}s the "{{ $e.Name }}" edge{{if not $e.Unique}}s{{ end }} to the {{ $e.Type.Name }} entity.
	func ({{ $receiver }} *{{ $builder }}) {{ $func }}({{ $p }} {{ if not $e.Unique }}...{{ end }}*{{ $e.Type.Name}}) *{{ $builder }} {
		{{ if $e.Unique -}}
			return {{ $receiver }}.{{ $idsFunc }}({{ $p }}.ID)
		{{- else -}}
			ids := make([]{{ $e.Type.ID.Type }}, len({{ $p }}))
			{{ $i := "i" }}{{ if eq $i $p }}{{ $i = "j" }}{{ end -}}
			for {{ $i }} := range {{ $p }} {
				ids[{{ $i }}] = {{ $p }}[{{ $i }}].ID
			}
			return {{ $receiver }}.{{ $idsFunc }}(ids...)
		{{- end }}
	}
{{ end }}

// Mutation returns the {{ $.MutationName }} object of the builder.
func ({{ $receiver }} *{{ $builder }}) Mutation() *{{ $.MutationName }} {
	return {{ $receiver }}.mutation
}

{{ end }}
