#![cfg_attr(docsrs, feature(doc_cfg))]
//! Token operations for the HPSVM

mod approve;
mod approve_checked;
mod burn;
mod burn_checked;
mod close_account;
mod create_account;
mod create_ata;
mod create_ata_idempotent;
mod create_mint;
mod create_multisig;
#[cfg(not(feature = "token-2022"))]
mod create_native_mint;
#[cfg(feature = "token-2022")]
mod create_native_mint_2022;
mod freeze_account;
mod mint_to;
mod mint_to_checked;
mod revoke;
mod set_authority;
mod sync_native;
mod thaw_account;
mod transfer;
mod transfer_checked;

pub use approve::*;
pub use approve_checked::*;
pub use burn::*;
pub use burn_checked::*;
pub use close_account::*;
pub use create_account::*;
pub use create_ata::*;
pub use create_ata_idempotent::*;
pub use create_mint::*;
pub use create_multisig::*;
pub use create_native_mint::*;
#[cfg(feature = "token-2022")]
use create_native_mint_2022 as create_native_mint;
pub use freeze_account::*;
use hpsvm::{HPSVM, types::FailedTransactionMetadata};
pub use mint_to::*;
pub use mint_to_checked::*;
pub use revoke::*;
pub use set_authority::*;
use solana_address::Address;
use solana_program_pack::{IsInitialized, Pack};
use solana_transaction_error::TransactionError;
#[cfg(feature = "token-2022")]
pub use spl_token_2022_interface as spl_token;
#[cfg(not(feature = "token-2022"))]
pub use spl_token_interface as spl_token;
pub use sync_native::*;
pub use thaw_account::*;
pub use transfer::*;
pub use transfer_checked::*;

/// SPL Token program ID
pub const TOKEN_ID: Address = spl_token::ID;

/// Get an SPL account from the SVM
pub fn get_spl_account<T: Pack + IsInitialized>(
    svm: &HPSVM,
    account: &Address,
) -> Result<T, FailedTransactionMetadata> {
    let account = T::unpack(
        &svm.get_account(account)
            .ok_or(FailedTransactionMetadata {
                err: TransactionError::AccountNotFound,
                meta: Default::default(),
            })?
            .data[..T::LEN],
    )?;

    Ok(account)
}

fn get_multisig_signers<'a>(
    authority: &Address,
    signing_pubkeys: &'a [Address],
) -> Vec<&'a Address> {
    if signing_pubkeys == [*authority] {
        vec![]
    } else {
        signing_pubkeys.iter().collect::<Vec<_>>()
    }
}
