// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

use super::*;
use crate::layout::spacer::spacer;

#[ derive( Clone, Debug, PartialEq, Eq ) ]
enum Msg { Pick( usize ) }

#[ test ]
fn defaults_have_no_pages()
{
	let n: Notebook<Msg> = notebook();
	assert_eq!( n.pages.len(), 0 );
	assert_eq!( n.selected, 0 );
	assert!( n.on_select.is_none() );
}

#[ test ]
fn page_appends_in_order()
{
	let n: Notebook<Msg> = notebook()
		.page( "A", spacer() )
		.page( "B", spacer() )
		.page( "C", spacer() );
	assert_eq!( n.pages.len(), 3 );
	assert_eq!( n.pages[0].label, "A" );
	assert_eq!( n.pages[1].label, "B" );
	assert_eq!( n.pages[2].label, "C" );
}

#[ test ]
fn selected_builder_records_index()
{
	let n: Notebook<Msg> = notebook()
		.page( "A", spacer() )
		.page( "B", spacer() )
		.selected( 1 );
	assert_eq!( n.selected, 1 );
}

#[ test ]
fn on_select_callback_is_invoked_for_index()
{
	let n: Notebook<Msg> = notebook()
		.page( "A", spacer() )
		.on_select( Msg::Pick );
	let cb = n.on_select.as_ref().expect( "callback present" );
	assert_eq!( cb( 7 ), Msg::Pick( 7 ) );
}

#[ test ]
fn build_does_not_panic_on_empty_pages()
{
	let _: Element<Msg> = notebook().build();
}

#[ test ]
fn build_does_not_panic_on_out_of_range_selected()
{
	// Out-of-range `selected` must fall back to page 0 instead of
	// panicking with an index error in the swap_remove.
	let n: Notebook<Msg> = notebook()
		.page( "A", spacer() )
		.page( "B", spacer() )
		.selected( 99 );
	let _: Element<Msg> = n.build();
}
