// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Meta host service.
// It is intended for use in Navidrome plugins built with extism-pdk.

use extism_pdk::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct MetaGetRequest {
    key: String,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MetaGetResponse {
    #[serde(default)]
    value: serde_json::Value,
    #[serde(default)]
    error: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct MetaSetRequest {
    data: std::collections::HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
struct MetaSetResponse {
    #[serde(default)]
    error: Option<String>,
}

#[host_fn]
extern "ExtismHost" {
    fn meta_get(input: Json<MetaGetRequest>) -> Json<MetaGetResponse>;
    fn meta_set(input: Json<MetaSetRequest>) -> Json<MetaSetResponse>;
}

/// Calls the meta_get host function.
///
/// # Arguments
/// * `key` - String parameter.
///
/// # Returns
/// The value value.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get(key: &str) -> Result<serde_json::Value, Error> {
    let response = unsafe {
        meta_get(Json(MetaGetRequest {
            key: key.to_owned(),
        }))?
    };

    if let Some(err) = response.0.error {
        return Err(Error::msg(err));
    }

    Ok(response.0.value)
}

/// Calls the meta_set host function.
///
/// # Arguments
/// * `data` - std::collections::HashMap<String, serde_json::Value> parameter.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn set(data: std::collections::HashMap<String, serde_json::Value>) -> Result<(), Error> {
    let response = unsafe {
        meta_set(Json(MetaSetRequest {
            data: data,
        }))?
    };

    if let Some(err) = response.0.error {
        return Err(Error::msg(err));
    }

    Ok(())
}
