//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
/// Creates a new position wrapped in a Token2022 NFT without relying on
/// metadata_program and metadata_account, reduce the cost for user to create a
/// personal position. # Arguments
/// * `ctx` - The context of accounts
/// * `tick_lower_index` - The low boundary of market
/// * `tick_upper_index` - The upper boundary of market
/// * `tick_array_lower_start_index` - The start index of tick array which
///   include tick low
/// * `tick_array_upper_start_index` - The start index of tick array which
///   include tick upper
/// * `liquidity` - The liquidity to be added, if zero, and the base_flag is
///   specified, calculate liquidity base amount_0_max or amount_1_max according
///   base_flag, otherwise open position with zero liquidity
/// * `amount_0_max` - The max amount of token_0 to spend, which serves as a
///   slippage check
/// * `amount_1_max` - The max amount of token_1 to spend, which serves as a
///   slippage check
/// * `with_metadata` - The flag indicating whether to create NFT mint metadata
/// * `base_flag` - if the liquidity specified as zero, true: calculate
///   liquidity base amount_0_max otherwise base amount_1_max
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct OpenPositionWithToken22Nft {
    pub tick_lower_index: i32,
    pub tick_upper_index: i32,
    pub tick_array_lower_start_index: i32,
    pub tick_array_upper_start_index: i32,
    pub liquidity: u128,
    pub amount0_max: u64,
    pub amount1_max: u64,
    pub with_metadata: bool,
    pub base_flag: Option<bool>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OpenPositionWithToken22NftInstructionAccounts {
    pub payer: solana_pubkey::Pubkey,
    pub position_nft_owner: solana_pubkey::Pubkey,
    pub position_nft_mint: solana_pubkey::Pubkey,
    pub position_nft_account: solana_pubkey::Pubkey,
    pub pool_state: solana_pubkey::Pubkey,
    pub protocol_position: solana_pubkey::Pubkey,
    pub tick_array_lower: solana_pubkey::Pubkey,
    pub tick_array_upper: solana_pubkey::Pubkey,
    pub personal_position: solana_pubkey::Pubkey,
    pub token_account0: solana_pubkey::Pubkey,
    pub token_account1: solana_pubkey::Pubkey,
    pub token_vault0: solana_pubkey::Pubkey,
    pub token_vault1: solana_pubkey::Pubkey,
    pub rent: solana_pubkey::Pubkey,
    pub system_program: solana_pubkey::Pubkey,
    pub token_program: solana_pubkey::Pubkey,
    pub associated_token_program: solana_pubkey::Pubkey,
    pub token_program2022: solana_pubkey::Pubkey,
    pub vault0_mint: solana_pubkey::Pubkey,
    pub vault1_mint: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl OpenPositionWithToken22Nft {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [77, 255, 174, 82, 125, 29, 201, 46] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

        borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
    }
}

impl ArrangeAccounts for OpenPositionWithToken22Nft {
    type ArrangedAccounts = OpenPositionWithToken22NftInstructionAccounts;

    fn arrange_accounts(
        accounts: &[solana_instruction::AccountMeta],
    ) -> Option<Self::ArrangedAccounts> {
        let mut iter = accounts.iter();

        let payer = next_account(&mut iter)?;
        let position_nft_owner = next_account(&mut iter)?;
        let position_nft_mint = next_account(&mut iter)?;
        let position_nft_account = next_account(&mut iter)?;
        let pool_state = next_account(&mut iter)?;
        let protocol_position = next_account(&mut iter)?;
        let tick_array_lower = next_account(&mut iter)?;
        let tick_array_upper = next_account(&mut iter)?;
        let personal_position = next_account(&mut iter)?;
        let token_account0 = next_account(&mut iter)?;
        let token_account1 = next_account(&mut iter)?;
        let token_vault0 = next_account(&mut iter)?;
        let token_vault1 = next_account(&mut iter)?;
        let rent = next_account(&mut iter)?;
        let system_program = next_account(&mut iter)?;
        let token_program = next_account(&mut iter)?;
        let associated_token_program = next_account(&mut iter)?;
        let token_program2022 = next_account(&mut iter)?;
        let vault0_mint = next_account(&mut iter)?;
        let vault1_mint = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(OpenPositionWithToken22NftInstructionAccounts {
            payer,
            position_nft_owner,
            position_nft_mint,
            position_nft_account,
            pool_state,
            protocol_position,
            tick_array_lower,
            tick_array_upper,
            personal_position,
            token_account0,
            token_account1,
            token_vault0,
            token_vault1,
            rent,
            system_program,
            token_program,
            associated_token_program,
            token_program2022,
            vault0_mint,
            vault1_mint,
            remaining: remaining.to_vec(),
        })
    }
}
