// Code generated by ndpgen. DO NOT EDIT.

package testpkg

import (
	"context"
	"encoding/json"

	extism "github.com/extism/go-sdk"
)

// PingPingResponse is the response type for Ping.Ping.
type PingPingResponse struct {
	Error string `json:"error,omitempty"`
}

// RegisterPingHostFunctions registers Ping service host functions.
// The returned host functions should be added to the plugin's configuration.
func RegisterPingHostFunctions(service PingService) []extism.HostFunction {
	return []extism.HostFunction{
		newPingPingHostFunction(service),
	}
}

func newPingPingHostFunction(service PingService) extism.HostFunction {
	return extism.NewHostFunctionWithStack(
		"ping_ping",
		func(ctx context.Context, p *extism.CurrentPlugin, stack []uint64) {

			// Call the service method
			if svcErr := service.Ping(ctx); svcErr != nil {
				pingWriteError(p, stack, svcErr)
				return
			}

			// Write JSON response to plugin memory
			resp := PingPingResponse{}
			pingWriteResponse(p, stack, resp)
		},
		[]extism.ValueType{extism.ValueTypePTR},
		[]extism.ValueType{extism.ValueTypePTR},
	)
}

// pingWriteResponse writes a JSON response to plugin memory.
func pingWriteResponse(p *extism.CurrentPlugin, stack []uint64, resp any) {
	respBytes, err := json.Marshal(resp)
	if err != nil {
		pingWriteError(p, stack, err)
		return
	}
	respPtr, err := p.WriteBytes(respBytes)
	if err != nil {
		stack[0] = 0
		return
	}
	stack[0] = respPtr
}

// pingWriteError writes an error response to plugin memory.
func pingWriteError(p *extism.CurrentPlugin, stack []uint64, err error) {
	errResp := struct {
		Error string `json:"error"`
	}{Error: err.Error()}
	respBytes, _ := json.Marshal(errResp)
	respPtr, _ := p.WriteBytes(respBytes)
	stack[0] = respPtr
}
