package qlog

import (
	

	
	
)

type Frame struct {
	Frame any
}

type frames []Frame

type (
	// An AckFrame is an ACK frame.
	AckFrame = wire.AckFrame
	// A ConnectionCloseFrame is a CONNECTION_CLOSE frame.
	ConnectionCloseFrame = wire.ConnectionCloseFrame
	// A DataBlockedFrame is a DATA_BLOCKED frame.
	DataBlockedFrame = wire.DataBlockedFrame
	// A HandshakeDoneFrame is a HANDSHAKE_DONE frame.
	HandshakeDoneFrame = wire.HandshakeDoneFrame
	// A MaxDataFrame is a MAX_DATA frame.
	MaxDataFrame = wire.MaxDataFrame
	// A MaxStreamDataFrame is a MAX_STREAM_DATA frame.
	MaxStreamDataFrame = wire.MaxStreamDataFrame
	// A MaxStreamsFrame is a MAX_STREAMS_FRAME.
	MaxStreamsFrame = wire.MaxStreamsFrame
	// A NewConnectionIDFrame is a NEW_CONNECTION_ID frame.
	NewConnectionIDFrame = wire.NewConnectionIDFrame
	// A NewTokenFrame is a NEW_TOKEN frame.
	NewTokenFrame = wire.NewTokenFrame
	// A PathChallengeFrame is a PATH_CHALLENGE frame.
	PathChallengeFrame = wire.PathChallengeFrame
	// A PathResponseFrame is a PATH_RESPONSE frame.
	PathResponseFrame = wire.PathResponseFrame
	// A PingFrame is a PING frame.
	PingFrame = wire.PingFrame
	// A ResetStreamFrame is a RESET_STREAM frame.
	ResetStreamFrame = wire.ResetStreamFrame
	// A RetireConnectionIDFrame is a RETIRE_CONNECTION_ID frame.
	RetireConnectionIDFrame = wire.RetireConnectionIDFrame
	// A StopSendingFrame is a STOP_SENDING frame.
	StopSendingFrame = wire.StopSendingFrame
	// A StreamsBlockedFrame is a STREAMS_BLOCKED frame.
	StreamsBlockedFrame = wire.StreamsBlockedFrame
	// A StreamDataBlockedFrame is a STREAM_DATA_BLOCKED frame.
	StreamDataBlockedFrame = wire.StreamDataBlockedFrame
	// An AckFrequencyFrame is an ACK_FREQUENCY frame.
	AckFrequencyFrame = wire.AckFrequencyFrame
	// An ImmediateAckFrame is an IMMEDIATE_ACK frame.
	ImmediateAckFrame = wire.ImmediateAckFrame
)

type AckRange = wire.AckRange

// A CryptoFrame is a CRYPTO frame.
type CryptoFrame struct {
	Offset int64
	Length int64
}

// A StreamFrame is a STREAM frame.
type StreamFrame struct {
	StreamID StreamID
	Offset   int64
	Length   int64
	Fin      bool
}

// A DatagramFrame is a DATAGRAM frame.
type DatagramFrame struct {
	Length int64
}

func ( frames) ( *jsontext.Encoder) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginArray)
	for ,  := range  {
		if  := .Encode();  != nil {
			return 
		}
	}
	.WriteToken(jsontext.EndArray)
	return .err
}

func ( Frame) ( *jsontext.Encoder) error {
	switch frame := .Frame.(type) {
	case *PingFrame:
		return encodePingFrame(, )
	case *AckFrame:
		return encodeAckFrame(, )
	case *ResetStreamFrame:
		return encodeResetStreamFrame(, )
	case *StopSendingFrame:
		return encodeStopSendingFrame(, )
	case *CryptoFrame:
		return encodeCryptoFrame(, )
	case *NewTokenFrame:
		return encodeNewTokenFrame(, )
	case *StreamFrame:
		return encodeStreamFrame(, )
	case *MaxDataFrame:
		return encodeMaxDataFrame(, )
	case *MaxStreamDataFrame:
		return encodeMaxStreamDataFrame(, )
	case *MaxStreamsFrame:
		return encodeMaxStreamsFrame(, )
	case *DataBlockedFrame:
		return encodeDataBlockedFrame(, )
	case *StreamDataBlockedFrame:
		return encodeStreamDataBlockedFrame(, )
	case *StreamsBlockedFrame:
		return encodeStreamsBlockedFrame(, )
	case *NewConnectionIDFrame:
		return encodeNewConnectionIDFrame(, )
	case *RetireConnectionIDFrame:
		return encodeRetireConnectionIDFrame(, )
	case *PathChallengeFrame:
		return encodePathChallengeFrame(, )
	case *PathResponseFrame:
		return encodePathResponseFrame(, )
	case *ConnectionCloseFrame:
		return encodeConnectionCloseFrame(, )
	case *HandshakeDoneFrame:
		return encodeHandshakeDoneFrame(, )
	case *DatagramFrame:
		return encodeDatagramFrame(, )
	case *AckFrequencyFrame:
		return encodeAckFrequencyFrame(, )
	case *ImmediateAckFrame:
		return encodeImmediateAckFrame(, )
	default:
		panic("unknown frame type")
	}
}

