use js_export_macro::js_export;
use miden_client::account::AccountVaultPatch as NativeAccountVaultPatch;
use crate::models::fungible_asset::FungibleAsset;
use crate::models::word::Word;
use crate::platform::{JsBytes, JsErr};
use crate::utils::{deserialize_from_bytes, serialize_to_bytes};
#[derive(Clone)]
#[js_export]
pub struct AccountVaultPatch(NativeAccountVaultPatch);
#[js_export]
impl AccountVaultPatch {
pub fn serialize(&self) -> JsBytes {
serialize_to_bytes(&self.0)
}
pub fn deserialize(bytes: JsBytes) -> Result<AccountVaultPatch, JsErr> {
deserialize_from_bytes::<NativeAccountVaultPatch>(&bytes).map(Self)
}
#[js_export(js_name = "isEmpty")]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[js_export(js_name = "numAssets")]
pub fn num_assets(&self) -> usize {
self.0.num_assets()
}
#[js_export(js_name = "updatedFungibleAssets")]
pub fn updated_fungible_assets(&self) -> Vec<FungibleAsset> {
self.0
.updated_assets()
.filter(miden_client::asset::Asset::is_fungible)
.map(|asset| asset.unwrap_fungible().into())
.collect()
}
#[js_export(js_name = "removedAssetIds")]
pub fn removed_asset_ids(&self) -> Vec<Word> {
self.0.removed_asset_ids().map(|id| id.to_word().into()).collect()
}
}
impl From<NativeAccountVaultPatch> for AccountVaultPatch {
fn from(patch: NativeAccountVaultPatch) -> Self {
Self(patch)
}
}
impl From<&NativeAccountVaultPatch> for AccountVaultPatch {
fn from(patch: &NativeAccountVaultPatch) -> Self {
Self(patch.clone())
}
}
impl From<AccountVaultPatch> for NativeAccountVaultPatch {
fn from(patch: AccountVaultPatch) -> Self {
patch.0
}
}