// 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 ndhost

import (
	"encoding/json"
	"errors"

	"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_set is the host function provided by Navidrome.
//
//go:wasmimport extism:host/user config_set
func config_set(uint64) uint64

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

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

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

type configSetRequest struct {
	Key   string `json:"key"`
	Value string `json:"value"`
}

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

type configHasResponse struct {
	Exists bool   `json:"exists,omitempty"`
	Error  string `json:"error,omitempty"`
}

// ConfigGet calls the config_get host function.
func ConfigGet(key string) (string, bool, error) {
	// Marshal request to JSON
	req := configGetRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return "", false, err
	}
	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, err
	}

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

	return response.Value, response.Exists, nil
}

// ConfigSet calls the config_set host function.
func ConfigSet(key string, value string) error {
	// Marshal request to JSON
	req := configSetRequest{
		Key:   key,
		Value: value,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

	// Parse error-only response
	var response struct {
		Error string `json:"error,omitempty"`
	}
	if err := json.Unmarshal(responseBytes, &response); err != nil {
		return err
	}
	if response.Error != "" {
		return errors.New(response.Error)
	}
	return nil
}

// ConfigHas calls the config_has host function.
func ConfigHas(key string) (bool, error) {
	// Marshal request to JSON
	req := configHasRequest{
		Key: key,
	}
	reqBytes, err := json.Marshal(req)
	if err != nil {
		return false, err
	}
	reqMem := pdk.AllocateBytes(reqBytes)
	defer reqMem.Free()

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

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

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

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

	return response.Exists, nil
}
