// Code generated by ndpgen. DO NOT EDIT.
//
// This file contains client wrappers for the Stream 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 StreamGetStreamRequest {
    uri: String,
}

#[host_fn]
extern "ExtismHost" {
}

#[link(wasm_import_module = "extism:host/user")]
extern "C" {
    fn stream_getstream(offset: u64) -> u64;
}

/// GetStream returns raw binary stream data with content type.
///
/// # Arguments
/// * `uri` - String parameter.
///
/// # Returns
/// A tuple of (content_type, data) with the raw binary response.
///
/// # Errors
/// Returns an error if the host function call fails.
pub fn get_stream(uri: &str) -> Result<(String, Vec<u8>), Error> {
    let req = StreamGetStreamRequest {
        uri: uri.to_owned(),
    };
    let input_bytes = serde_json::to_vec(&req).map_err(|e| Error::msg(e.to_string()))?;
    let input_mem = Memory::from_bytes(&input_bytes).map_err(|e| Error::msg(e.to_string()))?;

    let response_offset = unsafe { stream_getstream(input_mem.offset()) };

    let response_mem = Memory::find(response_offset)
        .ok_or_else(|| Error::msg("empty response from host"))?;
    let response_bytes = response_mem.to_vec();

    if response_bytes.is_empty() {
        return Err(Error::msg("empty response from host"));
    }
    if response_bytes[0] == 0x01 {
        let msg = String::from_utf8_lossy(&response_bytes[1..]).to_string();
        return Err(Error::msg(msg));
    }
    if response_bytes[0] != 0x00 {
        return Err(Error::msg("unknown response status"));
    }
    if response_bytes.len() < 5 {
        return Err(Error::msg("malformed raw response: incomplete header"));
    }
    let ct_len = u32::from_be_bytes([
        response_bytes[1],
        response_bytes[2],
        response_bytes[3],
        response_bytes[4],
    ]) as usize;
    if ct_len > response_bytes.len() - 5 {
        return Err(Error::msg("malformed raw response: content-type overflow"));
    }
    let ct_end = 5 + ct_len;
    let content_type = String::from_utf8_lossy(&response_bytes[5..ct_end]).to_string();
    let data = response_bytes[ct_end..].to_vec();
    Ok((content_type, data))
}
