use js_export_macro::js_export;
use miden_client::asset::{Asset as NativeAsset, NonFungibleAsset as NativeNonFungibleAsset};
use super::account_id::AccountId;
use super::word::Word;
use crate::platform::{JsErr, from_str_err};
#[derive(Clone, Copy)]
#[js_export]
pub struct NonFungibleAsset(NativeNonFungibleAsset);
#[js_export]
impl NonFungibleAsset {
#[js_export(js_name = "fromVaultEntry")]
pub fn from_vault_entry(key: &Word, value: &Word) -> Result<NonFungibleAsset, JsErr> {
NativeNonFungibleAsset::from_id_and_value_words(key.into(), value.into())
.map(NonFungibleAsset)
.map_err(|err| from_str_err(&format!("Failed to create NonFungibleAsset: {err}")))
}
#[js_export(js_name = "faucetId")]
pub fn faucet_id(&self) -> AccountId {
self.0.faucet_id().into()
}
#[js_export(js_name = "vaultKey")]
pub fn vault_key(&self) -> Word {
self.0.to_id_word().into()
}
#[js_export(js_name = "intoWord")]
pub fn into_word(&self) -> Word {
self.0.to_value_word().into()
}
}
impl From<NativeNonFungibleAsset> for NonFungibleAsset {
fn from(asset: NativeNonFungibleAsset) -> Self {
Self(asset)
}
}
impl From<NonFungibleAsset> for NativeAsset {
fn from(asset: NonFungibleAsset) -> Self {
asset.0.into()
}
}
impl From<&NonFungibleAsset> for NativeAsset {
fn from(asset: &NonFungibleAsset) -> Self {
asset.0.into()
}
}
impl_napi_from_value!(NonFungibleAsset);