//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
/// #[deprecated(note = "Use `swap_v2` instead.")]
/// Swaps one token for as much as possible of another token across a single
/// pool # Arguments
/// * `ctx` - The context of accounts
/// * `amount` - Arranged in pairs with other_amount_threshold. (amount_in,
///   amount_out_minimum) or (amount_out, amount_in_maximum)
/// * `other_amount_threshold` - For slippage check
/// * `sqrt_price_limit` - The Q64.64 sqrt price √P limit. If zero for one, the
///   price cannot
/// * `is_base_input` - swap base input or swap base output
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct Swap {
    pub amount: u64,
    pub other_amount_threshold: u64,
    pub sqrt_price_limit_x64: u128,
    pub is_base_input: bool,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SwapInstructionAccounts {
    pub payer: solana_pubkey::Pubkey,
    pub amm_config: solana_pubkey::Pubkey,
    pub pool_state: solana_pubkey::Pubkey,
    pub input_token_account: solana_pubkey::Pubkey,
    pub output_token_account: solana_pubkey::Pubkey,
    pub input_vault: solana_pubkey::Pubkey,
    pub output_vault: solana_pubkey::Pubkey,
    pub observation_state: solana_pubkey::Pubkey,
    pub token_program: solana_pubkey::Pubkey,
    pub tick_array: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl Swap {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [248, 198, 158, 145, 225, 117, 135, 200] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

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

impl ArrangeAccounts for Swap {
    type ArrangedAccounts = SwapInstructionAccounts;

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

        let payer = next_account(&mut iter)?;
        let amm_config = next_account(&mut iter)?;
        let pool_state = next_account(&mut iter)?;
        let input_token_account = next_account(&mut iter)?;
        let output_token_account = next_account(&mut iter)?;
        let input_vault = next_account(&mut iter)?;
        let output_vault = next_account(&mut iter)?;
        let observation_state = next_account(&mut iter)?;
        let token_program = next_account(&mut iter)?;
        let tick_array = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(SwapInstructionAccounts {
            payer,
            amm_config,
            pool_state,
            input_token_account,
            output_token_account,
            input_vault,
            output_vault,
            observation_state,
            token_program,
            tick_array,
            remaining: remaining.to_vec(),
        })
    }
}
