// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains export wrappers for the WebSocketCallback capability.
// It is intended for use in Navidrome plugins built with TinyGo.
//
//go:build wasip1

package websocket

import (
	"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
)

// OnBinaryMessageRequest is the request provided when a binary message is received.
type OnBinaryMessageRequest struct {
	// ConnectionID is the unique identifier for the WebSocket connection that received the message.
	ConnectionID string `json:"connectionId"`
	// Data is the binary data received from the WebSocket, encoded as base64.
	Data []byte `json:"data"`
}

// OnCloseRequest is the request provided when a WebSocket connection is closed.
type OnCloseRequest struct {
	// ConnectionID is the unique identifier for the WebSocket connection that was closed.
	ConnectionID string `json:"connectionId"`
	// Code is the WebSocket close status code (e.g., 1000 for normal closure,
	// 1001 for going away, 1006 for abnormal closure).
	Code int32 `json:"code"`
	// Reason is the human-readable reason for the connection closure, if provided.
	Reason string `json:"reason"`
}

// OnErrorRequest is the request provided when an error occurs on a WebSocket connection.
type OnErrorRequest struct {
	// ConnectionID is the unique identifier for the WebSocket connection where the error occurred.
	ConnectionID string `json:"connectionId"`
	// Error is the error message describing what went wrong.
	Error string `json:"error"`
}

// OnTextMessageRequest is the request provided when a text message is received.
type OnTextMessageRequest struct {
	// ConnectionID is the unique identifier for the WebSocket connection that received the message.
	ConnectionID string `json:"connectionId"`
	// Message is the text message content received from the WebSocket.
	Message string `json:"message"`
}

// WebSocket is the marker interface for websocket plugins.
// Implement one or more of the provider interfaces below.
// WebSocketCallback provides WebSocket message handling.
// This capability allows plugins to receive callbacks for WebSocket events
// such as text messages, binary messages, errors, and connection closures.
// Plugins that use the WebSocket host service must implement this capability
// to handle incoming events.
type WebSocket interface{}

// TextMessageProvider provides the OnTextMessage function.
type TextMessageProvider interface {
	OnTextMessage(OnTextMessageRequest) error
}

// BinaryMessageProvider provides the OnBinaryMessage function.
type BinaryMessageProvider interface {
	OnBinaryMessage(OnBinaryMessageRequest) error
}

// ErrorProvider provides the OnError function.
type ErrorProvider interface {
	OnError(OnErrorRequest) error
}

// CloseProvider provides the OnClose function.
type CloseProvider interface {
	OnClose(OnCloseRequest) error
} // Internal implementation holders
var (
	textMessageImpl   func(OnTextMessageRequest) error
	binaryMessageImpl func(OnBinaryMessageRequest) error
	errorImpl         func(OnErrorRequest) error
	closeImpl         func(OnCloseRequest) error
)

// Register registers a websocket implementation.
// The implementation is checked for optional provider interfaces.
func Register(impl WebSocket) {
	if p, ok := impl.(TextMessageProvider); ok {
		textMessageImpl = p.OnTextMessage
	}
	if p, ok := impl.(BinaryMessageProvider); ok {
		binaryMessageImpl = p.OnBinaryMessage
	}
	if p, ok := impl.(ErrorProvider); ok {
		errorImpl = p.OnError
	}
	if p, ok := impl.(CloseProvider); ok {
		closeImpl = p.OnClose
	}
}

// NotImplementedCode is the standard return code for unimplemented functions.
// The host recognizes this and skips the plugin gracefully.
const NotImplementedCode int32 = -2

//go:wasmexport nd_websocket_on_text_message
func _NdWebsocketOnTextMessage() int32 {
	if textMessageImpl == nil {
		// Return standard code - host will skip this plugin gracefully
		return NotImplementedCode
	}

	var input OnTextMessageRequest
	if err := pdk.InputJSON(&input); err != nil {
		pdk.SetError(err)
		return -1
	}

	if err := textMessageImpl(input); err != nil {
		pdk.SetError(err)
		return -1
	}

	return 0
}

//go:wasmexport nd_websocket_on_binary_message
func _NdWebsocketOnBinaryMessage() int32 {
	if binaryMessageImpl == nil {
		// Return standard code - host will skip this plugin gracefully
		return NotImplementedCode
	}

	var input OnBinaryMessageRequest
	if err := pdk.InputJSON(&input); err != nil {
		pdk.SetError(err)
		return -1
	}

	if err := binaryMessageImpl(input); err != nil {
		pdk.SetError(err)
		return -1
	}

	return 0
}

//go:wasmexport nd_websocket_on_error
func _NdWebsocketOnError() int32 {
	if errorImpl == nil {
		// Return standard code - host will skip this plugin gracefully
		return NotImplementedCode
	}

	var input OnErrorRequest
	if err := pdk.InputJSON(&input); err != nil {
		pdk.SetError(err)
		return -1
	}

	if err := errorImpl(input); err != nil {
		pdk.SetError(err)
		return -1
	}

	return 0
}

//go:wasmexport nd_websocket_on_close
func _NdWebsocketOnClose() int32 {
	if closeImpl == nil {
		// Return standard code - host will skip this plugin gracefully
		return NotImplementedCode
	}

	var input OnCloseRequest
	if err := pdk.InputJSON(&input); err != nil {
		pdk.SetError(err)
		return -1
	}

	if err := closeImpl(input); err != nil {
		pdk.SetError(err)
		return -1
	}

	return 0
}
