// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package trace

import (
	
	
)

// timestamp is an unprocessed timestamp.
type timestamp uint64

type batch struct {
	time timestamp
	gen  uint64
	data []byte
}

// readBatch copies b and parses the trace batch header inside.
// Returns the batch, bytes read, and an error.
func readBatch( []byte) (batch, uint64, error) {
	if len() == 0 {
		return batch{}, 0, fmt.Errorf("batch is empty")
	}
	 := make([]byte, len())
	copy(, )

	// Read batch header byte.
	if  := tracev2.EventType([0]);  == tracev2.EvEndOfGeneration {
		if len() != 1 {
			return batch{}, 1, fmt.Errorf("unexpected end of generation in batch of size >1")
		}
		return batch{data: }, 1, nil
	}
	if  := tracev2.EventType([0]);  != tracev2.EvEventBatch &&  != tracev2.EvExperimentalBatch {
		return batch{}, 1, fmt.Errorf("expected batch event, got event %d", )
	}
	 := 1
	 = [1:]

	// Read the generation
	, ,  := readUvarint()
	if  != nil {
		return batch{}, uint64( + ), fmt.Errorf("error reading batch gen: %w", )
	}
	 += 
	 = [:]

	// Read the M (discard it).
	_, ,  = readUvarint()
	if  != nil {
		return batch{}, uint64( + ), fmt.Errorf("error reading batch M ID: %w", )
	}
	 += 
	 = [:]

	// Read the timestamp.
	, ,  := readUvarint()
	if  != nil {
		return batch{}, uint64( + ), fmt.Errorf("error reading batch timestamp: %w", )
	}
	 += 
	 = [:]

	// Read the size of the batch to follow.
	, ,  := readUvarint()
	if  != nil {
		return batch{}, uint64( + ), fmt.Errorf("error reading batch size: %w", )
	}
	if  > tracev2.MaxBatchSize {
		return batch{}, uint64( + ), fmt.Errorf("invalid batch size %d, maximum is %d", , tracev2.MaxBatchSize)
	}
	 += 
	 += int()
	if  != len() {
		return batch{}, uint64(), fmt.Errorf("expected complete batch")
	}
	 = [:]

	// Return the batch.
	return batch{
		gen:  ,
		time: timestamp(),
		data: ,
	}, uint64(), nil
}