use solana_account::{
    Account, AccountSharedData, DUMMY_INHERITABLE_ACCOUNT_FIELDS, InheritableAccountFields,
};
use solana_hash::Hash;
use solana_instructions_sysvar::construct_instructions_data;
use solana_message::SanitizedMessage;
use solana_sha256_hasher::Hasher;

pub(crate) mod inner_instructions;
pub(crate) mod rent;
#[cfg(feature = "serde")]
pub(crate) mod serde_with_str;

/// Create a blockhash from the given bytes
pub(crate) fn create_blockhash(bytes: &[u8]) -> Hash {
    let mut hasher = Hasher::default();
    hasher.hash(bytes);
    hasher.result()
}

pub(crate) fn construct_instructions_account(message: &SanitizedMessage) -> AccountSharedData {
    AccountSharedData::from(Account {
        data: construct_instructions_data(&message.decompile_instructions()),
        owner: solana_sdk_ids::sysvar::id(),
        ..Account::default()
    })
}

pub(crate) fn create_loadable_account_with_fields(
    name: &str,
    (lamports, rent_epoch): InheritableAccountFields,
) -> AccountSharedData {
    AccountSharedData::from(Account {
        lamports,
        owner: solana_sdk_ids::native_loader::id(),
        data: name.as_bytes().to_vec(),
        executable: true,
        rent_epoch,
    })
}

pub(crate) fn create_loadable_account_for_test(name: &str) -> AccountSharedData {
    create_loadable_account_with_fields(name, DUMMY_INHERITABLE_ACCOUNT_FIELDS)
}
