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

package ndhost

import (
	"encoding/json"
	"errors"

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

// Result represents the Result data structure.
type Result struct {
	ID string `json:"id"`
}

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

// SearchFindRequest is the request type for Search.Find.
type SearchFindRequest struct {
	Query string `json:"query"`
}

// SearchFindResponse is the response type for Search.Find.
type SearchFindResponse struct {
	Results []Result `json:"results,omitempty"`
	Total   int32    `json:"total,omitempty"`
	Error   string   `json:"error,omitempty"`
}

// SearchFind calls the search_find host function.
func SearchFind(query string) (*SearchFindResponse, error) {
	// Marshal request to JSON
	req := SearchFindRequest{
		Query: query,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

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

	return &response, nil
}
