# Code generated by ndpgen. DO NOT EDIT.
#
# This file contains client wrappers for the Library host service.
# It is intended for use in Navidrome plugins built with extism-py.
#
# IMPORTANT: Due to a limitation in extism-py, you cannot import this file directly.
# The @extism.import_fn decorators are only detected when defined in the plugin's
# main __init__.py file. Copy the needed functions from this file into your plugin.

from dataclasses import dataclass
from typing import Any

import extism
import json


class HostFunctionError(Exception):
    """Raised when a host function returns an error."""
    pass


@extism.import_fn("extism:host/user", "library_getlibrary")
def _library_getlibrary(offset: int) -> int:
    """Raw host function - do not call directly."""
    ...


@extism.import_fn("extism:host/user", "library_getalllibraries")
def _library_getalllibraries(offset: int) -> int:
    """Raw host function - do not call directly."""
    ...


def library_get_library(id: int) -> Any:
    """GetLibrary retrieves metadata for a specific library by ID.

Parameters:
  - id: The library's unique identifier

Returns the library metadata, or an error if the library is not found.

    Args:
        id: int parameter.

    Returns:
        Any: The result value.

    Raises:
        HostFunctionError: If the host function returns an error.
    """
    request = {
        "id": id,
    }
    request_bytes = json.dumps(request).encode("utf-8")
    request_mem = extism.memory.alloc(request_bytes)
    response_offset = _library_getlibrary(request_mem.offset)
    response_mem = extism.memory.find(response_offset)
    response = json.loads(extism.memory.string(response_mem))

    if response.get("error"):
        raise HostFunctionError(response["error"])

    return response.get("result", None)


def library_get_all_libraries() -> Any:
    """GetAllLibraries retrieves metadata for all configured libraries.

Returns a slice of all libraries with their metadata.

    Returns:
        Any: The result value.

    Raises:
        HostFunctionError: If the host function returns an error.
    """
    request_bytes = b"{}"
    request_mem = extism.memory.alloc(request_bytes)
    response_offset = _library_getalllibraries(request_mem.offset)
    response_mem = extism.memory.find(response_offset)
    response = json.loads(extism.memory.string(response_mem))

    if response.get("error"):
        raise HostFunctionError(response["error"])

    return response.get("result", None)
