package qlogwriter

import (
	
	

	
	
)

type ConnectionID = protocol.ConnectionID

// Setting of this only works when quic-go is used as a library.
// When building a binary from this repository, the version can be set using the following go build flag:
// -ldflags="-X github.com/quic-go/quic-go/qlogwriter.quicGoVersion=foobar"
var quicGoVersion = "(devel)"

func init() {
	if quicGoVersion != "(devel)" { // variable set by ldflags
		return
	}
	,  := debug.ReadBuildInfo()
	if ! { // no build info available. This happens when quic-go is not used as a library.
		return
	}
	for ,  := range .Deps {
		if .Path == "github.com/tmc/go-iroh/internal/qng" {
			quicGoVersion = .Version
			if .Replace != nil {
				if len(.Replace.Version) > 0 {
					quicGoVersion = .Version
				} else {
					quicGoVersion += " (replaced)"
				}
			}
			break
		}
	}
}

type encoderHelper struct {
	enc *jsontext.Encoder
	err error
}

func ( *encoderHelper) ( jsontext.Token) {
	if .err != nil {
		return
	}
	.err = .enc.WriteToken()
}

type traceHeader struct {
	VantagePointType string
	GroupID          *ConnectionID
	ReferenceTime    time.Time
	EventSchemas     []string
}

func ( traceHeader) ( *jsontext.Encoder) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("file_schema"))
	.WriteToken(jsontext.String("urn:ietf:params:qlog:file:sequential"))
	.WriteToken(jsontext.String("serialization_format"))
	.WriteToken(jsontext.String("application/qlog+json-seq"))
	.WriteToken(jsontext.String("title"))
	.WriteToken(jsontext.String("quic-go qlog"))
	.WriteToken(jsontext.String("code_version"))
	.WriteToken(jsontext.String(quicGoVersion))

	.WriteToken(jsontext.String("trace"))
	// trace
	.WriteToken(jsontext.BeginObject)
	if len(.EventSchemas) > 0 {
		.WriteToken(jsontext.String("event_schemas"))
		.WriteToken(jsontext.BeginArray)
		for ,  := range .EventSchemas {
			.WriteToken(jsontext.String())
		}
		.WriteToken(jsontext.EndArray)
	}

	.WriteToken(jsontext.String("vantage_point"))
	// -- vantage_point
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("type"))
	.WriteToken(jsontext.String(.VantagePointType))
	// -- end vantage_point
	.WriteToken(jsontext.EndObject)

	.WriteToken(jsontext.String("common_fields"))
	// -- common_fields
	.WriteToken(jsontext.BeginObject)
	if .GroupID != nil {
		.WriteToken(jsontext.String("group_id"))
		.WriteToken(jsontext.String(.GroupID.String()))
	}
	.WriteToken(jsontext.String("reference_time"))
	// ---- reference_time
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("clock_type"))
	.WriteToken(jsontext.String("monotonic"))
	.WriteToken(jsontext.String("epoch"))
	.WriteToken(jsontext.String("unknown"))
	.WriteToken(jsontext.String("wall_clock_time"))
	.WriteToken(jsontext.String(.ReferenceTime.Format(time.RFC3339Nano)))
	// ---- end reference_time
	.WriteToken(jsontext.EndObject)
	// -- end common_fields
	.WriteToken(jsontext.EndObject)
	// end trace
	.WriteToken(jsontext.EndObject)

	// The following fields are not required by the qlog draft anymore,
	// but qvis still requires them to be present.
	.WriteToken(jsontext.String("qlog_format"))
	.WriteToken(jsontext.String("JSON-SEQ"))
	.WriteToken(jsontext.String("qlog_version"))
	.WriteToken(jsontext.String("0.3"))

	.WriteToken(jsontext.EndObject)
	return .err
}