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

package host

import (
	"encoding/json"

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

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

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

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

type configGetRequest struct {
	Key string `json:"key"`
}

type configGetResponse struct {
	Value  string `json:"value,omitempty"`
	Exists bool   `json:"exists,omitempty"`
}

type configGetIntRequest struct {
	Key string `json:"key"`
}

type configGetIntResponse struct {
	Value  int64 `json:"value,omitempty"`
	Exists bool  `json:"exists,omitempty"`
}

type configKeysRequest struct {
	Prefix string `json:"prefix"`
}

type configKeysResponse struct {
	Keys []string `json:"keys,omitempty"`
}

// ConfigGet calls the config_get host function.
// Get retrieves a configuration value as a string.
//
// Parameters:
//   - key: The configuration key
//
// Returns the value and whether the key exists.
func ConfigGet(key string) (string, bool) {
	// Marshal request to JSON
	req := configGetRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", false
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

	return response.Value, response.Exists
}

// ConfigGetInt calls the config_getint host function.
// GetInt retrieves a configuration value as an integer.
//
// Parameters:
//   - key: The configuration key
//
// Returns the value and whether the key exists. If the key exists but the
// value cannot be parsed as an integer, exists will be false.
func ConfigGetInt(key string) (int64, bool) {
	// Marshal request to JSON
	req := configGetIntRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return 0, false
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

	return response.Value, response.Exists
}

// ConfigKeys calls the config_keys host function.
// Keys returns configuration keys matching the given prefix.
//
// Parameters:
//   - prefix: Key prefix to filter by. If empty, returns all keys.
//
// Returns a sorted slice of matching configuration keys.
func ConfigKeys(prefix string) []string {
	// Marshal request to JSON
	req := configKeysRequest{
		Prefix: prefix,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return nil
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

	return response.Keys
}
