//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
/// Creates a pool for the given token pair and the initial price
/// # Arguments
/// * `ctx`- The context of accounts
/// * `sqrt_price_x64` - the initial sqrt price (amount_token_1 /
///   amount_token_0) of the pool as a Q64.64
///
/// Note: The open_time must be smaller than the current block_timestamp on
/// chain.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct CreatePool {
    pub sqrt_price_x64: u128,
    pub open_time: u64,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CreatePoolInstructionAccounts {
    pub pool_creator: solana_pubkey::Pubkey,
    pub amm_config: solana_pubkey::Pubkey,
    pub pool_state: solana_pubkey::Pubkey,
    pub token_mint0: solana_pubkey::Pubkey,
    pub token_mint1: solana_pubkey::Pubkey,
    pub token_vault0: solana_pubkey::Pubkey,
    pub token_vault1: solana_pubkey::Pubkey,
    pub observation_state: solana_pubkey::Pubkey,
    pub tick_array_bitmap: solana_pubkey::Pubkey,
    pub token_program0: solana_pubkey::Pubkey,
    pub token_program1: solana_pubkey::Pubkey,
    pub system_program: solana_pubkey::Pubkey,
    pub rent: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl CreatePool {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [233, 146, 209, 142, 207, 104, 64, 188] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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

impl ArrangeAccounts for CreatePool {
    type ArrangedAccounts = CreatePoolInstructionAccounts;

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

        let pool_creator = next_account(&mut iter)?;
        let amm_config = next_account(&mut iter)?;
        let pool_state = next_account(&mut iter)?;
        let token_mint0 = next_account(&mut iter)?;
        let token_mint1 = next_account(&mut iter)?;
        let token_vault0 = next_account(&mut iter)?;
        let token_vault1 = next_account(&mut iter)?;
        let observation_state = next_account(&mut iter)?;
        let tick_array_bitmap = next_account(&mut iter)?;
        let token_program0 = next_account(&mut iter)?;
        let token_program1 = next_account(&mut iter)?;
        let system_program = next_account(&mut iter)?;
        let rent = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(CreatePoolInstructionAccounts {
            pool_creator,
            amm_config,
            pool_state,
            token_mint0,
            token_mint1,
            token_vault0,
            token_vault1,
            observation_state,
            tick_array_bitmap,
            token_program0,
            token_program1,
            system_program,
            rent,
            remaining: remaining.to_vec(),
        })
    }
}
