//! This code was AUTOGENERATED using the codama library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun codama to update it.
//!
//! <https://github.com/codama-idl/codama>
//!

use crate::generated::types::CloseActionReceiptArgs;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

pub const CLOSE_ACTION_RECEIPT_ACCOUNT_DISCRIMINATOR: u8 = 22;

/// Accounts.
#[derive(Debug)]
pub struct CloseActionReceiptAccount {
    pub mint: solana_pubkey::Pubkey,

    pub verification_config_or_mint_authority: solana_pubkey::Pubkey,

    pub instructions_sysvar_or_creator: solana_pubkey::Pubkey,

    pub receipt_account: solana_pubkey::Pubkey,

    pub destination: solana_pubkey::Pubkey,

    pub mint_account: solana_pubkey::Pubkey,
}

impl CloseActionReceiptAccount {
    pub fn instruction(
        &self,
        args: CloseActionReceiptAccountInstructionArgs,
    ) -> solana_instruction::Instruction {
        self.instruction_with_remaining_accounts(args, &[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::vec_init_then_push)]
    pub fn instruction_with_remaining_accounts(
        &self,
        args: CloseActionReceiptAccountInstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(6 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.mint, false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.verification_config_or_mint_authority,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.instructions_sysvar_or_creator,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.receipt_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.destination,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.mint_account,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&CloseActionReceiptAccountInstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&args).unwrap();
        data.append(&mut args);

        solana_instruction::Instruction {
            program_id: crate::SECURITY_TOKEN_PROGRAM_ID,
            accounts,
            data,
        }
    }
}

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CloseActionReceiptAccountInstructionData {
    discriminator: u8,
}

impl CloseActionReceiptAccountInstructionData {
    pub fn new() -> Self {
        Self { discriminator: 22 }
    }
}

impl Default for CloseActionReceiptAccountInstructionData {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CloseActionReceiptAccountInstructionArgs {
    pub close_action_receipt_args: CloseActionReceiptArgs,
}

/// Instruction builder for `CloseActionReceiptAccount`.
///
/// ### Accounts:
///
///   0. `[]` mint
///   1. `[]` verification_config_or_mint_authority
///   2. `[]` instructions_sysvar_or_creator
///   3. `[writable]` receipt_account
///   4. `[writable]` destination
///   5. `[]` mint_account
#[derive(Clone, Debug, Default)]
pub struct CloseActionReceiptAccountBuilder {
    mint: Option<solana_pubkey::Pubkey>,
    verification_config_or_mint_authority: Option<solana_pubkey::Pubkey>,
    instructions_sysvar_or_creator: Option<solana_pubkey::Pubkey>,
    receipt_account: Option<solana_pubkey::Pubkey>,
    destination: Option<solana_pubkey::Pubkey>,
    mint_account: Option<solana_pubkey::Pubkey>,
    close_action_receipt_args: Option<CloseActionReceiptArgs>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl CloseActionReceiptAccountBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    #[inline(always)]
    pub fn mint(&mut self, mint: solana_pubkey::Pubkey) -> &mut Self {
        self.mint = Some(mint);
        self
    }
    #[inline(always)]
    pub fn verification_config_or_mint_authority(
        &mut self,
        verification_config_or_mint_authority: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.verification_config_or_mint_authority = Some(verification_config_or_mint_authority);
        self
    }
    #[inline(always)]
    pub fn instructions_sysvar_or_creator(
        &mut self,
        instructions_sysvar_or_creator: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.instructions_sysvar_or_creator = Some(instructions_sysvar_or_creator);
        self
    }
    #[inline(always)]
    pub fn receipt_account(&mut self, receipt_account: solana_pubkey::Pubkey) -> &mut Self {
        self.receipt_account = Some(receipt_account);
        self
    }
    #[inline(always)]
    pub fn destination(&mut self, destination: solana_pubkey::Pubkey) -> &mut Self {
        self.destination = Some(destination);
        self
    }
    #[inline(always)]
    pub fn mint_account(&mut self, mint_account: solana_pubkey::Pubkey) -> &mut Self {
        self.mint_account = Some(mint_account);
        self
    }
    #[inline(always)]
    pub fn close_action_receipt_args(
        &mut self,
        close_action_receipt_args: CloseActionReceiptArgs,
    ) -> &mut Self {
        self.close_action_receipt_args = Some(close_action_receipt_args);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
        self.__remaining_accounts.push(account);
        self
    }
    /// Add additional accounts to the instruction.
    #[inline(always)]
    pub fn add_remaining_accounts(
        &mut self,
        accounts: &[solana_instruction::AccountMeta],
    ) -> &mut Self {
        self.__remaining_accounts.extend_from_slice(accounts);
        self
    }
    #[allow(clippy::clone_on_copy)]
    pub fn instruction(&self) -> solana_instruction::Instruction {
        let accounts = CloseActionReceiptAccount {
            mint: self.mint.expect("mint is not set"),
            verification_config_or_mint_authority: self
                .verification_config_or_mint_authority
                .expect("verification_config_or_mint_authority is not set"),
            instructions_sysvar_or_creator: self
                .instructions_sysvar_or_creator
                .expect("instructions_sysvar_or_creator is not set"),
            receipt_account: self.receipt_account.expect("receipt_account is not set"),
            destination: self.destination.expect("destination is not set"),
            mint_account: self.mint_account.expect("mint_account is not set"),
        };
        let args = CloseActionReceiptAccountInstructionArgs {
            close_action_receipt_args: self
                .close_action_receipt_args
                .clone()
                .expect("close_action_receipt_args is not set"),
        };

        accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
    }
}

/// `close_action_receipt_account` CPI accounts.
pub struct CloseActionReceiptAccountCpiAccounts<'a, 'b> {
    pub mint: &'b solana_account_info::AccountInfo<'a>,

