// src/functions/common/buffer.rs

use crate::state::app::buffer::BufferState;
use crate::state::app::buffer::AppView;

pub fn get_view_layer(view: &AppView) -> u8 {
    match view {
        AppView::Intro => 1,
        AppView::Login | AppView::Register | AppView::Admin | AppView::AddTable | AppView::AddLogic => 2,
        AppView::Form | AppView::Scratch => 3,
    }
}

/// Switches the active buffer index.
pub fn switch_buffer(buffer_state: &mut BufferState, next: bool) -> bool {
    if buffer_state.history.len() <= 1 {
        return false;
    }

    let len = buffer_state.history.len();
    let current_index = buffer_state.active_index;
    let new_index = if next {
        (current_index + 1) % len
    } else {
        (current_index + len - 1) % len
    };

    if new_index != current_index {
        buffer_state.active_index = new_index;
        true
    } else {
        false
    }
}

