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

package sctp

import (
	
	
	
	
)

type paramHeaderUnrecognizedAction byte

type paramHeader struct {
	typ                paramType
	unrecognizedAction paramHeaderUnrecognizedAction
	len                int
	raw                []byte
}

/*
	 The Parameter Types are encoded such that the highest-order 2 bits specify
	 the action that is taken if the processing endpoint does not recognize the
	 Parameter Type.

	 00 - Stop processing this parameter and do not process any further parameters within this chunk.

	 01 - Stop processing this parameter, do not process any further parameters within this chunk, and
		  report the unrecognized parameter, as described in Section 3.2.2.

	 10 - Skip this parameter and continue processing.

	 11 - Skip this parameter and continue processing, but report the unrecognized
		  parameter, as described in Section 3.2.2.

	 https://www.rfc-editor.org/rfc/rfc9260.html#section-3.2.1
*/

const (
	paramHeaderUnrecognizedActionMask                                        = 0b11000000
	paramHeaderUnrecognizedActionStop          paramHeaderUnrecognizedAction = 0b00000000
	paramHeaderUnrecognizedActionStopAndReport paramHeaderUnrecognizedAction = 0b01000000
	paramHeaderUnrecognizedActionSkip          paramHeaderUnrecognizedAction = 0b10000000
	paramHeaderUnrecognizedActionSkipAndReport paramHeaderUnrecognizedAction = 0b11000000

	paramHeaderLength = 4
)

// Parameter header parse errors.
var (
	ErrParamHeaderTooShort                  = errors.New("param header too short")
	ErrParamHeaderSelfReportedLengthShorter = errors.New("param self reported length is shorter than header length")
	ErrParamHeaderSelfReportedLengthLonger  = errors.New("param self reported length is longer than header length")
	ErrParamHeaderParseFailed               = errors.New("failed to parse param type")
)

func ( *paramHeader) () ([]byte, error) {
	 := paramHeaderLength + len(.raw)

	 := make([]byte, )
	binary.BigEndian.PutUint16([0:], uint16(.typ))
	binary.BigEndian.PutUint16([2:], uint16()) //nolint:gosec // G115
	copy([paramHeaderLength:], .raw)

	return , nil
}

func ( *paramHeader) ( []byte) error {
	if len() < paramHeaderLength {
		return ErrParamHeaderTooShort
	}

	 := binary.BigEndian.Uint16([2:])
	if int() < paramHeaderLength {
		return fmt.Errorf(
			"%w: param self reported length (%d) shorter than header length (%d)",
			ErrParamHeaderSelfReportedLengthShorter, int(), paramHeaderLength,
		)
	}
	if len() < int() {
		return fmt.Errorf(
			"%w: param length (%d) shorter than its self reported length (%d)",
			ErrParamHeaderSelfReportedLengthLonger, len(), int(),
		)
	}

	,  := parseParamType([0:])
	if  != nil {
		return fmt.Errorf("%w: %v", ErrParamHeaderParseFailed, ) //nolint:errorlint
	}
	.typ = 
	.unrecognizedAction = paramHeaderUnrecognizedAction([0] & paramHeaderUnrecognizedActionMask)
	.raw = [paramHeaderLength:]
	.len = int()

	return nil
}

func ( *paramHeader) () int {
	return .len
}

// String makes paramHeader printable.
func ( paramHeader) () string {
	return fmt.Sprintf("%s (%d): %s", .typ, .len, hex.Dump(.raw))
}