    pub verification_config_or_mint_authority: &'b solana_account_info::AccountInfo<'a>,

    pub instructions_sysvar_or_creator: &'b solana_account_info::AccountInfo<'a>,

    pub receipt_account: &'b solana_account_info::AccountInfo<'a>,

    pub destination: &'b solana_account_info::AccountInfo<'a>,

    pub mint_account: &'b solana_account_info::AccountInfo<'a>,
}

/// `close_action_receipt_account` CPI instruction.
pub struct CloseActionReceiptAccountCpi<'a, 'b> {
    /// The program to invoke.
    pub __program: &'b solana_account_info::AccountInfo<'a>,

    pub mint: &'b solana_account_info::AccountInfo<'a>,

    pub verification_config_or_mint_authority: &'b solana_account_info::AccountInfo<'a>,

    pub instructions_sysvar_or_creator: &'b solana_account_info::AccountInfo<'a>,

    pub receipt_account: &'b solana_account_info::AccountInfo<'a>,

    pub destination: &'b solana_account_info::AccountInfo<'a>,

    pub mint_account: &'b solana_account_info::AccountInfo<'a>,
    /// The arguments for the instruction.
    pub __args: CloseActionReceiptAccountInstructionArgs,
}

impl<'a, 'b> CloseActionReceiptAccountCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: CloseActionReceiptAccountCpiAccounts<'a, 'b>,
        args: CloseActionReceiptAccountInstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            mint: accounts.mint,
            verification_config_or_mint_authority: accounts.verification_config_or_mint_authority,
            instructions_sysvar_or_creator: accounts.instructions_sysvar_or_creator,
            receipt_account: accounts.receipt_account,
            destination: accounts.destination,
            mint_account: accounts.mint_account,
            __args: args,
        }
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_error::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], &[])
    }
    #[inline(always)]
    pub fn invoke_with_remaining_accounts(
        &self,
        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> solana_program_error::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
    }
    #[inline(always)]
    pub fn invoke_signed(&self, signers_seeds: &[&[&[u8]]]) -> solana_program_error::ProgramResult {
        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
    }
    #[allow(clippy::arithmetic_side_effects)]
    #[allow(clippy::clone_on_copy)]
    #[allow(clippy::vec_init_then_push)]
    pub fn invoke_signed_with_remaining_accounts(
        &self,
        signers_seeds: &[&[&[u8]]],
        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> solana_program_error::ProgramResult {
        let mut accounts = Vec::with_capacity(6 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.mint.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.verification_config_or_mint_authority.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.instructions_sysvar_or_creator.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.receipt_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.destination.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.mint_account.key,
            false,
        ));
        remaining_accounts.iter().for_each(|remaining_account| {
            accounts.push(solana_instruction::AccountMeta {
                pubkey: *remaining_account.0.key,
                is_signer: remaining_account.1,
                is_writable: remaining_account.2,
            })
        });
        let mut data = borsh::to_vec(&CloseActionReceiptAccountInstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&self.__args).unwrap();
        data.append(&mut args);

        let instruction = solana_instruction::Instruction {
            program_id: crate::SECURITY_TOKEN_PROGRAM_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(7 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.mint.clone());
        account_infos.push(self.verification_config_or_mint_authority.clone());
        account_infos.push(self.instructions_sysvar_or_creator.clone());
        account_infos.push(self.receipt_account.clone());
        account_infos.push(self.destination.clone());
        account_infos.push(self.mint_account.clone());
        remaining_accounts
            .iter()
            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));

        if signers_seeds.is_empty() {
            solana_cpi::invoke(&instruction, &account_infos)
        } else {
            solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
        }
    }
}

