package iroh

import (
	
	
	

	quic 
)

// ApplicationError is an application-defined connection close error.
type ApplicationError struct {
	// Code is the application-defined close code.
	Code uint64
	// Reason is the application-defined close reason.
	Reason string
	// Remote reports whether the peer sent the close.
	Remote bool
}

// Error returns a human-readable close error.
func ( *ApplicationError) () string {
	 := "local"
	if .Remote {
		 = "remote"
	}
	if .Reason == "" {
		return fmt.Sprintf("application close %d (%s)", .Code, )
	}
	return fmt.Sprintf("application close %d (%s): %s", .Code, , .Reason)
}

// Unwrap returns [net.ErrClosed].
func ( *ApplicationError) () error { return net.ErrClosed }

// AsApplicationError returns the application close error in err, if any.
func ( error) (*ApplicationError, bool) {
	var  *quic.ApplicationError
	if !errors.As(, &) {
		return nil, false
	}
	return &ApplicationError{
		Code:   uint64(.ErrorCode),
		Reason: .ErrorMessage,
		Remote: .Remote,
	}, true
}

// ErrTLSHandshakeFailure reports a QUIC handshake that ended in the TLS
// "handshake_failure" alert (alert 40), which a peer sends when it finds no
// acceptable set of handshake parameters. Between iroh endpoints the cause is a
// key-exchange group mismatch: the two [KeyExchangePolicy] values have no group
// in common, so neither side can complete the exchange. The cipher suite and
// the raw-public-key signature algorithm are fixed by the protocol and never
// differ between iroh peers; a peer running another implementation may send the
// same alert for another reason.
//
// [Endpoint.Connect] and [Endpoint.Dial] wrap such a failure so it can be told
// apart from other handshake failures without matching error text:
//
//	if _, err := ep.Connect(ctx, addr, alpn); errors.Is(err, iroh.ErrTLSHandshakeFailure) {
//		// no key exchange group in common; see WithKeyExchangePolicy
//	}
//
// The alert is symmetric: the dialer sees it whether its own policy or the
// peer's was the narrower one.
var ErrTLSHandshakeFailure = errors.New("iroh: tls handshake failure")

// alertHandshakeFailure is the TLS handshake_failure alert (RFC 8446, B.2).
const alertHandshakeFailure = 40

// tlsHandshakeFailure joins err with [ErrTLSHandshakeFailure] when err carries a
// QUIC CRYPTO_ERROR for the handshake_failure alert, and returns err unchanged
// otherwise. The message is err's own, so only errors.Is sees the difference.
func tlsHandshakeFailure( error) error {
	var  *quic.TransportError
	if !errors.As(, &) || !.ErrorCode.IsCryptoError() {
		return 
	}
	if .ErrorCode-0x100 != alertHandshakeFailure {
		return 
	}
	return handshakeFailureError{}
}

// handshakeFailureError adds [ErrTLSHandshakeFailure] to an error chain without
// changing what the error says.
type handshakeFailureError struct{ err error }

func ( handshakeFailureError) () string   { return .err.Error() }
func ( handshakeFailureError) () []error { return []error{ErrTLSHandshakeFailure, .err} }