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

pub const INITIALIZE_MINT_DISCRIMINATOR: u8 = 0;

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

    pub authority: solana_pubkey::Pubkey,

    pub payer: solana_pubkey::Pubkey,

    pub token_program: solana_pubkey::Pubkey,

    pub system_program: solana_pubkey::Pubkey,

    pub rent_sysvar: solana_pubkey::Pubkey,
}

impl InitializeMint {
    pub fn instruction(
        &self,
        args: InitializeMintInstructionArgs,
    ) -> 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: InitializeMintInstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(6 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new(self.mint, true));
        accounts.push(solana_instruction::AccountMeta::new(self.authority, false));
        accounts.push(solana_instruction::AccountMeta::new(self.payer, true));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.token_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.system_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.rent_sysvar,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&InitializeMintInstructionData::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 InitializeMintInstructionData {
    discriminator: u8,
}

impl InitializeMintInstructionData {
    pub fn new() -> Self {
        Self { discriminator: 0 }
    }
}

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

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

/// Instruction builder for `InitializeMint`.
///
/// ### Accounts:
///
///   0. `[writable, signer]` mint
///   1. `[writable]` authority
///   2. `[writable, signer]` payer
///   3. `[optional]` token_program (default to `TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`)
///   4. `[optional]` system_program (default to `11111111111111111111111111111111`)
///   5. `[optional]` rent_sysvar (default to `SysvarRent111111111111111111111111111111111`)
#[derive(Clone, Debug, Default)]
pub struct InitializeMintBuilder {
    mint: Option<solana_pubkey::Pubkey>,
    authority: Option<solana_pubkey::Pubkey>,
    payer: Option<solana_pubkey::Pubkey>,
    token_program: Option<solana_pubkey::Pubkey>,
    system_program: Option<solana_pubkey::Pubkey>,
    rent_sysvar: Option<solana_pubkey::Pubkey>,
    initialize_mint_args: Option<InitializeMintArgs>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl InitializeMintBuilder {
    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 authority(&mut self, authority: solana_pubkey::Pubkey) -> &mut Self {
        self.authority = Some(authority);
        self
    }
    #[inline(always)]
    pub fn payer(&mut self, payer: solana_pubkey::Pubkey) -> &mut Self {
        self.payer = Some(payer);
        self
    }
    /// `[optional account, default to 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb']`
    #[inline(always)]
    pub fn token_program(&mut self, token_program: solana_pubkey::Pubkey) -> &mut Self {
        self.token_program = Some(token_program);
        self
    }
    /// `[optional account, default to '11111111111111111111111111111111']`
    #[inline(always)]
    pub fn system_program(&mut self, system_program: solana_pubkey::Pubkey) -> &mut Self {
        self.system_program = Some(system_program);
        self
    }
    /// `[optional account, default to 'SysvarRent111111111111111111111111111111111']`
    #[inline(always)]
    pub fn rent_sysvar(&mut self, rent_sysvar: solana_pubkey::Pubkey) -> &mut Self {
        self.rent_sysvar = Some(rent_sysvar);
        self
    }
    #[inline(always)]
    pub fn initialize_mint_args(&mut self, initialize_mint_args: InitializeMintArgs) -> &mut Self {
        self.initialize_mint_args = Some(initialize_mint_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 = InitializeMint {
            mint: self.mint.expect("mint is not set"),
            authority: self.authority.expect("authority is not set"),
            payer: self.payer.expect("payer is not set"),
            token_program: self.token_program.unwrap_or(solana_pubkey::pubkey!(
                "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
            )),
            system_program: self
                .system_program
                .unwrap_or(solana_pubkey::pubkey!("11111111111111111111111111111111")),
            rent_sysvar: self.rent_sysvar.unwrap_or(solana_pubkey::pubkey!(
                "SysvarRent111111111111111111111111111111111"
            )),
        };
        let args = InitializeMintInstructionArgs {
            initialize_mint_args: self
                .initialize_mint_args
                .clone()
                .expect("initialize_mint_args is not set"),
        };

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl<'a, 'b> InitializeMintCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: InitializeMintCpiAccounts<'a, 'b>,
        args: InitializeMintInstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            mint: accounts.mint,
            authority: accounts.authority,
            payer: accounts.payer,
            token_program: accounts.token_program,
            system_program: accounts.system_program,
            rent_sysvar: accounts.rent_sysvar,
            __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(*self.mint.key, true));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.authority.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new(*self.payer.key, true));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.token_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.system_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.rent_sysvar.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(&InitializeMintInstructionData::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.authority.clone());
        account_infos.push(self.payer.clone());
        account_infos.push(self.token_program.clone());
        account_infos.push(self.system_program.clone());
        account_infos.push(self.rent_sysvar.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 `InitializeMint` via CPI.
///
/// ### Accounts:
///
///   0. `[writable, signer]` mint
///   1. `[writable]` authority
///   2. `[writable, signer]` payer
///   3. `[]` token_program
///   4. `[]` system_program
///   5. `[]` rent_sysvar
#[derive(Clone, Debug)]
pub struct InitializeMintCpiBuilder<'a, 'b> {
    instruction: Box<InitializeMintCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> InitializeMintCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(InitializeMintCpiBuilderInstruction {
            __program: program,
            mint: None,
            authority: None,
            payer: None,
            token_program: None,
            system_program: None,
            rent_sysvar: None,
            initialize_mint_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 authority(&mut self, authority: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.authority = Some(authority);
        self
    }
    #[inline(always)]
    pub fn payer(&mut self, payer: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.payer = Some(payer);
        self
    }
    #[inline(always)]
    pub fn token_program(
        &mut self,
        token_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.token_program = Some(token_program);
        self
    }
    #[inline(always)]
    pub fn system_program(
        &mut self,
        system_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.system_program = Some(system_program);
        self
    }
    #[inline(always)]
    pub fn rent_sysvar(
        &mut self,
        rent_sysvar: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.rent_sysvar = Some(rent_sysvar);
        self
    }
    #[inline(always)]
    pub fn initialize_mint_args(&mut self, initialize_mint_args: InitializeMintArgs) -> &mut Self {
        self.instruction.initialize_mint_args = Some(initialize_mint_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 = InitializeMintInstructionArgs {
            initialize_mint_args: self
                .instruction
                .initialize_mint_args
                .clone()
                .expect("initialize_mint_args is not set"),
        };
        let instruction = InitializeMintCpi {
            __program: self.instruction.__program,

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

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

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

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

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

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

#[derive(Clone, Debug)]
struct InitializeMintCpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    payer: Option<&'b solana_account_info::AccountInfo<'a>>,
    token_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    system_program: Option<&'b solana_account_info::AccountInfo<'a>>,
    rent_sysvar: Option<&'b solana_account_info::AccountInfo<'a>>,
    initialize_mint_args: Option<InitializeMintArgs>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
