use lsp_types::ClientCapabilities;

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct ResolvedClientCapabilities {
    pub(crate) code_action_deferred_edit_resolution: bool,
    pub(crate) apply_edit: bool,
    pub(crate) document_changes: bool,
    pub(crate) workspace_refresh: bool,
    pub(crate) pull_diagnostics: bool,
    pub(crate) hierarchical_document_symbols: bool,
    pub(crate) folding_range_limit: Option<u32>,
    pub(crate) line_folding_only: bool,
}

impl ResolvedClientCapabilities {
    pub(super) fn new(client_capabilities: &ClientCapabilities) -> Self {
        let code_action_settings = client_capabilities
            .text_document
            .as_ref()
            .and_then(|doc_settings| doc_settings.code_action.as_ref());
        let code_action_data_support = code_action_settings
            .and_then(|settings| settings.data_support)
            .unwrap_or_default();
        let code_action_edit_resolution = code_action_settings
            .and_then(|settings| settings.resolve_support.as_ref())
            .is_some_and(|resolve_support| resolve_support.properties.contains(&"edit".into()));

        let apply_edit = client_capabilities
            .workspace
            .as_ref()
            .and_then(|workspace| workspace.apply_edit)
            .unwrap_or_default();

        let document_changes = client_capabilities
            .workspace
            .as_ref()
            .and_then(|workspace| workspace.workspace_edit.as_ref())
            .and_then(|workspace_edit| workspace_edit.document_changes)
            .unwrap_or_default();

        let pull_diagnostics = client_capabilities
            .text_document
            .as_ref()
            .and_then(|text_document| text_document.diagnostic.as_ref())
            .is_some();

        let hierarchical_document_symbols = client_capabilities
            .text_document
            .as_ref()
            .and_then(|text_document| text_document.document_symbol.as_ref())
            .and_then(|document_symbol| document_symbol.hierarchical_document_symbol_support)
            .unwrap_or_default();

        let folding_ranges = client_capabilities
            .text_document
            .as_ref()
            .and_then(|text_document| text_document.folding_range.as_ref());
        let folding_range_limit = folding_ranges.and_then(|folding| folding.range_limit);
        let line_folding_only = folding_ranges
            .and_then(|folding| folding.line_folding_only)
            .unwrap_or_default();

        Self {
            code_action_deferred_edit_resolution: code_action_data_support
                && code_action_edit_resolution,
            apply_edit,
            document_changes,
            workspace_refresh: false,
            pull_diagnostics,
            hierarchical_document_symbols,
            folding_range_limit,
            line_folding_only,
        }
    }
}
