pub mod dispatcher;
pub mod filetree_renderer;
pub mod loader;
pub mod page_renderer;
pub mod picker_renderer;
pub mod protocol;
pub mod state;

use leptos::prelude::*;

#[component]
pub fn TreeShellRoot(mode: &'static str, headline: String) -> impl IntoView {
    view! {
        <section data-testid="tree-shell-root" data-mode=mode>
            <header>
                <p>"mnote tree shell"</p>
                <h1>{headline}</h1>
            </header>
        </section>
    }
}

#[cfg(test)]
mod tests {
    use super::dispatcher::TreeShellCommandAction;
    use super::loader::{TreeShellBootstrap, TreeShellLoader};
    use super::protocol;
    use super::state::TreeShellUiState;

    #[test]
    fn tree_shell_leptos_scaffold_exposes_loader_dispatcher_and_state() {
        let bootstrap = TreeShellBootstrap {
            workspace_id: "ws_demo".into(),
            root_node_id: Some("page_root".into()),
            mode: protocol::MODE_PAGE.into(),
            request_id: "req_demo".into(),
            trace_id: "trace_demo".into(),
        };
        let loader = TreeShellLoader::from_bootstrap(bootstrap.clone());
        let mut state = TreeShellUiState::from_defaults(&["page_root".into()]);
        state.select("page_root");
        state.focus("page_root");

        assert_eq!(loader.bootstrap.workspace_id, "ws_demo");
        assert_eq!(
            TreeShellCommandAction::Create.preferred_command_name(),
            "tree.node.create"
        );
        assert!(state.expanded.contains("page_root"));
        assert_eq!(state.selected.as_deref(), Some("page_root"));
        assert_eq!(state.focused.as_deref(), Some("page_root"));
        assert_eq!(bootstrap.mode, protocol::MODE_PAGE);
    }
}
