// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package proto // import "go.opentelemetry.io/collector/internal/cmd/pdatagen/internal/proto"

import (
	"go.opentelemetry.io/collector/internal/cmd/pdatagen/internal/tmplutil"
)

const copyOther = `{{ if .repeated -}}
	dest.{{ .fieldName }} = append(dest.{{ .fieldName }}[:0], src.{{ .fieldName }}...)
{{ else if ne .oneOfGroup "" -}}
	var ov *{{ .oneOfMessageName }}
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		ov = &{{ .oneOfMessageName }}{}
	} else {
		ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageName }})
	}
	ov.{{ .fieldName }} = t.{{ .fieldName }}
	dest.{{ .oneOfGroup }} = ov
{{ else if .nullable -}}
	if src.Has{{ .fieldName }}() {
		dest.Set{{ .fieldName }}(src.{{ .fieldName }})
	} else {
		dest.Remove{{ .fieldName }}()
	}
{{ else -}}
	dest.{{ .fieldName }} = src.{{ .fieldName }}
{{- end }}`

const copyMessage = `{{ if .repeated -}}
	dest.{{ .fieldName }} = Copy{{ .messageName }}{{ if .nullable }}Ptr{{ end }}Slice(dest.{{ .fieldName }}, src.{{ .fieldName }})
{{- else if ne .oneOfGroup "" -}}
	var ov *{{ .oneOfMessageName }}
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		ov = &{{ .oneOfMessageName }}{}
	} else {
		ov = ProtoPool{{ .oneOfMessageName }}.Get().(*{{ .oneOfMessageName }})
	}
	ov.{{ .fieldName }} = New{{ .messageName }}()
	Copy{{ .messageName }}(ov.{{ .fieldName }}, t.{{ .fieldName }})
	dest.{{ .oneOfGroup }} = ov	
{{- else if .nullable -}}
	dest.{{ .fieldName }} = Copy{{ .messageName }}(dest.{{ .fieldName }}, src.{{ .fieldName }})
{{- else -}}
	Copy{{ .messageName }}(&dest.{{ .fieldName }}, &src.{{ .fieldName }})
{{- end }}
`

func (pf *Field) GenCopy() string {
	tf := pf.getTemplateFields()
	if pf.Type == TypeMessage {
		return tmplutil.Execute(tmplutil.Parse("copyMessage", []byte(copyMessage)), tf)
	}
	return tmplutil.Execute(tmplutil.Parse("copyOther", []byte(copyOther)), tf)
}
