// 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 (
	"encoding/binary"
	"fmt"
	"sync"

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

// Sample represents each record value encountered within a profiled program.
type Sample struct {
	AttributeIndices   []int32
	Values             []int64
	TimestampsUnixNano []uint64
	StackIndex         int32
	LinkIndex          int32
}

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

func NewSample() *Sample {
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		return &Sample{}
	}
	return protoPoolSample.Get().(*Sample)
}

func DeleteSample(orig *Sample, nullable bool) {
	if orig == nil {
		return
	}

	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		orig.Reset()
		return
	}

	orig.Reset()
	if nullable {
		protoPoolSample.Put(orig)
	}
}

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

	if src == nil {
		return nil
	}

	if dest == nil {
		dest = NewSample()
	}
	dest.StackIndex = src.StackIndex
	dest.AttributeIndices = append(dest.AttributeIndices[:0], src.AttributeIndices...)

	dest.LinkIndex = src.LinkIndex
	dest.Values = append(dest.Values[:0], src.Values...)

	dest.TimestampsUnixNano = append(dest.TimestampsUnixNano[:0], src.TimestampsUnixNano...)

	return dest
}

func CopySampleSlice(dest, src []Sample) []Sample {
	var newDest []Sample
	if cap(dest) < len(src) {
		newDest = make([]Sample, 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++ {
			DeleteSample(&dest[i], false)
		}
	}
	for i := range src {
		CopySample(&newDest[i], &src[i])
	}
	return newDest
}

func CopySamplePtrSlice(dest, src []*Sample) []*Sample {
	var newDest []*Sample
	if cap(dest) < len(src) {
		newDest = make([]*Sample, 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] = NewSample()
		}
	} 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++ {
			DeleteSample(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] = NewSample()
		}
	}
	for i := range src {
		CopySample(newDest[i], src[i])
	}
	return newDest
}

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

// MarshalJSON marshals all properties from the current struct to the destination stream.
func (orig *Sample) MarshalJSON(dest *json.Stream) {
	dest.WriteObjectStart()
	if orig.StackIndex != int32(0) {
		dest.WriteObjectField("stackIndex")
		dest.WriteInt32(orig.StackIndex)
	}
	if len(orig.AttributeIndices) > 0 {
		dest.WriteObjectField("attributeIndices")
		dest.WriteArrayStart()
		dest.WriteInt32(orig.AttributeIndices[0])
		for i := 1; i < len(orig.AttributeIndices); i++ {
			dest.WriteMore()
			dest.WriteInt32(orig.AttributeIndices[i])
		}
		dest.WriteArrayEnd()
	}

	if orig.LinkIndex != int32(0) {
		dest.WriteObjectField("linkIndex")
		dest.WriteInt32(orig.LinkIndex)
	}
	if len(orig.Values) > 0 {
		dest.WriteObjectField("values")
		dest.WriteArrayStart()
		dest.WriteInt64(orig.Values[0])
		for i := 1; i < len(orig.Values); i++ {
			dest.WriteMore()
			dest.WriteInt64(orig.Values[i])
		}
		dest.WriteArrayEnd()
	}

	if len(orig.TimestampsUnixNano) > 0 {
		dest.WriteObjectField("timestampsUnixNano")
		dest.WriteArrayStart()
		dest.WriteUint64(orig.TimestampsUnixNano[0])
		for i := 1; i < len(orig.TimestampsUnixNano); i++ {
			dest.WriteMore()
			dest.WriteUint64(orig.TimestampsUnixNano[i])
		}
		dest.WriteArrayEnd()
	}

	dest.WriteObjectEnd()
}

// UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
func (orig *Sample) UnmarshalJSON(iter *json.Iterator) {
	for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
		switch f {
		case "stackIndex", "stack_index":
			orig.StackIndex = iter.ReadInt32()
		case "attributeIndices", "attribute_indices":
			for iter.ReadArray() {
				orig.AttributeIndices = append(orig.AttributeIndices, iter.ReadInt32())
			}

		case "linkIndex", "link_index":
			orig.LinkIndex = iter.ReadInt32()
		case "values":
			for iter.ReadArray() {
				orig.Values = append(orig.Values, iter.ReadInt64())
			}

		case "timestampsUnixNano", "timestamps_unix_nano":
			for iter.ReadArray() {
				orig.TimestampsUnixNano = append(orig.TimestampsUnixNano, iter.ReadUint64())
			}

		default:
			iter.HandleUnknownField(f)
		}
	}
}

func (orig *Sample) SizeProto() int {
	var n int
	var l int
	_ = l
	if orig.StackIndex != int32(0) {
		n += 1 + proto.Sov(uint64(orig.StackIndex))
	}

	if len(orig.AttributeIndices) > 0 {
		l = 0
		for _, e := range orig.AttributeIndices {
			l += proto.Sov(uint64(e))
		}
		n += 1 + proto.Sov(uint64(l)) + l
	}
	if orig.LinkIndex != int32(0) {
		n += 1 + proto.Sov(uint64(orig.LinkIndex))
	}

	if len(orig.Values) > 0 {
		l = 0
		for _, e := range orig.Values {
			l += proto.Sov(uint64(e))
		}
		n += 1 + proto.Sov(uint64(l)) + l
	}
	l = len(orig.TimestampsUnixNano)
	if l > 0 {
		l *= 8
		n += 1 + proto.Sov(uint64(l)) + l
	}
	return n
}

