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

package stun

// Interfaces that are implemented by message attributes, shorthands for them,
// or helpers for message fields as type or transaction id.
type (
	// Setter sets *Message attribute.
	Setter interface {
		AddTo(m *Message) error
	}
	// Getter parses attribute from *Message.
	Getter interface {
		GetFrom(m *Message) error
	}
	// Checker checks *Message attribute.
	Checker interface {
		Check(m *Message) error
	}
)

// Build resets message and applies setters to it in batch, returning on
// first error. To prevent allocations, pass pointers to values.
//
// Example:
//
//	var (
//		t        = BindingRequest
//		username = NewUsername("username")
//		nonce    = NewNonce("nonce")
//		realm    = NewRealm("example.org")
//	)
//	m := new(Message)
//	m.Build(t, username, nonce, realm)     // 4 allocations
//	m.Build(&t, &username, &nonce, &realm) // 0 allocations
//
// See BenchmarkBuildOverhead.
func ( *Message) ( ...Setter) error {
	.Reset()
	.WriteHeader()
	for ,  := range  {
		if  := .AddTo();  != nil {
			return 
		}
	}
	return nil
}

// Check applies checkers to message in batch, returning on first error.
func ( *Message) ( ...Checker) error {
	for ,  := range  {
		if  := .Check();  != nil {
			return 
		}
	}
	return nil
}

// Parse applies getters to message in batch, returning on first error.
func ( *Message) ( ...Getter) error {
	for ,  := range  {
		if  := .GetFrom();  != nil {
			return 
		}
	}
	return nil
}

// MustBuild wraps Build call and panics on error.
func ( ...Setter) *Message {
	,  := Build(...)
	if  != nil {
		panic() //nolint
	}
	return 
}

// Build wraps Message.Build method.
func ( ...Setter) (*Message, error) {
	 := new(Message)
	if  := .Build(...);  != nil {
		return nil, 
	}
	return , nil
}

// ForEach is helper that iterates over message attributes allowing to call
// Getter in f callback to get all attributes of type t and returning on first
// f error.
//
// The m.Get method inside f will be returning next attribute on each f call.
// Does not error if there are no results.
func ( *Message) ( AttrType,  func( *Message) error) error {
	 := .Attributes
	defer func() {
		.Attributes = 
	}()
	for ,  := range  {
		if .Type !=  {
			continue
		}
		.Attributes = [:]
		if  := ();  != nil {
			return 
		}
	}
	return nil
}