package wire

import (
	
	
	

	
)

// ParseVersionNegotiationPacket parses a Version Negotiation packet.
func ( []byte) (,  protocol.ArbitraryLenConnectionID,  []protocol.Version,  error) {
	, , ,  := ParseArbitraryLenConnectionIDs()
	if  != nil {
		return nil, nil, nil, 
	}
	 = [:]
	if len() == 0 {
		//nolint:staticcheck // SA1021: the packet is called Version Negotiation packet
		return nil, nil, nil, errors.New("Version Negotiation packet has empty version list")
	}
	if len()%4 != 0 {
		//nolint:staticcheck // SA1021: the packet is called Version Negotiation packet
		return nil, nil, nil, errors.New("Version Negotiation packet has a version list with an invalid length")
	}
	 := make([]protocol.Version, len()/4)
	for  := 0; len() > 0; ++ {
		[] = protocol.Version(binary.BigEndian.Uint32([:4]))
		 = [4:]
	}
	return , , , nil
}

// ComposeVersionNegotiation composes a Version Negotiation
func (,  protocol.ArbitraryLenConnectionID,  []protocol.Version) []byte {
	 := protocol.GetGreasedVersions()
	 := 1 /* type byte */ + 4 /* version field */ + 1 /* dest connection ID length field */ + .Len() + 1 /* src connection ID length field */ + .Len() + len()*4
	 := make([]byte, 1+4 /* type byte and version field */, )
	_, _ = rand.Read([:1]) // ignore the error here. It is not critical to have perfect random here.
	// Setting the "QUIC bit" (0x40) is not required by the RFC,
	// but it allows clients to demultiplex QUIC with a long list of other protocols.
	// See RFC 9443 and https://mailarchive.ietf.org/arch/msg/quic/oR4kxGKY6mjtPC1CZegY1ED4beg/ for details.
	[0] |= 0xc0
	// The next 4 bytes are left at 0 (version number).
	 = append(, uint8(.Len()))
	 = append(, .Bytes()...)
	 = append(, uint8(.Len()))
	 = append(, .Bytes()...)
	for ,  := range  {
		 = binary.BigEndian.AppendUint32(, uint32())
	}
	return 
}