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

package ndhost

import (
	"encoding/json"
	"errors"

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

// echo_echo is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user echo_echo
func echo_echo(uint64) uint64

type echoEchoRequest struct {
	Message string `json:"message"`
}

type echoEchoResponse struct {
	Reply string `json:"reply,omitempty"`
	Error string `json:"error,omitempty"`
}

// EchoEcho calls the echo_echo host function.
func EchoEcho(message string) (string, error) {
	// Marshal request to JSON
	req := echoEchoRequest{
		Message: message,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

	// Call the host function
	responsePtr := echo_echo(reqMem.Offset())

	// Read the response from memory
	responseMem := pdk.FindMemory(responsePtr)
	responseBytes := responseMem.ReadBytes()

	// Parse the response
	var response echoEchoResponse
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return "", err
	}

	// Convert Error field to Go error
	if response.Error != "" {
		return "", errors.New(response.Error)
	}

	return response.Reply, nil
}
