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

// Code generated by "internal/cmd/pdatagen/main.go". DO NOT EDIT.
// To regenerate this file run "make genpdata".

package internal

import (
	"fmt"
	"sync"

	"go.opentelemetry.io/collector/pdata/internal/json"
	"go.opentelemetry.io/collector/pdata/internal/metadata"
	"go.opentelemetry.io/collector/pdata/internal/proto"
)

// Profiles is the top-level struct that is propagated through the profiles pipeline.
// Use NewProfiles to create new instance, zero-initialized instance is not valid for use.
type ExportProfilesServiceRequest struct {
	ResourceProfiles []*ResourceProfiles
	Dictionary       ProfilesDictionary
}

var (
	protoPoolExportProfilesServiceRequest = sync.Pool{
		New: func() any {
			return &ExportProfilesServiceRequest{}
		},
	}
)

func NewExportProfilesServiceRequest() *ExportProfilesServiceRequest {
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		return &ExportProfilesServiceRequest{}
	}
	return protoPoolExportProfilesServiceRequest.Get().(*ExportProfilesServiceRequest)
}

func DeleteExportProfilesServiceRequest(orig *ExportProfilesServiceRequest, nullable bool) {
	if orig == nil {
		return
	}

	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		orig.Reset()
		return
	}
	for i := range orig.ResourceProfiles {
		DeleteResourceProfiles(orig.ResourceProfiles[i], true)
	}
	DeleteProfilesDictionary(&orig.Dictionary, false)
	orig.Reset()
	if nullable {
		protoPoolExportProfilesServiceRequest.Put(orig)
	}
}

func CopyExportProfilesServiceRequest(dest, src *ExportProfilesServiceRequest) *ExportProfilesServiceRequest {
	// If copying to same object, just return.
	if src == dest {
		return dest
	}

	if src == nil {
		return nil
	}

	if dest == nil {
		dest = NewExportProfilesServiceRequest()
	}
	dest.ResourceProfiles = CopyResourceProfilesPtrSlice(dest.ResourceProfiles, src.ResourceProfiles)

	CopyProfilesDictionary(&dest.Dictionary, &src.Dictionary)

	return dest
}

func CopyExportProfilesServiceRequestSlice(dest, src []ExportProfilesServiceRequest) []ExportProfilesServiceRequest {
	var newDest []ExportProfilesServiceRequest
	if cap(dest) < len(src) {
		newDest = make([]ExportProfilesServiceRequest, len(src))
	} else {
		newDest = dest[:len(src)]
		// Cleanup the rest of the elements so GC can free the memory.
		// This can happen when len(src) < len(dest) < cap(dest).
		for i := len(src); i < len(dest); i++ {
			DeleteExportProfilesServiceRequest(&dest[i], false)
		}
	}
	for i := range src {
		CopyExportProfilesServiceRequest(&newDest[i], &src[i])
	}
	return newDest
}

func CopyExportProfilesServiceRequestPtrSlice(dest, src []*ExportProfilesServiceRequest) []*ExportProfilesServiceRequest {
	var newDest []*ExportProfilesServiceRequest
	if cap(dest) < len(src) {
		newDest = make([]*ExportProfilesServiceRequest, len(src))
		// Copy old pointers to re-use.
		copy(newDest, dest)
		// Add new pointers for missing elements from len(dest) to len(srt).
		for i := len(dest); i < len(src); i++ {
			newDest[i] = NewExportProfilesServiceRequest()
		}
	} else {
		newDest = dest[:len(src)]
		// Cleanup the rest of the elements so GC can free the memory.
		// This can happen when len(src) < len(dest) < cap(dest).
		for i := len(src); i < len(dest); i++ {
			DeleteExportProfilesServiceRequest(dest[i], true)
			dest[i] = nil
		}
		// Add new pointers for missing elements.
		// This can happen when len(dest) < len(src) < cap(dest).
		for i := len(dest); i < len(src); i++ {
			newDest[i] = NewExportProfilesServiceRequest()
		}
	}
	for i := range src {
		CopyExportProfilesServiceRequest(newDest[i], src[i])
	}
	return newDest
}

func (orig *ExportProfilesServiceRequest) Reset() {
	*orig = ExportProfilesServiceRequest{}
}

