// 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"

	otlpmetrics "go.opentelemetry.io/collector/pdata/internal/data/protogen/metrics/v1"
	"go.opentelemetry.io/collector/pdata/internal/json"
	"go.opentelemetry.io/collector/pdata/internal/proto"
)

var (
	protoPoolSum = sync.Pool{
		New: func() any {
			return &otlpmetrics.Sum{}
		},
	}
)

func NewOrigSum() *otlpmetrics.Sum {
	if !UseProtoPooling.IsEnabled() {
		return &otlpmetrics.Sum{}
	}
	return protoPoolSum.Get().(*otlpmetrics.Sum)
}

func DeleteOrigSum(orig *otlpmetrics.Sum, nullable bool) {
	if orig == nil {
		return
	}

	if !UseProtoPooling.IsEnabled() {
		orig.Reset()
		return
	}

	for i := range orig.DataPoints {
		DeleteOrigNumberDataPoint(orig.DataPoints[i], true)
	}

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

func CopyOrigSum(dest, src *otlpmetrics.Sum) {
	// If copying to same object, just return.
	if src == dest {
		return
	}
	dest.DataPoints = CopyOrigNumberDataPointSlice(dest.DataPoints, src.DataPoints)
	dest.AggregationTemporality = src.AggregationTemporality
	dest.IsMonotonic = src.IsMonotonic
}

func GenTestOrigSum() *otlpmetrics.Sum {
	orig := NewOrigSum()
	orig.DataPoints = GenerateOrigTestNumberDataPointSlice()
	orig.AggregationTemporality = otlpmetrics.AggregationTemporality(1)
	orig.IsMonotonic = true
	return orig
}

// MarshalJSONOrig marshals all properties from the current struct to the destination stream.
func MarshalJSONOrigSum(orig *otlpmetrics.Sum, dest *json.Stream) {
	dest.WriteObjectStart()
	if len(orig.DataPoints) > 0 {
		dest.WriteObjectField("dataPoints")
		dest.WriteArrayStart()
		MarshalJSONOrigNumberDataPoint(orig.DataPoints[0], dest)
		for i := 1; i < len(orig.DataPoints); i++ {
			dest.WriteMore()
			MarshalJSONOrigNumberDataPoint(orig.DataPoints[i], dest)
		}
		dest.WriteArrayEnd()
	}

	if int32(orig.AggregationTemporality) != 0 {
		dest.WriteObjectField("aggregationTemporality")
		dest.WriteInt32(int32(orig.AggregationTemporality))
	}
	if orig.IsMonotonic != false {
		dest.WriteObjectField("isMonotonic")
		dest.WriteBool(orig.IsMonotonic)
	}
	dest.WriteObjectEnd()
}

// UnmarshalJSONOrigSum unmarshals all properties from the current struct from the source iterator.
func UnmarshalJSONOrigSum(orig *otlpmetrics.Sum, iter *json.Iterator) {
	for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
		switch f {
		case "dataPoints", "data_points":
			for iter.ReadArray() {
				orig.DataPoints = append(orig.DataPoints, NewOrigNumberDataPoint())
				UnmarshalJSONOrigNumberDataPoint(orig.DataPoints[len(orig.DataPoints)-1], iter)
			}

		case "aggregationTemporality", "aggregation_temporality":
			orig.AggregationTemporality = otlpmetrics.AggregationTemporality(iter.ReadEnumValue(otlpmetrics.AggregationTemporality_value))
		case "isMonotonic", "is_monotonic":
			orig.IsMonotonic = iter.ReadBool()
		default:
			iter.Skip()
		}
	}
}

func SizeProtoOrigSum(orig *otlpmetrics.Sum) int {
	var n int
	var l int
	_ = l
	for i := range orig.DataPoints {
		l = SizeProtoOrigNumberDataPoint(orig.DataPoints[i])
		n += 1 + proto.Sov(uint64(l)) + l
	}
	if orig.AggregationTemporality != 0 {
		n += 1 + proto.Sov(uint64(orig.AggregationTemporality))
	}
	if orig.IsMonotonic {
		n += 2
	}
	return n
}

func MarshalProtoOrigSum(orig *otlpmetrics.Sum, buf []byte) int {
	pos := len(buf)
	var l int
	_ = l
	for i := len(orig.DataPoints) - 1; i >= 0; i-- {
		l = MarshalProtoOrigNumberDataPoint(orig.DataPoints[i], buf[:pos])
		pos -= l
		pos = proto.EncodeVarint(buf, pos, uint64(l))
		pos--
		buf[pos] = 0xa
	}
	if orig.AggregationTemporality != 0 {
		pos = proto.EncodeVarint(buf, pos, uint64(orig.AggregationTemporality))
		pos--
		buf[pos] = 0x10
	}
	if orig.IsMonotonic {
		pos--
		if orig.IsMonotonic {
			buf[pos] = 1
		} else {
			buf[pos] = 0
		}
		pos--
		buf[pos] = 0x18
	}
	return len(buf) - pos
}

func UnmarshalProtoOrigSum(orig *otlpmetrics.Sum, 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 DataPoints", wireType)
			}
			var length int
			length, pos, err = proto.ConsumeLen(buf, pos)
			if err != nil {
				return err
			}
			startPos := pos - length
			orig.DataPoints = append(orig.DataPoints, NewOrigNumberDataPoint())
			err = UnmarshalProtoOrigNumberDataPoint(orig.DataPoints[len(orig.DataPoints)-1], buf[startPos:pos])
			if err != nil {
				return err
			}

		case 2:
			if wireType != proto.WireTypeVarint {
				return fmt.Errorf("proto: wrong wireType = %d for field AggregationTemporality", wireType)
			}
			var num uint64
			num, pos, err = proto.ConsumeVarint(buf, pos)
			if err != nil {
				return err
			}

			orig.AggregationTemporality = otlpmetrics.AggregationTemporality(num)

		case 3:
			if wireType != proto.WireTypeVarint {
				return fmt.Errorf("proto: wrong wireType = %d for field IsMonotonic", wireType)
			}
			var num uint64
			num, pos, err = proto.ConsumeVarint(buf, pos)
			if err != nil {
				return err
			}

			orig.IsMonotonic = num != 0
		default:
			pos, err = proto.ConsumeUnknown(buf, pos, wireType)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
