mod breadcrumb_item;
mod theme;

pub use theme::BreadcrumbTheme;

use crate::{use_theme, Theme};
pub use breadcrumb_item::BreadcrumbItem;
use leptos::*;
use thaw_utils::{class_list, mount_style, OptionalProp};

#[component]
pub fn Breadcrumb(
    #[prop(default = MaybeSignal::Static("/".to_string()),into)] separator: MaybeSignal<String>,
    #[prop(optional, into)] class: OptionalProp<MaybeSignal<String>>,
    children: Children,
) -> impl IntoView {
    mount_style("breadcrumb", include_str!("./breadcrumb.css"));
    let theme = use_theme(Theme::light);
    let css_vars = create_memo(move |_| {
        let mut css_vars = String::new();
        theme.with(|theme| {
            css_vars.push_str(&format!(
                "--thaw-font-color: {};",
                theme.breadcrumb.item_font_color
            ));
            css_vars.push_str(&format!(
                "--thaw-font-color-hover: {};",
                theme.breadcrumb.item_font_color_hover
            ));
            css_vars.push_str(&format!(
                "--thaw-background-color-hover: {};",
                theme.breadcrumb.item_background_color_hover
            ));
        });
        css_vars
    });

    view! {
        <Provider value=BreadcrumbSeparatorInjection(separator)>
            <nav
                class=class_list!["thaw-breadcrumb", class.map(| c | move || c.get())]
                style=move || css_vars.get()
            >
                <ul>{children()}</ul>
            </nav>
        </Provider>
    }
}

#[derive(Clone)]
pub(crate) struct BreadcrumbSeparatorInjection(MaybeSignal<String>);

pub(crate) fn use_breadcrumb_separator() -> BreadcrumbSeparatorInjection {
    expect_context()
}
