//! This code was AUTOGENERATED using the kinobi library.
//! Please DO NOT EDIT THIS FILE, instead use visitors
//! to add features, then rerun kinobi to update it.
//!
//! [https://github.com/metaplex-foundation/kinobi]
//!

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

/// Accounts.
pub struct InitializeConfig {
    pub admin_wallet: solana_program::pubkey::Pubkey,

    pub config_account: solana_program::pubkey::Pubkey,

    pub config_token_account: solana_program::pubkey::Pubkey,

    pub mint_account: solana_program::pubkey::Pubkey,

    pub metadata_account: solana_program::pubkey::Pubkey,

    pub master_edition_account: solana_program::pubkey::Pubkey,

    pub token_program: solana_program::pubkey::Pubkey,

    pub associated_token_program: solana_program::pubkey::Pubkey,

    pub metadata_program: solana_program::pubkey::Pubkey,

    pub system_program: solana_program::pubkey::Pubkey,
}

impl InitializeConfig {
    pub fn instruction(
        &self,
        args: InitializeConfigInstructionArgs,
    ) -> solana_program::instruction::Instruction {
        self.instruction_with_remaining_accounts(args, &[])
    }
    #[allow(clippy::vec_init_then_push)]
    pub fn instruction_with_remaining_accounts(
        &self,
        args: InitializeConfigInstructionArgs,
        remaining_accounts: &[solana_program::instruction::AccountMeta],
    ) -> solana_program::instruction::Instruction {
        let mut accounts = Vec::with_capacity(10 + remaining_accounts.len());
        accounts.push(solana_program::instruction::AccountMeta::new(
            self.admin_wallet,
            true,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            self.config_account,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            self.config_token_account,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            self.mint_account,
            true,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            self.metadata_account,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            self.master_edition_account,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            self.token_program,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            self.associated_token_program,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            self.metadata_program,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            self.system_program,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = InitializeConfigInstructionData::new().try_to_vec().unwrap();
        let mut args = args.try_to_vec().unwrap();
        data.append(&mut args);

        solana_program::instruction::Instruction {
            program_id: crate::STABLEBOND_ID,
            accounts,
            data,
        }
    }
}

#[derive(BorshDeserialize, BorshSerialize)]
struct InitializeConfigInstructionData {
    discriminator: u8,
}

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

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

/// Instruction builder for `InitializeConfig`.
///
/// ### Accounts:
///
///   0. `[writable, signer]` admin_wallet
///   1. `[writable]` config_account
///   2. `[writable]` config_token_account
///   3. `[writable, signer]` mint_account
///   4. `[writable]` metadata_account
///   5. `[writable]` master_edition_account
///   6. `[optional]` token_program (default to `TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`)
///   7. `[]` associated_token_program
///   8. `[]` metadata_program
///   9. `[optional]` system_program (default to `11111111111111111111111111111111`)
#[derive(Default)]
pub struct InitializeConfigBuilder {
    admin_wallet: Option<solana_program::pubkey::Pubkey>,
    config_account: Option<solana_program::pubkey::Pubkey>,
    config_token_account: Option<solana_program::pubkey::Pubkey>,
    mint_account: Option<solana_program::pubkey::Pubkey>,
    metadata_account: Option<solana_program::pubkey::Pubkey>,
    master_edition_account: Option<solana_program::pubkey::Pubkey>,
    token_program: Option<solana_program::pubkey::Pubkey>,
    associated_token_program: Option<solana_program::pubkey::Pubkey>,
    metadata_program: Option<solana_program::pubkey::Pubkey>,
    system_program: Option<solana_program::pubkey::Pubkey>,
    oracle: Option<OracleSetup>,
    __remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
}

impl InitializeConfigBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    #[inline(always)]
    pub fn admin_wallet(&mut self, admin_wallet: solana_program::pubkey::Pubkey) -> &mut Self {
        self.admin_wallet = Some(admin_wallet);
        self
    }
    #[inline(always)]
    pub fn config_account(&mut self, config_account: solana_program::pubkey::Pubkey) -> &mut Self {
        self.config_account = Some(config_account);
        self
    }
    #[inline(always)]
    pub fn config_token_account(
        &mut self,
        config_token_account: solana_program::pubkey::Pubkey,
    ) -> &mut Self {
        self.config_token_account = Some(config_token_account);
        self
    }
    #[inline(always)]
    pub fn mint_account(&mut self, mint_account: solana_program::pubkey::Pubkey) -> &mut Self {
        self.mint_account = Some(mint_account);
        self
    }
    #[inline(always)]
    pub fn metadata_account(
        &mut self,
        metadata_account: solana_program::pubkey::Pubkey,
    ) -> &mut Self {
        self.metadata_account = Some(metadata_account);
        self
    }
    #[inline(always)]
    pub fn master_edition_account(
        &mut self,
        master_edition_account: solana_program::pubkey::Pubkey,
    ) -> &mut Self {
        self.master_edition_account = Some(master_edition_account);
        self
    }
    /// `[optional account, default to 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA']`
    #[inline(always)]
    pub fn token_program(&mut self, token_program: solana_program::pubkey::Pubkey) -> &mut Self {
        self.token_program = Some(token_program);
        self
    }
    #[inline(always)]
    pub fn associated_token_program(
        &mut self,
        associated_token_program: solana_program::pubkey::Pubkey,
    ) -> &mut Self {
        self.associated_token_program = Some(associated_token_program);
        self
    }
    #[inline(always)]
    pub fn metadata_program(
        &mut self,
        metadata_program: solana_program::pubkey::Pubkey,
    ) -> &mut Self {
        self.metadata_program = Some(metadata_program);
        self
    }
    /// `[optional account, default to '11111111111111111111111111111111']`
    #[inline(always)]
    pub fn system_program(&mut self, system_program: solana_program::pubkey::Pubkey) -> &mut Self {
        self.system_program = Some(system_program);
        self
    }
    #[inline(always)]
    pub fn oracle(&mut self, oracle: OracleSetup) -> &mut Self {
        self.oracle = Some(oracle);
        self
    }
    /// Add an aditional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(
        &mut self,
        account: solana_program::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_program::instruction::AccountMeta],
    ) -> &mut Self {
        self.__remaining_accounts.extend_from_slice(accounts);
        self
    }
    #[allow(clippy::clone_on_copy)]
    pub fn instruction(&self) -> solana_program::instruction::Instruction {
        let accounts = InitializeConfig {
            admin_wallet: self.admin_wallet.expect("admin_wallet is not set"),
            config_account: self.config_account.expect("config_account is not set"),
            config_token_account: self
                .config_token_account
                .expect("config_token_account is not set"),
            mint_account: self.mint_account.expect("mint_account is not set"),
            metadata_account: self.metadata_account.expect("metadata_account is not set"),
            master_edition_account: self
                .master_edition_account
                .expect("master_edition_account is not set"),
            token_program: self.token_program.unwrap_or(solana_program::pubkey!(
                "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
            )),
            associated_token_program: self
                .associated_token_program
                .expect("associated_token_program is not set"),
            metadata_program: self.metadata_program.expect("metadata_program is not set"),
            system_program: self
                .system_program
                .unwrap_or(solana_program::pubkey!("11111111111111111111111111111111")),
        };
        let args = InitializeConfigInstructionArgs {
            oracle: self.oracle.clone().expect("oracle is not set"),
        };

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

/// `initialize_config` CPI accounts.
pub struct InitializeConfigCpiAccounts<'a, 'b> {
    pub admin_wallet: &'b solana_program::account_info::AccountInfo<'a>,

    pub config_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub config_token_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub mint_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub metadata_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub master_edition_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub token_program: &'b solana_program::account_info::AccountInfo<'a>,

    pub associated_token_program: &'b solana_program::account_info::AccountInfo<'a>,

    pub metadata_program: &'b solana_program::account_info::AccountInfo<'a>,

    pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
}

/// `initialize_config` CPI instruction.
pub struct InitializeConfigCpi<'a, 'b> {
    /// The program to invoke.
    pub __program: &'b solana_program::account_info::AccountInfo<'a>,

    pub admin_wallet: &'b solana_program::account_info::AccountInfo<'a>,

    pub config_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub config_token_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub mint_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub metadata_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub master_edition_account: &'b solana_program::account_info::AccountInfo<'a>,

    pub token_program: &'b solana_program::account_info::AccountInfo<'a>,

    pub associated_token_program: &'b solana_program::account_info::AccountInfo<'a>,

    pub metadata_program: &'b solana_program::account_info::AccountInfo<'a>,

    pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
    /// The arguments for the instruction.
    pub __args: InitializeConfigInstructionArgs,
}

impl<'a, 'b> InitializeConfigCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_program::account_info::AccountInfo<'a>,
        accounts: InitializeConfigCpiAccounts<'a, 'b>,
        args: InitializeConfigInstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            admin_wallet: accounts.admin_wallet,
            config_account: accounts.config_account,
            config_token_account: accounts.config_token_account,
            mint_account: accounts.mint_account,
            metadata_account: accounts.metadata_account,
            master_edition_account: accounts.master_edition_account,
            token_program: accounts.token_program,
            associated_token_program: accounts.associated_token_program,
            metadata_program: accounts.metadata_program,
            system_program: accounts.system_program,
            __args: args,
        }
    }
    #[inline(always)]
    pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], &[])
    }
    #[inline(always)]
    pub fn invoke_with_remaining_accounts(
        &self,
        remaining_accounts: &[(
            &'b solana_program::account_info::AccountInfo<'a>,
            bool,
            bool,
        )],
    ) -> solana_program::entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
    }
    #[inline(always)]
    pub fn invoke_signed(
        &self,
        signers_seeds: &[&[&[u8]]],
    ) -> solana_program::entrypoint::ProgramResult {
        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
    }
    #[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_program::account_info::AccountInfo<'a>,
            bool,
            bool,
        )],
    ) -> solana_program::entrypoint::ProgramResult {
        let mut accounts = Vec::with_capacity(10 + remaining_accounts.len());
        accounts.push(solana_program::instruction::AccountMeta::new(
            *self.admin_wallet.key,
            true,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            *self.config_account.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            *self.config_token_account.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            *self.mint_account.key,
            true,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            *self.metadata_account.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new(
            *self.master_edition_account.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            *self.token_program.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            *self.associated_token_program.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            *self.metadata_program.key,
            false,
        ));
        accounts.push(solana_program::instruction::AccountMeta::new_readonly(
            *self.system_program.key,
            false,
        ));
        remaining_accounts.iter().for_each(|remaining_account| {
            accounts.push(solana_program::instruction::AccountMeta {
                pubkey: *remaining_account.0.key,
                is_signer: remaining_account.1,
                is_writable: remaining_account.2,
            })
        });
        let mut data = InitializeConfigInstructionData::new().try_to_vec().unwrap();
        let mut args = self.__args.try_to_vec().unwrap();
        data.append(&mut args);

        let instruction = solana_program::instruction::Instruction {
            program_id: crate::STABLEBOND_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(10 + 1 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.admin_wallet.clone());
        account_infos.push(self.config_account.clone());
        account_infos.push(self.config_token_account.clone());
        account_infos.push(self.mint_account.clone());
        account_infos.push(self.metadata_account.clone());
        account_infos.push(self.master_edition_account.clone());
        account_infos.push(self.token_program.clone());
        account_infos.push(self.associated_token_program.clone());
        account_infos.push(self.metadata_program.clone());
        account_infos.push(self.system_program.clone());
        remaining_accounts
            .iter()
            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));

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

/// Instruction builder for `InitializeConfig` via CPI.
///
/// ### Accounts:
///
///   0. `[writable, signer]` admin_wallet
///   1. `[writable]` config_account
///   2. `[writable]` config_token_account
///   3. `[writable, signer]` mint_account
///   4. `[writable]` metadata_account
///   5. `[writable]` master_edition_account
///   6. `[]` token_program
///   7. `[]` associated_token_program
///   8. `[]` metadata_program
///   9. `[]` system_program
pub struct InitializeConfigCpiBuilder<'a, 'b> {
    instruction: Box<InitializeConfigCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> InitializeConfigCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(InitializeConfigCpiBuilderInstruction {
            __program: program,
            admin_wallet: None,
            config_account: None,
            config_token_account: None,
            mint_account: None,
            metadata_account: None,
            master_edition_account: None,
            token_program: None,
            associated_token_program: None,
            metadata_program: None,
            system_program: None,
            oracle: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    #[inline(always)]
    pub fn admin_wallet(
        &mut self,
        admin_wallet: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.admin_wallet = Some(admin_wallet);
        self
    }
    #[inline(always)]
    pub fn config_account(
        &mut self,
        config_account: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.config_account = Some(config_account);
        self
    }
    #[inline(always)]
    pub fn config_token_account(
        &mut self,
        config_token_account: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.config_token_account = Some(config_token_account);
        self
    }
    #[inline(always)]
    pub fn mint_account(
        &mut self,
        mint_account: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.mint_account = Some(mint_account);
        self
    }
    #[inline(always)]
    pub fn metadata_account(
        &mut self,
        metadata_account: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.metadata_account = Some(metadata_account);
        self
    }
    #[inline(always)]
    pub fn master_edition_account(
        &mut self,
        master_edition_account: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.master_edition_account = Some(master_edition_account);
        self
    }
    #[inline(always)]
    pub fn token_program(
        &mut self,
        token_program: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.token_program = Some(token_program);
        self
    }
    #[inline(always)]
    pub fn associated_token_program(
        &mut self,
        associated_token_program: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.associated_token_program = Some(associated_token_program);
        self
    }
    #[inline(always)]
    pub fn metadata_program(
        &mut self,
        metadata_program: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.metadata_program = Some(metadata_program);
        self
    }
    #[inline(always)]
    pub fn system_program(
        &mut self,
        system_program: &'b solana_program::account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.system_program = Some(system_program);
        self
    }
    #[inline(always)]
    pub fn oracle(&mut self, oracle: OracleSetup) -> &mut Self {
        self.instruction.oracle = Some(oracle);
        self
    }
    /// Add an additional account to the instruction.
    #[inline(always)]
    pub fn add_remaining_account(
        &mut self,
        account: &'b solana_program::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_program::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::entrypoint::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::entrypoint::ProgramResult {
        let args = InitializeConfigInstructionArgs {
            oracle: self.instruction.oracle.clone().expect("oracle is not set"),
        };
        let instruction = InitializeConfigCpi {
            __program: self.instruction.__program,

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

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

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

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

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

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

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

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

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

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

struct InitializeConfigCpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_program::account_info::AccountInfo<'a>,
    admin_wallet: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    config_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    config_token_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    mint_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    metadata_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    master_edition_account: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    token_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    associated_token_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    metadata_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    system_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
    oracle: Option<OracleSetup>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(
        &'b solana_program::account_info::AccountInfo<'a>,
        bool,
        bool,
    )>,
}
