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

package extension

import (
	

	
)

const (
	supportedGroupsHeaderSize = 6
)

// SupportedEllipticCurves allows a Client/Server to communicate
// what curves they both support
//
// https://tools.ietf.org/html/rfc8422#section-5.1.1
type SupportedEllipticCurves struct {
	EllipticCurves []elliptic.Curve
}

// TypeValue returns the extension TypeValue.
func ( SupportedEllipticCurves) () TypeValue {
	return SupportedEllipticCurvesTypeValue
}

// Marshal encodes the extension.
func ( *SupportedEllipticCurves) () ([]byte, error) {
	 := make([]byte, supportedGroupsHeaderSize)

	binary.BigEndian.PutUint16(, uint16(.TypeValue()))
	binary.BigEndian.PutUint16([2:], uint16(2+(len(.EllipticCurves)*2))) //nolint:gosec // G115
	binary.BigEndian.PutUint16([4:], uint16(len(.EllipticCurves)*2))     //nolint:gosec // G115

	for ,  := range .EllipticCurves {
		 = append(, []byte{0x00, 0x00}...) //nolint:makezero // todo: fix
		binary.BigEndian.PutUint16([len()-2:], uint16())
	}

	return , nil
}

// Unmarshal populates the extension from encoded data.
func ( *SupportedEllipticCurves) ( []byte) error {
	if len() <= supportedGroupsHeaderSize {
		return errBufferTooSmall
	} else if TypeValue(binary.BigEndian.Uint16()) != .TypeValue() {
		return errInvalidExtensionType
	}

	 := int(binary.BigEndian.Uint16([4:]) / 2)
	if supportedGroupsHeaderSize+(*2) > len() {
		return errLengthMismatch
	}

	for  := 0;  < ; ++ {
		 := elliptic.Curve(binary.BigEndian.Uint16([(supportedGroupsHeaderSize + ( * 2)):]))
		if ,  := elliptic.Curves()[];  {
			.EllipticCurves = append(.EllipticCurves, )
		}
	}

	return nil
}