//! Content slug resolution functionality

use super::loaders::load_content_index;
use super::types::SlugResolution;

/// Resolve a slug to its canonical ID and language by searching all content
/// types
pub fn resolve_slug(slug: &str) -> Option<SlugResolution> {
    // Remove .html extension if present
    let clean_slug = slug.strip_suffix(".html").unwrap_or(slug);

    // Get all supported content types from registry
    #[cfg(not(target_arch = "wasm32"))]
    {
        use crate::content::traits::ContentKindRegistry;
        let content_types = crate::get_enabled_content_types();

        for content_type in content_types {
            if let Some(resolution) = resolve_content_slug(clean_slug, &content_type) {
                return Some(resolution);
            }
        }
    }

    #[cfg(target_arch = "wasm32")]
    {
        // For WASM, try available content types
        let available_types = crate::get_all_content_types();
        for content_type in &available_types {
            if let Some(resolution) = resolve_content_slug(clean_slug, content_type) {
                return Some(resolution);
            }
        }
    }

    None
}

/// Generic content slug resolver that works with any content type
/// Uses content-kinds.toml configuration to check all content types
pub fn resolve_content_slug(slug: &str, content_type: &str) -> Option<SlugResolution> {
    // Check all supported languages
    let supported_languages = crate::config::get_supported_languages();
    for lang in supported_languages {
        if let Ok(index) = load_content_index(content_type, lang) {
            // Check all items in the content index
            for item in &index.items {
                if item.id == slug || item.localized_slug.as_deref() == Some(slug) {
                    return Some(SlugResolution {
                        canonical_id: item.id.clone(),
                        language: lang.to_string(),
                        content_type: content_type.to_string(),
                    });
                }
            }
        }
    }
    None
}

/// Generic content slug resolver with language priority
/// Uses content-kinds.toml configuration to check content by type
pub fn resolve_content_slug_with_language_priority(
    slug: &str,
    content_type: &str,
    requested_language: &str,
) -> Option<SlugResolution> {
    // Try the requested language first
    if let Ok(index) = load_content_index(content_type, requested_language) {
        // Check all items in the content index
        for item in &index.items {
            if item.id == slug || item.localized_slug.as_deref() == Some(slug) {
                return Some(SlugResolution {
                    canonical_id: item.id.clone(),
                    language: requested_language.to_string(),
                    content_type: content_type.to_string(),
                });
            }
        }
    }

    // If not found in requested language, try other supported languages
    let supported_languages = crate::config::get_supported_languages();
    for lang in supported_languages {
        if lang == requested_language {
            continue; // Skip already checked language
        }

        if let Ok(index) = load_content_index(content_type, lang) {
            // Check all items in the content index
            for item in &index.items {
                if item.id == slug || item.localized_slug.as_deref() == Some(slug) {
                    return Some(SlugResolution {
                        canonical_id: item.id.clone(),
                        language: lang.to_string(),
                        content_type: content_type.to_string(),
                    });
                }
            }
        }
    }
    None
}