/// Instruction builder for `CloseActionReceiptAccount` via CPI.
///
/// ### Accounts:
///
///   0. `[]` mint
///   1. `[]` verification_config_or_mint_authority
///   2. `[]` instructions_sysvar_or_creator
///   3. `[writable]` receipt_account
///   4. `[writable]` destination
///   5. `[]` mint_account
#[derive(Clone, Debug)]
pub struct CloseActionReceiptAccountCpiBuilder<'a, 'b> {
    instruction: Box<CloseActionReceiptAccountCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> CloseActionReceiptAccountCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(CloseActionReceiptAccountCpiBuilderInstruction {
            __program: program,
            mint: None,
            verification_config_or_mint_authority: None,
            instructions_sysvar_or_creator: None,
            receipt_account: None,
            destination: None,
            mint_account: None,
            close_action_receipt_args: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    #[inline(always)]
    pub fn mint(&mut self, mint: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.mint = Some(mint);
        self
    }
    #[inline(always)]
    pub fn verification_config_or_mint_authority(
        &mut self,
        verification_config_or_mint_authority: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.verification_config_or_mint_authority =
            Some(verification_config_or_mint_authority);
        self
    }
    #[inline(always)]
    pub fn instructions_sysvar_or_creator(
        &mut self,
        instructions_sysvar_or_creator: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.instructions_sysvar_or_creator = Some(instructions_sysvar_or_creator);
        self
    }
    #[inline(always)]
    pub fn receipt_account(
        &mut self,
        receipt_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.receipt_account = Some(receipt_account);
        self
    }
    #[inline(always)]
    pub fn destination(
        &mut self,
        destination: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.destination = Some(destination);
        self
    }
    #[inline(always)]
    pub fn mint_account(
        &mut self,
        mint_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.mint_account = Some(mint_account);
        self
    }
    #[inline(always)]
    pub fn close_action_receipt_args(
        &mut self,
        close_action_receipt_args: CloseActionReceiptArgs,
    ) -> &mut Self {
        self.instruction.close_action_receipt_args = Some(close_action_receipt_args);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(
        &mut self,
        account: &'b solana_account_info::AccountInfo<'a>,
        is_writable: bool,
        is_signer: bool,
    ) -> &mut Self {
        self.instruction
            .__remaining_accounts
            .push((account, is_writable, is_signer));
        self
    }
    /// Add additional accounts to the instruction.
    ///
    /// Each account is represented by a tuple of the `AccountInfo`, a `bool` indicating whether the account is writable or not,
    /// and a `bool` indicating whether the account is a signer or not.
    #[inline(always)]
    pub fn add_remaining_accounts(
        &mut self,
        accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
    ) -> &mut Self {
        self.instruction
            .__remaining_accounts
            .extend_from_slice(accounts);
        self
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program_error::ProgramResult {
        self.invoke_signed(&[])
    }
    #[allow(clippy::clone_on_copy)]
    #[allow(clippy::vec_init_then_push)]
    pub fn invoke_signed(&self, signers_seeds: &[&[&[u8]]]) -> solana_program_error::ProgramResult {
        let args = CloseActionReceiptAccountInstructionArgs {
            close_action_receipt_args: self
                .instruction
                .close_action_receipt_args
                .clone()
                .expect("close_action_receipt_args is not set"),
        };
        let instruction = CloseActionReceiptAccountCpi {
            __program: self.instruction.__program,

            mint: self.instruction.mint.expect("mint is not set"),

            verification_config_or_mint_authority: self
                .instruction
                .verification_config_or_mint_authority
                .expect("verification_config_or_mint_authority is not set"),

            instructions_sysvar_or_creator: self
                .instruction
                .instructions_sysvar_or_creator
                .expect("instructions_sysvar_or_creator is not set"),

            receipt_account: self
                .instruction
                .receipt_account
                .expect("receipt_account is not set"),

            destination: self
                .instruction
                .destination
                .expect("destination is not set"),

            mint_account: self
                .instruction
                .mint_account
                .expect("mint_account is not set"),
            __args: args,
        };
        instruction.invoke_signed_with_remaining_accounts(
            signers_seeds,
            &self.instruction.__remaining_accounts,
        )
    }
}

#[derive(Clone, Debug)]
struct CloseActionReceiptAccountCpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    verification_config_or_mint_authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    instructions_sysvar_or_creator: Option<&'b solana_account_info::AccountInfo<'a>>,
    receipt_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    destination: Option<&'b solana_account_info::AccountInfo<'a>>,
    mint_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    close_action_receipt_args: Option<CloseActionReceiptArgs>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
