//! 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::CloseRateArgs;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

pub const CLOSE_RATE_ACCOUNT_DISCRIMINATOR: u8 = 15;

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

    pub verification_config_or_mint_authority: solana_pubkey::Pubkey,

    pub instructions_sysvar_or_creator: solana_pubkey::Pubkey,

    pub rate_account: solana_pubkey::Pubkey,

    pub destination: solana_pubkey::Pubkey,

    pub mint_from: solana_pubkey::Pubkey,

    pub mint_to: solana_pubkey::Pubkey,
}

impl CloseRateAccount {
    pub fn instruction(
        &self,
        args: CloseRateAccountInstructionArgs,
    ) -> 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: CloseRateAccountInstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(7 + 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.rate_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            self.destination,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.mint_from,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.mint_to,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&CloseRateAccountInstructionData::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 CloseRateAccountInstructionData {
    discriminator: u8,
}

impl CloseRateAccountInstructionData {
    pub fn new() -> Self {
        Self { discriminator: 15 }
    }
}

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

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

/// Instruction builder for `CloseRateAccount`.
///
/// ### Accounts:
///
///   0. `[]` mint
///   1. `[]` verification_config_or_mint_authority
///   2. `[]` instructions_sysvar_or_creator
///   3. `[writable]` rate_account
///   4. `[writable]` destination
///   5. `[]` mint_from
///   6. `[]` mint_to
#[derive(Clone, Debug, Default)]
pub struct CloseRateAccountBuilder {
    mint: Option<solana_pubkey::Pubkey>,
    verification_config_or_mint_authority: Option<solana_pubkey::Pubkey>,
    instructions_sysvar_or_creator: Option<solana_pubkey::Pubkey>,
    rate_account: Option<solana_pubkey::Pubkey>,
    destination: Option<solana_pubkey::Pubkey>,
    mint_from: Option<solana_pubkey::Pubkey>,
    mint_to: Option<solana_pubkey::Pubkey>,
    close_rate_args: Option<CloseRateArgs>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl CloseRateAccountBuilder {
    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 rate_account(&mut self, rate_account: solana_pubkey::Pubkey) -> &mut Self {
        self.rate_account = Some(rate_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_from(&mut self, mint_from: solana_pubkey::Pubkey) -> &mut Self {
        self.mint_from = Some(mint_from);
        self
    }
    #[inline(always)]
    pub fn mint_to(&mut self, mint_to: solana_pubkey::Pubkey) -> &mut Self {
        self.mint_to = Some(mint_to);
        self
    }
    #[inline(always)]
    pub fn close_rate_args(&mut self, close_rate_args: CloseRateArgs) -> &mut Self {
        self.close_rate_args = Some(close_rate_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 = CloseRateAccount {
            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"),
            rate_account: self.rate_account.expect("rate_account is not set"),
            destination: self.destination.expect("destination is not set"),
            mint_from: self.mint_from.expect("mint_from is not set"),
            mint_to: self.mint_to.expect("mint_to is not set"),
        };
        let args = CloseRateAccountInstructionArgs {
            close_rate_args: self
                .close_rate_args
                .clone()
                .expect("close_rate_args is not set"),
        };

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

/// `close_rate_account` CPI accounts.
pub struct CloseRateAccountCpiAccounts<'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 rate_account: &'b solana_account_info::AccountInfo<'a>,

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

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

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

/// `close_rate_account` CPI instruction.
pub struct CloseRateAccountCpi<'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 rate_account: &'b solana_account_info::AccountInfo<'a>,

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

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

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

impl<'a, 'b> CloseRateAccountCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: CloseRateAccountCpiAccounts<'a, 'b>,
        args: CloseRateAccountInstructionArgs,
    ) -> 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,
            rate_account: accounts.rate_account,
            destination: accounts.destination,
            mint_from: accounts.mint_from,
            mint_to: accounts.mint_to,
            __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(7 + 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.rate_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.destination.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.mint_from.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.mint_to.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(&CloseRateAccountInstructionData::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(8 + 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.rate_account.clone());
        account_infos.push(self.destination.clone());
        account_infos.push(self.mint_from.clone());
        account_infos.push(self.mint_to.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 `CloseRateAccount` via CPI.
///
/// ### Accounts:
///
///   0. `[]` mint
///   1. `[]` verification_config_or_mint_authority
///   2. `[]` instructions_sysvar_or_creator
///   3. `[writable]` rate_account
///   4. `[writable]` destination
///   5. `[]` mint_from
///   6. `[]` mint_to
#[derive(Clone, Debug)]
pub struct CloseRateAccountCpiBuilder<'a, 'b> {
    instruction: Box<CloseRateAccountCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> CloseRateAccountCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(CloseRateAccountCpiBuilderInstruction {
            __program: program,
            mint: None,
            verification_config_or_mint_authority: None,
            instructions_sysvar_or_creator: None,
            rate_account: None,
            destination: None,
            mint_from: None,
            mint_to: None,
            close_rate_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 rate_account(
        &mut self,
        rate_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.rate_account = Some(rate_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_from(&mut self, mint_from: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.mint_from = Some(mint_from);
        self
    }
    #[inline(always)]
    pub fn mint_to(&mut self, mint_to: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.mint_to = Some(mint_to);
        self
    }
    #[inline(always)]
    pub fn close_rate_args(&mut self, close_rate_args: CloseRateArgs) -> &mut Self {
        self.instruction.close_rate_args = Some(close_rate_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 = CloseRateAccountInstructionArgs {
            close_rate_args: self
                .instruction
                .close_rate_args
                .clone()
                .expect("close_rate_args is not set"),
        };
        let instruction = CloseRateAccountCpi {
            __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"),

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

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

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

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

#[derive(Clone, Debug)]
struct CloseRateAccountCpiBuilderInstruction<'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>>,
    rate_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    destination: Option<&'b solana_account_info::AccountInfo<'a>>,
    mint_from: Option<&'b solana_account_info::AccountInfo<'a>>,
    mint_to: Option<&'b solana_account_info::AccountInfo<'a>>,
    close_rate_args: Option<CloseRateArgs>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