func encodePingFrame( *jsontext.Encoder,  *PingFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("ping"))
	.WriteToken(jsontext.EndObject)
	return .err
}

type ackRanges []wire.AckRange

func ( ackRanges) ( *jsontext.Encoder) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginArray)
	for ,  := range  {
		if  := ackRange().encode();  != nil {
			return 
		}
	}
	.WriteToken(jsontext.EndArray)
	return .err
}

type ackRange wire.AckRange

func ( ackRange) ( *jsontext.Encoder) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginArray)
	.WriteToken(jsontext.Int(int64(.Smallest)))
	if .Smallest != .Largest {
		.WriteToken(jsontext.Int(int64(.Largest)))
	}
	.WriteToken(jsontext.EndArray)
	return .err
}

func encodeAckFrame( *jsontext.Encoder,  *AckFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("ack"))
	if .DelayTime > 0 {
		.WriteToken(jsontext.String("ack_delay"))
		.WriteToken(jsontext.Float(milliseconds(.DelayTime)))
	}
	.WriteToken(jsontext.String("acked_ranges"))
	if  := ackRanges(.AckRanges).encode();  != nil {
		return 
	}
	 := .ECT0 > 0 || .ECT1 > 0 || .ECNCE > 0
	if  {
		.WriteToken(jsontext.String("ect0"))
		.WriteToken(jsontext.Uint(.ECT0))
		.WriteToken(jsontext.String("ect1"))
		.WriteToken(jsontext.Uint(.ECT1))
		.WriteToken(jsontext.String("ce"))
		.WriteToken(jsontext.Uint(.ECNCE))
	}
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeResetStreamFrame( *jsontext.Encoder,  *ResetStreamFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	if .ReliableSize > 0 {
		.WriteToken(jsontext.String("reset_stream_at"))
	} else {
		.WriteToken(jsontext.String("reset_stream"))
	}
	.WriteToken(jsontext.String("stream_id"))
	.WriteToken(jsontext.Int(int64(.StreamID)))
	.WriteToken(jsontext.String("error_code"))
	.WriteToken(jsontext.Int(int64(.ErrorCode)))
	.WriteToken(jsontext.String("final_size"))
	.WriteToken(jsontext.Int(int64(.FinalSize)))
	if .ReliableSize > 0 {
		.WriteToken(jsontext.String("reliable_size"))
		.WriteToken(jsontext.Int(int64(.ReliableSize)))
	}
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeStopSendingFrame( *jsontext.Encoder,  *StopSendingFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("stop_sending"))
	.WriteToken(jsontext.String("stream_id"))
	.WriteToken(jsontext.Int(int64(.StreamID)))
	.WriteToken(jsontext.String("error_code"))
	.WriteToken(jsontext.Int(int64(.ErrorCode)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeCryptoFrame( *jsontext.Encoder,  *CryptoFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("crypto"))
	.WriteToken(jsontext.String("offset"))
	.WriteToken(jsontext.Int(.Offset))
	.WriteToken(jsontext.String("length"))
	.WriteToken(jsontext.Int(.Length))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeNewTokenFrame( *jsontext.Encoder,  *NewTokenFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("new_token"))
	.WriteToken(jsontext.String("token"))
	if  := (Token{Raw: .Token}).encode();  != nil {
		return 
	}
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeStreamFrame( *jsontext.Encoder,  *StreamFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("stream"))
	.WriteToken(jsontext.String("stream_id"))
	.WriteToken(jsontext.Int(int64(.StreamID)))
	.WriteToken(jsontext.String("offset"))
	.WriteToken(jsontext.Int(.Offset))
	.WriteToken(jsontext.String("length"))
	.WriteToken(jsontext.Int(.Length))
	if .Fin {
		.WriteToken(jsontext.String("fin"))
		.WriteToken(jsontext.True)
	}
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeMaxDataFrame( *jsontext.Encoder,  *MaxDataFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("max_data"))
	.WriteToken(jsontext.String("maximum"))
	.WriteToken(jsontext.Int(int64(.MaximumData)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeMaxStreamDataFrame( *jsontext.Encoder,  *MaxStreamDataFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("max_stream_data"))
	.WriteToken(jsontext.String("stream_id"))
	.WriteToken(jsontext.Int(int64(.StreamID)))
	.WriteToken(jsontext.String("maximum"))
	.WriteToken(jsontext.Int(int64(.MaximumStreamData)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeMaxStreamsFrame( *jsontext.Encoder,  *MaxStreamsFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("max_streams"))
	.WriteToken(jsontext.String("stream_type"))
	.WriteToken(jsontext.String(streamType(.Type).String()))
	.WriteToken(jsontext.String("maximum"))
	.WriteToken(jsontext.Int(int64(.MaxStreamNum)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeDataBlockedFrame( *jsontext.Encoder,  *DataBlockedFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("data_blocked"))
	.WriteToken(jsontext.String("limit"))
	.WriteToken(jsontext.Int(int64(.MaximumData)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeStreamDataBlockedFrame( *jsontext.Encoder,  *StreamDataBlockedFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("stream_data_blocked"))
	.WriteToken(jsontext.String("stream_id"))
	.WriteToken(jsontext.Int(int64(.StreamID)))
	.WriteToken(jsontext.String("limit"))
	.WriteToken(jsontext.Int(int64(.MaximumStreamData)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeStreamsBlockedFrame( *jsontext.Encoder,  *StreamsBlockedFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("streams_blocked"))
	.WriteToken(jsontext.String("stream_type"))
	.WriteToken(jsontext.String(streamType(.Type).String()))
	.WriteToken(jsontext.String("limit"))
	.WriteToken(jsontext.Int(int64(.StreamLimit)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeNewConnectionIDFrame( *jsontext.Encoder,  *NewConnectionIDFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("new_connection_id"))
	.WriteToken(jsontext.String("sequence_number"))
	.WriteToken(jsontext.Uint(.SequenceNumber))
	.WriteToken(jsontext.String("retire_prior_to"))
	.WriteToken(jsontext.Uint(.RetirePriorTo))
	.WriteToken(jsontext.String("length"))
	.WriteToken(jsontext.Int(int64(.ConnectionID.Len())))
	.WriteToken(jsontext.String("connection_id"))
	.WriteToken(jsontext.String(.ConnectionID.String()))
	.WriteToken(jsontext.String("stateless_reset_token"))
	.WriteToken(jsontext.String(hex.EncodeToString(.StatelessResetToken[:])))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeRetireConnectionIDFrame( *jsontext.Encoder,  *RetireConnectionIDFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("retire_connection_id"))
	.WriteToken(jsontext.String("sequence_number"))
	.WriteToken(jsontext.Uint(.SequenceNumber))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodePathChallengeFrame( *jsontext.Encoder,  *PathChallengeFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("path_challenge"))
	.WriteToken(jsontext.String("data"))
	.WriteToken(jsontext.String(hex.EncodeToString(.Data[:])))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodePathResponseFrame( *jsontext.Encoder,  *PathResponseFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("path_response"))
	.WriteToken(jsontext.String("data"))
	.WriteToken(jsontext.String(hex.EncodeToString(.Data[:])))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeConnectionCloseFrame( *jsontext.Encoder,  *ConnectionCloseFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("connection_close"))
	.WriteToken(jsontext.String("error_space"))
	 := "transport"
	if .IsApplicationError {
		 = "application"
	}
	.WriteToken(jsontext.String())
	 := transportError(.ErrorCode).String()
	if len() > 0 {
		.WriteToken(jsontext.String("error_code"))
		.WriteToken(jsontext.String())
	} else {
		.WriteToken(jsontext.String("error_code"))
		.WriteToken(jsontext.Uint(.ErrorCode))
	}
	.WriteToken(jsontext.String("raw_error_code"))
	.WriteToken(jsontext.Uint(.ErrorCode))
	.WriteToken(jsontext.String("reason"))
	.WriteToken(jsontext.String(.ReasonPhrase))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeHandshakeDoneFrame( *jsontext.Encoder,  *HandshakeDoneFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("handshake_done"))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeDatagramFrame( *jsontext.Encoder,  *DatagramFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("datagram"))
	.WriteToken(jsontext.String("length"))
	.WriteToken(jsontext.Int(.Length))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeAckFrequencyFrame( *jsontext.Encoder,  *AckFrequencyFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("ack_frequency"))
	.WriteToken(jsontext.String("sequence_number"))
	.WriteToken(jsontext.Uint(.SequenceNumber))
	.WriteToken(jsontext.String("ack_eliciting_threshold"))
	.WriteToken(jsontext.Uint(.AckElicitingThreshold))
	.WriteToken(jsontext.String("request_max_ack_delay"))
	.WriteToken(jsontext.Float(milliseconds(.RequestMaxAckDelay)))
	.WriteToken(jsontext.String("reordering_threshold"))
	.WriteToken(jsontext.Int(int64(.ReorderingThreshold)))
	.WriteToken(jsontext.EndObject)
	return .err
}

func encodeImmediateAckFrame( *jsontext.Encoder,  *ImmediateAckFrame) error {
	 := encoderHelper{enc: }
	.WriteToken(jsontext.BeginObject)
	.WriteToken(jsontext.String("frame_type"))
	.WriteToken(jsontext.String("immediate_ack"))
	.WriteToken(jsontext.EndObject)
	return .err
}