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

package sctp

import (
	
	
)

// errorCauseHeader represents the shared header that is shared by all error causes.
type errorCauseHeader struct {
	code errorCauseCode
	len  uint16
	raw  []byte
}

const (
	errorCauseHeaderLength = 4
)

// ErrInvalidSCTPChunk is returned when an SCTP chunk is invalid.
var ErrInvalidSCTPChunk = errors.New("invalid SCTP chunk")

func ( *errorCauseHeader) () ([]byte, error) {
	.len = uint16(len(.raw)) + uint16(errorCauseHeaderLength) //nolint:gosec // G115
	 := make([]byte, .len)
	binary.BigEndian.PutUint16([0:], uint16(.code))
	binary.BigEndian.PutUint16([2:], .len)
	copy([errorCauseHeaderLength:], .raw)

	return , nil
}

func ( *errorCauseHeader) ( []byte) error {
	.code = errorCauseCode(binary.BigEndian.Uint16([0:]))
	.len = binary.BigEndian.Uint16([2:])
	if .len < errorCauseHeaderLength || int(.len) > len() {
		return ErrInvalidSCTPChunk
	}
	 := .len - errorCauseHeaderLength
	.raw = [errorCauseHeaderLength : errorCauseHeaderLength+]

	return nil
}

func ( *errorCauseHeader) () uint16 {
	return .len
}

func ( *errorCauseHeader) () errorCauseCode {
	return .code
}

// String makes errorCauseHeader printable.
func ( errorCauseHeader) () string {
	return .code.String()
}