//! 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::CpiRule;
use crate::generated::types::MetadataAdditionalFieldRule;
use crate::generated::types::TransferAmountRule;
use borsh::BorshDeserialize;
use borsh::BorshSerialize;

pub const CREATE_GUARD_DISCRIMINATOR: [u8; 8] = [251, 254, 17, 198, 219, 218, 154, 99];

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

    pub mint: solana_pubkey::Pubkey,

    pub mint_token_account: solana_pubkey::Pubkey,

    pub guard_authority: solana_pubkey::Pubkey,

    pub payer: solana_pubkey::Pubkey,

    pub associated_token_program: solana_pubkey::Pubkey,

    pub token_program: solana_pubkey::Pubkey,

    pub system_program: solana_pubkey::Pubkey,
}

impl CreateGuard {
    pub fn instruction(&self, args: CreateGuardInstructionArgs) -> 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: CreateGuardInstructionArgs,
        remaining_accounts: &[solana_instruction::AccountMeta],
    ) -> solana_instruction::Instruction {
        let mut accounts = Vec::with_capacity(8 + remaining_accounts.len());
        accounts.push(solana_instruction::AccountMeta::new(self.guard, false));
        accounts.push(solana_instruction::AccountMeta::new(self.mint, true));
        accounts.push(solana_instruction::AccountMeta::new(
            self.mint_token_account,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.guard_authority,
            true,
        ));
        accounts.push(solana_instruction::AccountMeta::new(self.payer, true));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.associated_token_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.token_program,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            self.system_program,
            false,
        ));
        accounts.extend_from_slice(remaining_accounts);
        let mut data = borsh::to_vec(&CreateGuardInstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&args).unwrap();
        data.append(&mut args);

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

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

impl CreateGuardInstructionData {
    pub fn new() -> Self {
        Self {
            discriminator: [251, 254, 17, 198, 219, 218, 154, 99],
        }
    }
}

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

#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreateGuardInstructionArgs {
    pub name: String,
    pub symbol: String,
    pub uri: String,
    pub cpi_rule: Option<CpiRule>,
    pub transfer_amount_rule: Option<TransferAmountRule>,
    pub additional_fields_rule: Vec<MetadataAdditionalFieldRule>,
}

/// Instruction builder for `CreateGuard`.
///
/// ### Accounts:
///
///   0. `[writable]` guard
///   1. `[writable, signer]` mint
///   2. `[writable]` mint_token_account
///   3. `[signer]` guard_authority
///   4. `[writable, signer]` payer
///   5. `[optional]` associated_token_program (default to `ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`)
///   6. `[optional]` token_program (default to `TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`)
///   7. `[optional]` system_program (default to `11111111111111111111111111111111`)
#[derive(Clone, Debug, Default)]
pub struct CreateGuardBuilder {
    guard: Option<solana_pubkey::Pubkey>,
    mint: Option<solana_pubkey::Pubkey>,
    mint_token_account: Option<solana_pubkey::Pubkey>,
    guard_authority: Option<solana_pubkey::Pubkey>,
    payer: Option<solana_pubkey::Pubkey>,
    associated_token_program: Option<solana_pubkey::Pubkey>,
    token_program: Option<solana_pubkey::Pubkey>,
    system_program: Option<solana_pubkey::Pubkey>,
    name: Option<String>,
    symbol: Option<String>,
    uri: Option<String>,
    cpi_rule: Option<CpiRule>,
    transfer_amount_rule: Option<TransferAmountRule>,
    additional_fields_rule: Option<Vec<MetadataAdditionalFieldRule>>,
    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
}

