//! 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::SwapInstructionBaseIn;
use crate::generated::types::SwapInstructionBaseOut;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

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

    pub amm_authority: solana_pubkey::Pubkey,

    pub amm_open_orders: solana_pubkey::Pubkey,

    pub pool_coin_token_account: solana_pubkey::Pubkey,

    pub pool_pc_token_account: solana_pubkey::Pubkey,

    pub lp_mint_address: solana_pubkey::Pubkey,

    pub serum_market: solana_pubkey::Pubkey,

    pub serum_event_queue: solana_pubkey::Pubkey,
}

impl SimulateInfo {
    pub fn instruction(
        &self,
        args: SimulateInfoInstructionArgs,
    ) -> 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: SimulateInfoInstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(8 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.amm, false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.amm_authority,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.amm_open_orders,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.pool_coin_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.pool_pc_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.lp_mint_address,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.serum_market,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.serum_event_queue,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&SimulateInfoInstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&args).unwrap();
        data.append(&mut args);

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

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

impl SimulateInfoInstructionData {
    pub fn new() -> Self {
        Self {
            discriminator: [12],
        }
    }
}

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

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SimulateInfoInstructionArgs {
    pub param: u8,
    pub swap_base_in_value: Option<SwapInstructionBaseIn>,
    pub swap_base_out_value: Option<SwapInstructionBaseOut>,
}

/// Instruction builder for `SimulateInfo`.
///
/// ### Accounts:
///
///   0. `[]` amm
///   1. `[]` amm_authority
///   2. `[]` amm_open_orders
///   3. `[]` pool_coin_token_account
///   4. `[]` pool_pc_token_account
///   5. `[]` lp_mint_address
///   6. `[]` serum_market
///   7. `[]` serum_event_queue
#[derive(Clone, Debug, Default)]
pub struct SimulateInfoBuilder {
    amm: Option<solana_pubkey::Pubkey>,
    amm_authority: Option<solana_pubkey::Pubkey>,
    amm_open_orders: Option<solana_pubkey::Pubkey>,
    pool_coin_token_account: Option<solana_pubkey::Pubkey>,
    pool_pc_token_account: Option<solana_pubkey::Pubkey>,
    lp_mint_address: Option<solana_pubkey::Pubkey>,
    serum_market: Option<solana_pubkey::Pubkey>,
    serum_event_queue: Option<solana_pubkey::Pubkey>,
    param: Option<u8>,
    swap_base_in_value: Option<SwapInstructionBaseIn>,
    swap_base_out_value: Option<SwapInstructionBaseOut>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl SimulateInfoBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    #[inline(always)]
    pub fn amm(&mut self, amm: solana_pubkey::Pubkey) -> &mut Self {
        self.amm = Some(amm);
        self
    }
    #[inline(always)]
    pub fn amm_authority(&mut self, amm_authority: solana_pubkey::Pubkey) -> &mut Self {
        self.amm_authority = Some(amm_authority);
        self
    }
    #[inline(always)]
    pub fn amm_open_orders(&mut self, amm_open_orders: solana_pubkey::Pubkey) -> &mut Self {
        self.amm_open_orders = Some(amm_open_orders);
        self
    }
    #[inline(always)]
    pub fn pool_coin_token_account(
        &mut self,
        pool_coin_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.pool_coin_token_account = Some(pool_coin_token_account);
        self
    }
    #[inline(always)]
    pub fn pool_pc_token_account(
        &mut self,
        pool_pc_token_account: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.pool_pc_token_account = Some(pool_pc_token_account);
        self
    }
    #[inline(always)]
    pub fn lp_mint_address(&mut self, lp_mint_address: solana_pubkey::Pubkey) -> &mut Self {
        self.lp_mint_address = Some(lp_mint_address);
        self
    }
    #[inline(always)]
    pub fn serum_market(&mut self, serum_market: solana_pubkey::Pubkey) -> &mut Self {
        self.serum_market = Some(serum_market);
        self
    }
    #[inline(always)]
    pub fn serum_event_queue(&mut self, serum_event_queue: solana_pubkey::Pubkey) -> &mut Self {
        self.serum_event_queue = Some(serum_event_queue);
        self
    }
    #[inline(always)]
    pub fn param(&mut self, param: u8) -> &mut Self {
        self.param = Some(param);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn swap_base_in_value(&mut self, swap_base_in_value: SwapInstructionBaseIn) -> &mut Self {
        self.swap_base_in_value = Some(swap_base_in_value);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn swap_base_out_value(
        &mut self,
        swap_base_out_value: SwapInstructionBaseOut,
    ) -> &mut Self {
        self.swap_base_out_value = Some(swap_base_out_value);
        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 = SimulateInfo {
            amm: self.amm.expect("amm is not set"),
            amm_authority: self.amm_authority.expect("amm_authority is not set"),
            amm_open_orders: self.amm_open_orders.expect("amm_open_orders is not set"),
            pool_coin_token_account: self
                .pool_coin_token_account
                .expect("pool_coin_token_account is not set"),
            pool_pc_token_account: self
                .pool_pc_token_account
                .expect("pool_pc_token_account is not set"),
            lp_mint_address: self.lp_mint_address.expect("lp_mint_address is not set"),
            serum_market: self.serum_market.expect("serum_market is not set"),
            serum_event_queue: self
                .serum_event_queue
                .expect("serum_event_queue is not set"),
        };
        let args = SimulateInfoInstructionArgs {
            param: self.param.clone().expect("param is not set"),
            swap_base_in_value: self.swap_base_in_value.clone(),
            swap_base_out_value: self.swap_base_out_value.clone(),
        };

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

/// `simulate_info` CPI accounts.
pub struct SimulateInfoCpiAccounts<'a, 'b> {
    pub amm: &'b solana_account_info::AccountInfo<'a>,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl<'a, 'b> SimulateInfoCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: SimulateInfoCpiAccounts<'a, 'b>,
        args: SimulateInfoInstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            amm: accounts.amm,
            amm_authority: accounts.amm_authority,
            amm_open_orders: accounts.amm_open_orders,
            pool_coin_token_account: accounts.pool_coin_token_account,
            pool_pc_token_account: accounts.pool_pc_token_account,
            lp_mint_address: accounts.lp_mint_address,
            serum_market: accounts.serum_market,
            serum_event_queue: accounts.serum_event_queue,
            __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_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::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_entrypoint::ProgramResult {
        let mut accounts = Vec::with_capacity(8 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.amm.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.amm_authority.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.amm_open_orders.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.pool_coin_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.pool_pc_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.lp_mint_address.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.serum_market.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.serum_event_queue.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(&SimulateInfoInstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&self.__args).unwrap();
        data.append(&mut args);

        let instruction = solana_instruction::Instruction {
            program_id: crate::RAYDIUM_AMM_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(9 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.amm.clone());
        account_infos.push(self.amm_authority.clone());
        account_infos.push(self.amm_open_orders.clone());
        account_infos.push(self.pool_coin_token_account.clone());
        account_infos.push(self.pool_pc_token_account.clone());
        account_infos.push(self.lp_mint_address.clone());
        account_infos.push(self.serum_market.clone());
        account_infos.push(self.serum_event_queue.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 `SimulateInfo` via CPI.
///
/// ### Accounts:
///
///   0. `[]` amm
///   1. `[]` amm_authority
///   2. `[]` amm_open_orders
///   3. `[]` pool_coin_token_account
///   4. `[]` pool_pc_token_account
///   5. `[]` lp_mint_address
///   6. `[]` serum_market
///   7. `[]` serum_event_queue
#[derive(Clone, Debug)]
pub struct SimulateInfoCpiBuilder<'a, 'b> {
    instruction: Box<SimulateInfoCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> SimulateInfoCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(SimulateInfoCpiBuilderInstruction {
            __program: program,
            amm: None,
            amm_authority: None,
            amm_open_orders: None,
            pool_coin_token_account: None,
            pool_pc_token_account: None,
            lp_mint_address: None,
            serum_market: None,
            serum_event_queue: None,
            param: None,
            swap_base_in_value: None,
            swap_base_out_value: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    #[inline(always)]
    pub fn amm(&mut self, amm: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.amm = Some(amm);
        self
    }
    #[inline(always)]
    pub fn amm_authority(
        &mut self,
        amm_authority: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.amm_authority = Some(amm_authority);
        self
    }
    #[inline(always)]
    pub fn amm_open_orders(
        &mut self,
        amm_open_orders: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.amm_open_orders = Some(amm_open_orders);
        self
    }
    #[inline(always)]
    pub fn pool_coin_token_account(
        &mut self,
        pool_coin_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.pool_coin_token_account = Some(pool_coin_token_account);
        self
    }
    #[inline(always)]
    pub fn pool_pc_token_account(
        &mut self,
        pool_pc_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.pool_pc_token_account = Some(pool_pc_token_account);
        self
    }
    #[inline(always)]
    pub fn lp_mint_address(
        &mut self,
        lp_mint_address: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.lp_mint_address = Some(lp_mint_address);
        self
    }
    #[inline(always)]
    pub fn serum_market(
        &mut self,
        serum_market: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.serum_market = Some(serum_market);
        self
    }
    #[inline(always)]
    pub fn serum_event_queue(
        &mut self,
        serum_event_queue: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.serum_event_queue = Some(serum_event_queue);
        self
    }
    #[inline(always)]
    pub fn param(&mut self, param: u8) -> &mut Self {
        self.instruction.param = Some(param);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn swap_base_in_value(&mut self, swap_base_in_value: SwapInstructionBaseIn) -> &mut Self {
        self.instruction.swap_base_in_value = Some(swap_base_in_value);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn swap_base_out_value(
        &mut self,
        swap_base_out_value: SwapInstructionBaseOut,
    ) -> &mut Self {
        self.instruction.swap_base_out_value = Some(swap_base_out_value);
        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_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 = SimulateInfoInstructionArgs {
            param: self.instruction.param.clone().expect("param is not set"),
            swap_base_in_value: self.instruction.swap_base_in_value.clone(),
            swap_base_out_value: self.instruction.swap_base_out_value.clone(),
        };
        let instruction = SimulateInfoCpi {
            __program: self.instruction.__program,

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

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

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

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

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

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

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

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

#[derive(Clone, Debug)]
struct SimulateInfoCpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    amm: Option<&'b solana_account_info::AccountInfo<'a>>,
    amm_authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    amm_open_orders: Option<&'b solana_account_info::AccountInfo<'a>>,
    pool_coin_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    pool_pc_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    lp_mint_address: Option<&'b solana_account_info::AccountInfo<'a>>,
    serum_market: Option<&'b solana_account_info::AccountInfo<'a>>,
    serum_event_queue: Option<&'b solana_account_info::AccountInfo<'a>>,
    param: Option<u8>,
    swap_base_in_value: Option<SwapInstructionBaseIn>,
    swap_base_out_value: Option<SwapInstructionBaseOut>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
