//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::PlatformFeeWrapUnwrapArgs,
    carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts},
};
/// Wrap/Unwrap with optional specified receiver
/// - Wrap (SOL -> WSOL): receiver is WSOL token account (ATA)
/// - Unwrap (WSOL -> SOL): receiver is system account (EOA)
///
/// Transfer amount:
/// - From fee: amount_in
/// - To fee: amount_in - fees
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct WrapUnwrapV3WithReceiver {
    pub args: PlatformFeeWrapUnwrapArgs,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WrapUnwrapV3WithReceiverInstructionAccounts {
    pub payer: solana_pubkey::Pubkey,
    pub payer_wsol_account: solana_pubkey::Pubkey,
    pub wsol_mint: solana_pubkey::Pubkey,
    pub temp_wsol_account: Option<solana_pubkey::Pubkey>,
    pub commission_account: Option<solana_pubkey::Pubkey>,
    pub platform_fee_account: Option<solana_pubkey::Pubkey>,
    pub authority_pda: Option<solana_pubkey::Pubkey>,
    pub wsol_sa: Option<solana_pubkey::Pubkey>,
    pub token_program: solana_pubkey::Pubkey,
    pub system_program: solana_pubkey::Pubkey,
    pub receiver: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl WrapUnwrapV3WithReceiver {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [70, 211, 190, 165, 47, 40, 213, 95] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

        borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
    }
}

impl ArrangeAccounts for WrapUnwrapV3WithReceiver {
    type ArrangedAccounts = WrapUnwrapV3WithReceiverInstructionAccounts;

    fn arrange_accounts(
        accounts: &[solana_instruction::AccountMeta],
    ) -> Option<Self::ArrangedAccounts> {
        let mut iter = accounts.iter();

        let payer = next_account(&mut iter)?;
        let payer_wsol_account = next_account(&mut iter)?;
        let wsol_mint = next_account(&mut iter)?;
        let temp_wsol_account = next_account(&mut iter);
        let commission_account = next_account(&mut iter);
        let platform_fee_account = next_account(&mut iter);
        let authority_pda = next_account(&mut iter);
        let wsol_sa = next_account(&mut iter);
        let token_program = next_account(&mut iter)?;
        let system_program = next_account(&mut iter)?;
        let receiver = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(WrapUnwrapV3WithReceiverInstructionAccounts {
            payer,
            payer_wsol_account,
            wsol_mint,
            temp_wsol_account,
            commission_account,
            platform_fee_account,
            authority_pda,
            wsol_sa,
            token_program,
            system_program,
            receiver,
            remaining: remaining.to_vec(),
        })
    }
}
