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

// Gauge represents the type of a numeric metric that always exports the "current value" for every data point.
type Gauge struct {
	DataPoints []*NumberDataPoint
}

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

func NewGauge() *Gauge {
	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		return &Gauge{}
	}
	return protoPoolGauge.Get().(*Gauge)
}

func DeleteGauge(orig *Gauge, nullable bool) {
	if orig == nil {
		return
	}

	if !metadata.PdataUseProtoPoolingFeatureGate.IsEnabled() {
		orig.Reset()
		return
	}
	for i := range orig.DataPoints {
		DeleteNumberDataPoint(orig.DataPoints[i], true)
	}
	orig.Reset()
	if nullable {
		protoPoolGauge.Put(orig)
	}
}

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

	if src == nil {
		return nil
	}

	if dest == nil {
		dest = NewGauge()
	}
	dest.DataPoints = CopyNumberDataPointPtrSlice(dest.DataPoints, src.DataPoints)

	return dest
}

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

func CopyGaugePtrSlice(dest, src []*Gauge) []*Gauge {
	var newDest []*Gauge
	if cap(dest) < len(src) {
		newDest = make([]*Gauge, 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] = NewGauge()
		}
	} 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++ {
			DeleteGauge(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] = NewGauge()
		}
	}
	for i := range src {
		CopyGauge(newDest[i], src[i])
	}
	return newDest
}

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

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

// UnmarshalJSON unmarshals all properties from the current struct from the source iterator.
func (orig *Gauge) UnmarshalJSON(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, NewNumberDataPoint())
				orig.DataPoints[len(orig.DataPoints)-1].UnmarshalJSON(iter)
			}

		default:
			iter.Skip()
		}
	}
}

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

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

func (orig *Gauge) 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 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, NewNumberDataPoint())
			err = orig.DataPoints[len(orig.DataPoints)-1].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 GenTestGauge() *Gauge {
	orig := NewGauge()
	orig.DataPoints = []*NumberDataPoint{{}, GenTestNumberDataPoint()}
	return orig
}

func GenTestGaugePtrSlice() []*Gauge {
	orig := make([]*Gauge, 5)
	orig[0] = NewGauge()
	orig[1] = GenTestGauge()
	orig[2] = NewGauge()
	orig[3] = GenTestGauge()
	orig[4] = NewGauge()
	return orig
}

func GenTestGaugeSlice() []Gauge {
	orig := make([]Gauge, 5)
	orig[1] = *GenTestGauge()
	orig[3] = *GenTestGauge()
	return orig
}