impl CreateGuardBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    #[inline(always)]
    pub fn guard(&mut self, guard: solana_pubkey::Pubkey) -> &mut Self {
        self.guard = Some(guard);
        self
    }
    #[inline(always)]
    pub fn mint(&mut self, mint: solana_pubkey::Pubkey) -> &mut Self {
        self.mint = Some(mint);
        self
    }
    #[inline(always)]
    pub fn mint_token_account(&mut self, mint_token_account: solana_pubkey::Pubkey) -> &mut Self {
        self.mint_token_account = Some(mint_token_account);
        self
    }
    #[inline(always)]
    pub fn guard_authority(&mut self, guard_authority: solana_pubkey::Pubkey) -> &mut Self {
        self.guard_authority = Some(guard_authority);
        self
    }
    #[inline(always)]
    pub fn payer(&mut self, payer: solana_pubkey::Pubkey) -> &mut Self {
        self.payer = Some(payer);
        self
    }
    /// `[optional account, default to 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL']`
    #[inline(always)]
    pub fn associated_token_program(
        &mut self,
        associated_token_program: solana_pubkey::Pubkey,
    ) -> &mut Self {
        self.associated_token_program = Some(associated_token_program);
        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
    }
    #[inline(always)]
    pub fn name(&mut self, name: String) -> &mut Self {
        self.name = Some(name);
        self
    }
    #[inline(always)]
    pub fn symbol(&mut self, symbol: String) -> &mut Self {
        self.symbol = Some(symbol);
        self
    }
    #[inline(always)]
    pub fn uri(&mut self, uri: String) -> &mut Self {
        self.uri = Some(uri);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn cpi_rule(&mut self, cpi_rule: CpiRule) -> &mut Self {
        self.cpi_rule = Some(cpi_rule);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn transfer_amount_rule(&mut self, transfer_amount_rule: TransferAmountRule) -> &mut Self {
        self.transfer_amount_rule = Some(transfer_amount_rule);
        self
    }
    #[inline(always)]
    pub fn additional_fields_rule(
        &mut self,
        additional_fields_rule: Vec<MetadataAdditionalFieldRule>,
    ) -> &mut Self {
        self.additional_fields_rule = Some(additional_fields_rule);
        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 = CreateGuard {
            guard: self.guard.expect("guard is not set"),
            mint: self.mint.expect("mint is not set"),
            mint_token_account: self
                .mint_token_account
                .expect("mint_token_account is not set"),
            guard_authority: self.guard_authority.expect("guard_authority is not set"),
            payer: self.payer.expect("payer is not set"),
            associated_token_program: self.associated_token_program.unwrap_or(
                solana_pubkey::pubkey!("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"),
            ),
            token_program: self.token_program.unwrap_or(solana_pubkey::pubkey!(
                "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
            )),
            system_program: self
                .system_program
                .unwrap_or(solana_pubkey::pubkey!("11111111111111111111111111111111")),
        };
        let args = CreateGuardInstructionArgs {
            name: self.name.clone().expect("name is not set"),
            symbol: self.symbol.clone().expect("symbol is not set"),
            uri: self.uri.clone().expect("uri is not set"),
            cpi_rule: self.cpi_rule.clone(),
            transfer_amount_rule: self.transfer_amount_rule.clone(),
            additional_fields_rule: self
                .additional_fields_rule
                .clone()
                .expect("additional_fields_rule is not set"),
        };

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

/// `create_guard` CPI accounts.
pub struct CreateGuardCpiAccounts<'a, 'b> {
    pub guard: &'b solana_account_info::AccountInfo<'a>,

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

impl<'a, 'b> CreateGuardCpi<'a, 'b> {
    pub fn new(
        program: &'b solana_account_info::AccountInfo<'a>,
        accounts: CreateGuardCpiAccounts<'a, 'b>,
        args: CreateGuardInstructionArgs,
    ) -> Self {
        Self {
            __program: program,
            guard: accounts.guard,
            mint: accounts.mint,
            mint_token_account: accounts.mint_token_account,
            guard_authority: accounts.guard_authority,
            payer: accounts.payer,
            associated_token_program: accounts.associated_token_program,
            token_program: accounts.token_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_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(*self.guard.key, false));
        accounts.push(solana_instruction::AccountMeta::new(*self.mint.key, true));
        accounts.push(solana_instruction::AccountMeta::new(
            *self.mint_token_account.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.guard_authority.key,
            true,
        ));
        accounts.push(solana_instruction::AccountMeta::new(*self.payer.key, true));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.associated_token_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.token_program.key,
            false,
        ));
        accounts.push(solana_instruction::AccountMeta::new_readonly(
            *self.system_program.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(&CreateGuardInstructionData::new()).unwrap();
        let mut args = borsh::to_vec(&self.__args).unwrap();
        data.append(&mut args);

        let instruction = solana_instruction::Instruction {
            program_id: crate::WEN_TRANSFER_GUARD_ID,
            accounts,
            data,
        };
        let mut account_infos = Vec::with_capacity(9 + remaining_accounts.len());
        account_infos.push(self.__program.clone());
        account_infos.push(self.guard.clone());
        account_infos.push(self.mint.clone());
        account_infos.push(self.mint_token_account.clone());
        account_infos.push(self.guard_authority.clone());
        account_infos.push(self.payer.clone());
        account_infos.push(self.associated_token_program.clone());
        account_infos.push(self.token_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_cpi::invoke(&instruction, &account_infos)
        } else {
            solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
        }
    }
}

/// Instruction builder for `CreateGuard` via CPI.
///
/// ### Accounts:
///
///   0. `[writable]` guard
///   1. `[writable, signer]` mint
///   2. `[writable]` mint_token_account
///   3. `[signer]` guard_authority
///   4. `[writable, signer]` payer
///   5. `[]` associated_token_program
///   6. `[]` token_program
///   7. `[]` system_program
#[derive(Clone, Debug)]
pub struct CreateGuardCpiBuilder<'a, 'b> {
    instruction: Box<CreateGuardCpiBuilderInstruction<'a, 'b>>,
}

impl<'a, 'b> CreateGuardCpiBuilder<'a, 'b> {
    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
        let instruction = Box::new(CreateGuardCpiBuilderInstruction {
            __program: program,
            guard: None,
            mint: None,
            mint_token_account: None,
            guard_authority: None,
            payer: None,
            associated_token_program: None,
            token_program: None,
            system_program: None,
            name: None,
            symbol: None,
            uri: None,
            cpi_rule: None,
            transfer_amount_rule: None,
            additional_fields_rule: None,
            __remaining_accounts: Vec::new(),
        });
        Self { instruction }
    }
    #[inline(always)]
    pub fn guard(&mut self, guard: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
        self.instruction.guard = Some(guard);
        self
    }
    #[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 mint_token_account(
        &mut self,
        mint_token_account: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.mint_token_account = Some(mint_token_account);
        self
    }
    #[inline(always)]
    pub fn guard_authority(
        &mut self,
        guard_authority: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.guard_authority = Some(guard_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 associated_token_program(
        &mut self,
        associated_token_program: &'b solana_account_info::AccountInfo<'a>,
    ) -> &mut Self {
        self.instruction.associated_token_program = Some(associated_token_program);
        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 name(&mut self, name: String) -> &mut Self {
        self.instruction.name = Some(name);
        self
    }
    #[inline(always)]
    pub fn symbol(&mut self, symbol: String) -> &mut Self {
        self.instruction.symbol = Some(symbol);
        self
    }
    #[inline(always)]
    pub fn uri(&mut self, uri: String) -> &mut Self {
        self.instruction.uri = Some(uri);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn cpi_rule(&mut self, cpi_rule: CpiRule) -> &mut Self {
        self.instruction.cpi_rule = Some(cpi_rule);
        self
    }
    /// `[optional argument]`
    #[inline(always)]
    pub fn transfer_amount_rule(&mut self, transfer_amount_rule: TransferAmountRule) -> &mut Self {
        self.instruction.transfer_amount_rule = Some(transfer_amount_rule);
        self
    }
    #[inline(always)]
    pub fn additional_fields_rule(
        &mut self,
        additional_fields_rule: Vec<MetadataAdditionalFieldRule>,
    ) -> &mut Self {
        self.instruction.additional_fields_rule = Some(additional_fields_rule);
        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 = CreateGuardInstructionArgs {
            name: self.instruction.name.clone().expect("name is not set"),
            symbol: self.instruction.symbol.clone().expect("symbol is not set"),
            uri: self.instruction.uri.clone().expect("uri is not set"),
            cpi_rule: self.instruction.cpi_rule.clone(),
            transfer_amount_rule: self.instruction.transfer_amount_rule.clone(),
            additional_fields_rule: self
                .instruction
                .additional_fields_rule
                .clone()
                .expect("additional_fields_rule is not set"),
        };
        let instruction = CreateGuardCpi {
            __program: self.instruction.__program,

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

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

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

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

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

            associated_token_program: self
                .instruction
                .associated_token_program
                .expect("associated_token_program 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"),
            __args: args,
        };
        instruction.invoke_signed_with_remaining_accounts(
            signers_seeds,
            &self.instruction.__remaining_accounts,
        )
    }
}

#[derive(Clone, Debug)]
struct CreateGuardCpiBuilderInstruction<'a, 'b> {
    __program: &'b solana_account_info::AccountInfo<'a>,
    guard: Option<&'b solana_account_info::AccountInfo<'a>>,
    mint: Option<&'b solana_account_info::AccountInfo<'a>>,
    mint_token_account: Option<&'b solana_account_info::AccountInfo<'a>>,
    guard_authority: Option<&'b solana_account_info::AccountInfo<'a>>,
    payer: Option<&'b solana_account_info::AccountInfo<'a>>,
    associated_token_program: 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>>,
    name: Option<String>,
    symbol: Option<String>,
    uri: Option<String>,
    cpi_rule: Option<CpiRule>,
    transfer_amount_rule: Option<TransferAmountRule>,
    additional_fields_rule: Option<Vec<MetadataAdditionalFieldRule>>,
    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
}
