//! Unified SPA link component interface using shared delegation patterns
//!
//! This module provides a unified interface that automatically selects between
//! client-side reactive and server-side static implementations based on
//! context.

use leptos::prelude::*;

#[cfg(target_arch = "wasm32")]
use crate::ui::spa_link::client::SpaLink as SpaLinkClient;
#[cfg(not(target_arch = "wasm32"))]
use crate::ui::spa_link::ssr::SpaLinkSSR;

/// Unified SPA link component that delegates to appropriate implementation
#[component]
pub fn SpaLink(
    /// The target URL
    href: String,
    /// Link content/children
    children: Children,
    /// CSS class
    #[prop(optional)]
    class: Option<String>,
    /// Whether this is an external link
    #[prop(default = false)]
    external: bool,
    /// Target attribute for external links
    #[prop(optional)]
    target: Option<String>,
    /// Rel attribute for external links
    #[prop(optional)]
    rel: Option<String>,
    /// ARIA label
    #[prop(optional)]
    aria_label: Option<String>,
    /// Title attribute
    #[prop(optional)]
    title: Option<String>,
) -> impl IntoView {
    #[cfg(not(target_arch = "wasm32"))]
    {
        // SSR context: use static implementation
        view! {
            <SpaLinkSSR
                href=href
                class=class.unwrap_or_default()
                external=external
                target=target.unwrap_or_default()
                rel=rel.unwrap_or_default()
                aria_label=aria_label.unwrap_or_default()
                title=title.unwrap_or_default()
            >
                {children()}
            </SpaLinkSSR>
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // Client context: use reactive implementation
        view! {
            <SpaLinkClient
                href=href
                class=class.unwrap_or_default()
                external=external
                target=target.unwrap_or_default()
                rel=rel.unwrap_or_default()
                aria_label=aria_label.unwrap_or_default()
                title=title.unwrap_or_default()
            >
                {children()}
            </SpaLinkClient>
        }
    }
}
