package quic

import (
	
	

	
)

var errQNTTooManyRemoteAddresses = ErrNATTraversalTooManyAddresses

type qntRemoteAddressState struct {
	max   int
	addrs map[uint64]netip.AddrPort
}

func newQNTRemoteAddressState( uint8) *qntRemoteAddressState {
	return &qntRemoteAddressState{max: int(), addrs: make(map[uint64]netip.AddrPort)}
}

func ( *qntRemoteAddressState) ( *wire.AddAddressFrame) (netip.AddrPort, bool, error) {
	 := canonicalAddrPort(.Addr, .Port)
	if !.IsValid() || .Port() == 0 {
		return netip.AddrPort{}, false, nil
	}
	if ,  := .addrs[.SeqNo];  {
		if  ==  {
			return netip.AddrPort{}, false, nil
		}
		.addrs[.SeqNo] = 
		return , true, nil
	}
	if len(.addrs) >= .max {
		return netip.AddrPort{}, false, errQNTTooManyRemoteAddresses
	}
	.addrs[.SeqNo] = 
	return , true, nil
}

func ( *qntRemoteAddressState) ( *wire.RemoveAddressFrame) (netip.AddrPort, bool) {
	,  := .addrs[.SeqNo]
	if ! {
		return netip.AddrPort{}, false
	}
	delete(.addrs, .SeqNo)
	return , true
}

func ( *qntRemoteAddressState) ( *wire.AddAddressFrame) bool {
	 := canonicalAddrPort(.Addr, .Port)
	,  := .addrs[.SeqNo]
	return ! ||  == 
}

func ( *qntRemoteAddressState) () []netip.AddrPort {
	 := make([]uint64, 0, len(.addrs))
	for  := range .addrs {
		 = append(, )
	}
	slices.Sort()
	 := make([]netip.AddrPort, 0, len(.addrs))
	for ,  := range  {
		 = append(, .addrs[])
	}
	return 
}

func canonicalAddrPort( netip.Addr,  uint16) netip.AddrPort {
	return netip.AddrPortFrom(.Unmap(), )
}