func (orig *Sample) MarshalProto(buf []byte) int {
	pos := len(buf)
	var l int
	_ = l
	if orig.StackIndex != int32(0) {
		pos = proto.EncodeVarint(buf, pos, uint64(orig.StackIndex))
		pos--
		buf[pos] = 0x8
	}
	l = len(orig.AttributeIndices)
	if l > 0 {
		endPos := pos
		for i := l - 1; i >= 0; i-- {
			pos = proto.EncodeVarint(buf, pos, uint64(orig.AttributeIndices[i]))
		}
		pos = proto.EncodeVarint(buf, pos, uint64(endPos-pos))
		pos--
		buf[pos] = 0x12
	}
	if orig.LinkIndex != int32(0) {
		pos = proto.EncodeVarint(buf, pos, uint64(orig.LinkIndex))
		pos--
		buf[pos] = 0x18
	}
	l = len(orig.Values)
	if l > 0 {
		endPos := pos
		for i := l - 1; i >= 0; i-- {
			pos = proto.EncodeVarint(buf, pos, uint64(orig.Values[i]))
		}
		pos = proto.EncodeVarint(buf, pos, uint64(endPos-pos))
		pos--
		buf[pos] = 0x22
	}
	l = len(orig.TimestampsUnixNano)
	if l > 0 {
		for i := l - 1; i >= 0; i-- {
			pos -= 8
			binary.LittleEndian.PutUint64(buf[pos:], uint64(orig.TimestampsUnixNano[i]))
		}
		pos = proto.EncodeVarint(buf, pos, uint64(l*8))
		pos--
		buf[pos] = 0x2a
	}
	return len(buf) - pos
}

func (orig *Sample) 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.WireTypeVarint {
				return fmt.Errorf("proto: wrong wireType = %d for field StackIndex", wireType)
			}
			var num uint64
			num, pos, err = proto.ConsumeVarint(buf, pos)
			if err != nil {
				return err
			}
			orig.StackIndex = int32(num)
		case 2:
			switch wireType {
			case proto.WireTypeLen:
				var length int
				length, pos, err = proto.ConsumeLen(buf, pos)
				if err != nil {
					return err
				}
				startPos := pos - length
				var num uint64
				for startPos < pos {
					num, startPos, err = proto.ConsumeVarint(buf[:pos], startPos)
					if err != nil {
						return err
					}
					orig.AttributeIndices = append(orig.AttributeIndices, int32(num))
				}
				if startPos != pos {
					return fmt.Errorf("proto: invalid field len = %d for field AttributeIndices", pos-startPos)
				}
			case proto.WireTypeVarint:
				var num uint64
				num, pos, err = proto.ConsumeVarint(buf, pos)
				if err != nil {
					return err
				}
				orig.AttributeIndices = append(orig.AttributeIndices, int32(num))
			default:
				return fmt.Errorf("proto: wrong wireType = %d for field AttributeIndices", wireType)
			}

		case 3:
			if wireType != proto.WireTypeVarint {
				return fmt.Errorf("proto: wrong wireType = %d for field LinkIndex", wireType)
			}
			var num uint64
			num, pos, err = proto.ConsumeVarint(buf, pos)
			if err != nil {
				return err
			}
			orig.LinkIndex = int32(num)
		case 4:
			switch wireType {
			case proto.WireTypeLen:
				var length int
				length, pos, err = proto.ConsumeLen(buf, pos)
				if err != nil {
					return err
				}
				startPos := pos - length
				var num uint64
				for startPos < pos {
					num, startPos, err = proto.ConsumeVarint(buf[:pos], startPos)
					if err != nil {
						return err
					}
					orig.Values = append(orig.Values, int64(num))
				}
				if startPos != pos {
					return fmt.Errorf("proto: invalid field len = %d for field Values", pos-startPos)
				}
			case proto.WireTypeVarint:
				var num uint64
				num, pos, err = proto.ConsumeVarint(buf, pos)
				if err != nil {
					return err
				}
				orig.Values = append(orig.Values, int64(num))
			default:
				return fmt.Errorf("proto: wrong wireType = %d for field Values", wireType)
			}
		case 5:
			switch wireType {
			case proto.WireTypeLen:
				var length int
				length, pos, err = proto.ConsumeLen(buf, pos)
				if err != nil {
					return err
				}
				startPos := pos - length
				size := length / 8
				orig.TimestampsUnixNano = make([]uint64, size)
				var num uint64
				for i := 0; i < size; i++ {
					num, startPos, err = proto.ConsumeI64(buf[:pos], startPos)
					if err != nil {
						return err
					}
					orig.TimestampsUnixNano[i] = uint64(num)
				}
				if startPos != pos {
					return fmt.Errorf("proto: invalid field len = %d for field TimestampsUnixNano", pos-startPos)
				}
			case proto.WireTypeI64:
				var num uint64
				num, pos, err = proto.ConsumeI64(buf, pos)
				if err != nil {
					return err
				}
				orig.TimestampsUnixNano = append(orig.TimestampsUnixNano, uint64(num))
			default:
				return fmt.Errorf("proto: wrong wireType = %d for field TimestampsUnixNano", wireType)
			}
		default:
			pos, err = proto.ConsumeUnknown(buf, pos, wireType)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

func GenTestSample() *Sample {
	orig := NewSample()
	orig.StackIndex = int32(13)
	orig.AttributeIndices = []int32{int32(0), int32(13)}
	orig.LinkIndex = int32(13)
	orig.Values = []int64{int64(0), int64(13)}
	orig.TimestampsUnixNano = []uint64{uint64(0), uint64(13)}
	return orig
}

func GenTestSamplePtrSlice() []*Sample {
	orig := make([]*Sample, 5)
	orig[0] = NewSample()
	orig[1] = GenTestSample()
	orig[2] = NewSample()
	orig[3] = GenTestSample()
	orig[4] = NewSample()
	return orig
}

func GenTestSampleSlice() []Sample {
	orig := make([]Sample, 5)
	orig[1] = *GenTestSample()
	orig[3] = *GenTestSample()
	return orig
}
