// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package stun

import 

// UnknownAttributes represents UNKNOWN-ATTRIBUTES attribute.
//
// RFC 5389 Section 15.9
type UnknownAttributes []AttrType

func ( UnknownAttributes) () string {
	 := ""
	if len() == 0 {
		return "<nil>"
	}
	 := len() - 1
	for ,  := range  {
		 += .String()
		if  !=  {
			 += ", "
		}
	}
	return 
}

// type size is 16 bit.
const attrTypeSize = 4

// AddTo adds UNKNOWN-ATTRIBUTES attribute to message.
func ( UnknownAttributes) ( *Message) error {
	 := make([]byte, 0, attrTypeSize*20) // 20 should be enough
	// If len(a.Types) > 20, there will be allocations.
	for ,  := range  {
		 = append(, 0, 0, 0, 0) // 4 times by 0 (16 bits)
		 := attrTypeSize * 
		 :=  + attrTypeSize
		bin.PutUint16([:], .Value())
	}
	.Add(AttrUnknownAttributes, )
	return nil
}

// ErrBadUnknownAttrsSize means that UNKNOWN-ATTRIBUTES attribute value
// has invalid length.
var ErrBadUnknownAttrsSize = errors.New("bad UNKNOWN-ATTRIBUTES size")

// GetFrom parses UNKNOWN-ATTRIBUTES from message.
func ( *UnknownAttributes) ( *Message) error {
	,  := .Get(AttrUnknownAttributes)
	if  != nil {
		return 
	}
	if len()%attrTypeSize != 0 {
		return ErrBadUnknownAttrsSize
	}
	* = (*)[:0]
	 := 0
	for  < len() {
		 :=  + attrTypeSize
		* = append(*, AttrType(bin.Uint16([:])))
		 = 
	}
	return nil
}