//! 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 carbon_core::account_utils::next_account;
use carbon_core::borsh::{self, BorshDeserialize};
use carbon_core::deserialize::ArrangeAccounts;

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(
    Debug,
    Clone,
    carbon_core::borsh::BorshSerialize,
    carbon_core::borsh::BorshDeserialize,
    PartialEq,
)]
pub struct WithdrawBuyback {}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WithdrawBuybackInstructionAccounts {
    pub state: solana_pubkey::Pubkey,
    pub pool: solana_pubkey::Pubkey,
    pub token_x: solana_pubkey::Pubkey,
    pub token_y: solana_pubkey::Pubkey,
    pub buyback_x_account: solana_pubkey::Pubkey,
    pub buyback_y_account: solana_pubkey::Pubkey,
    pub pool_x_account: solana_pubkey::Pubkey,
    pub pool_y_account: solana_pubkey::Pubkey,
    pub admin: solana_pubkey::Pubkey,
    pub program_authority: solana_pubkey::Pubkey,
    pub system_program: solana_pubkey::Pubkey,
    pub token_program: solana_pubkey::Pubkey,
    pub associated_token_program: solana_pubkey::Pubkey,
    pub rent: solana_pubkey::Pubkey,
    pub remaining: Vec<solana_instruction::AccountMeta>,
}

impl WithdrawBuyback {
    pub fn decode(data: &[u8]) -> Option<Self> {
        if data.len() < 8 {
            return None;
        }
        let discriminator = &data[0..8];
        if discriminator != [188, 75, 30, 198, 99, 43, 12, 54] {
            return None;
        }

        let mut data_slice = data;

        data_slice = &data_slice[8..];

        if let Ok(decoded) = Self::deserialize(&mut data_slice) {
            return Some(decoded);
        }

        None
    }
}

impl ArrangeAccounts for WithdrawBuyback {
    type ArrangedAccounts = WithdrawBuybackInstructionAccounts;

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

        let state = next_account(&mut iter)?;
        let pool = next_account(&mut iter)?;
        let token_x = next_account(&mut iter)?;
        let token_y = next_account(&mut iter)?;
        let buyback_x_account = next_account(&mut iter)?;
        let buyback_y_account = next_account(&mut iter)?;
        let pool_x_account = next_account(&mut iter)?;
        let pool_y_account = next_account(&mut iter)?;
        let admin = next_account(&mut iter)?;
        let program_authority = 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 rent = next_account(&mut iter)?;

        let remaining = iter.as_slice();

        Some(WithdrawBuybackInstructionAccounts {
            state,
            pool,
            token_x,
            token_y,
            buyback_x_account,
            buyback_y_account,
            pool_x_account,
            pool_y_account,
            admin,
            program_authority,
            system_program,
            token_program,
            associated_token_program,
            rent,
            remaining: remaining.to_vec(),
        })
    }
}