// MarshalJSON marshals all properties from the current struct to the destination stream.
func (orig *ExportProfilesServiceRequest) MarshalJSON(dest *json.Stream) {
	dest.WriteObjectStart()
	if len(orig.ResourceProfiles) > 0 {
		dest.WriteObjectField("resourceProfiles")
		dest.WriteArrayStart()
		orig.ResourceProfiles[0].MarshalJSON(dest)
		for i := 1; i < len(orig.ResourceProfiles); i++ {
			dest.WriteMore()
			orig.ResourceProfiles[i].MarshalJSON(dest)
		}
		dest.WriteArrayEnd()
	}
	dest.WriteObjectField("dictionary")
	orig.Dictionary.MarshalJSON(dest)
	dest.WriteObjectEnd()
}

// UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
func (orig *ExportProfilesServiceRequest) UnmarshalJSON(iter *json.Iterator) {
	for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
		switch f {
		case "resourceProfiles", "resource_profiles":
			for iter.ReadArray() {
				orig.ResourceProfiles = append(orig.ResourceProfiles, NewResourceProfiles())
				orig.ResourceProfiles[len(orig.ResourceProfiles)-1].UnmarshalJSON(iter)
			}

		case "dictionary":

			orig.Dictionary.UnmarshalJSON(iter)
		default:
			iter.HandleUnknownField(f)
		}
	}
}

func (orig *ExportProfilesServiceRequest) SizeProto() int {
	var n int
	var l int
	_ = l
	for i := range orig.ResourceProfiles {
		l = orig.ResourceProfiles[i].SizeProto()
		n += 1 + proto.Sov(uint64(l)) + l
	}
	l = orig.Dictionary.SizeProto()
	n += 1 + proto.Sov(uint64(l)) + l
	return n
}

func (orig *ExportProfilesServiceRequest) MarshalProto(buf []byte) int {
	pos := len(buf)
	var l int
	_ = l
	for i := len(orig.ResourceProfiles) - 1; i >= 0; i-- {
		l = orig.ResourceProfiles[i].MarshalProto(buf[:pos])
		pos -= l
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0xa
	}
	l = orig.Dictionary.MarshalProto(buf[:pos])
	pos -= l
	pos = proto.EncodeVarint(buf, pos, uint64(l))
	pos--
	buf[pos] = 0x12

	return len(buf) - pos
}

func (orig *ExportProfilesServiceRequest) UnmarshalProto(buf []byte) error {
	var err error
	var fieldNum int32
	var wireType proto.WireType

	l := len(buf)
	pos := 0
	for pos < l {
		// If in a group parsing, move to the next tag.
		fieldNum, wireType, pos, err = proto.ConsumeTag(buf, pos)
		if err != nil {
			return err
		}
		switch fieldNum {

		case 1:
			if wireType != proto.WireTypeLen {
				return fmt.Errorf("proto: wrong wireType = %d for field ResourceProfiles", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length
			orig.ResourceProfiles = append(orig.ResourceProfiles, NewResourceProfiles())
			err = orig.ResourceProfiles[len(orig.ResourceProfiles)-1].UnmarshalProto(buf[startPos:pos])
			if err != nil {
				return err
			}

		case 2:
			if wireType != proto.WireTypeLen {
				return fmt.Errorf("proto: wrong wireType = %d for field Dictionary", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length

			err = orig.Dictionary.UnmarshalProto(buf[startPos:pos])
			if err != nil {
				return err
			}
		default:
			pos, err = proto.ConsumeUnknown(buf, pos, wireType)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func GenTestExportProfilesServiceRequest() *ExportProfilesServiceRequest {
	orig := NewExportProfilesServiceRequest()
	orig.ResourceProfiles = []*ResourceProfiles{{}, GenTestResourceProfiles()}
	orig.Dictionary = *GenTestProfilesDictionary()
	return orig
}

func GenTestExportProfilesServiceRequestPtrSlice() []*ExportProfilesServiceRequest {
	orig := make([]*ExportProfilesServiceRequest, 5)
	orig[0] = NewExportProfilesServiceRequest()
	orig[1] = GenTestExportProfilesServiceRequest()
	orig[2] = NewExportProfilesServiceRequest()
	orig[3] = GenTestExportProfilesServiceRequest()
	orig[4] = NewExportProfilesServiceRequest()
	return orig
}

func GenTestExportProfilesServiceRequestSlice() []ExportProfilesServiceRequest {
	orig := make([]ExportProfilesServiceRequest, 5)
	orig[1] = *GenTestExportProfilesServiceRequest()
	orig[3] = *GenTestExportProfilesServiceRequest()
	return orig
}
