//! URL builders for HTMX endpoints under `/api/htmx/*`.

use crate::HTMX_API_ROOT;

/// Build a path under the HTMX API root.
///
/// `subpath` is appended with exactly one `/` separator regardless of leading
/// slashes on the input.
///
/// ```
/// use rustelo_components_htmx::primitives::htmx_path;
/// assert_eq!(htmx_path("theme/toggle"), "/api/htmx/theme/toggle");
/// assert_eq!(htmx_path("/auth/login"), "/api/htmx/auth/login");
/// ```
pub fn htmx_path(subpath: &str) -> String {
    let trimmed = subpath.trim_start_matches('/');
    format!("{HTMX_API_ROOT}/{trimmed}")
}

/// Build a path under the generic `/api/*` root (non-HTMX server functions).
///
/// Used when a component points at a server function or REST endpoint that
/// returns JSON rather than an HTML fragment.
pub fn api_path(subpath: &str) -> String {
    let trimmed = subpath.trim_start_matches('/');
    format!("/api/{trimmed}")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn htmx_path_strips_leading_slash() {
        assert_eq!(htmx_path("/x/y"), "/api/htmx/x/y");
        assert_eq!(htmx_path("x/y"), "/api/htmx/x/y");
    }

    #[test]
    fn api_path_strips_leading_slash() {
        assert_eq!(api_path("/health"), "/api/health");
        assert_eq!(api_path("health"), "/api/health");
    }
}
