#![allow(deprecated)]

use_prelude!();
use super::*;

/// Use [`pending_id_specific_op.observe_local(...)`] to receive `SingleDocumentLiveQueryEvent`s
/// describing document changes.
///
/// Provides information about a live query event relating to a single document live query.
///
/// [`pending_id_specific_op.observe_local(...)`]: crate::store::query_builder::PendingIdSpecificOperation::observe_local
#[deprecated(
    since = "4.14.4",
    note = "Will be removed in SDK 5.0. Use DQL queries instead"
)]
#[derive(Debug)]
pub struct SingleDocumentLiveQueryEvent {
    /// States whether the event is an `initial` event or not.
    pub(crate) is_initial: bool,
    pub(crate) old_document: Option<ffi_sdk::BoxedDocument>,
}

impl SingleDocumentLiveQueryEvent {
    /// Return true if the event is an "initial"
    pub fn is_initial(&self) -> bool {
        self.is_initial
    }

    /// Return the old boxed document
    pub fn old_document(&self) -> &Option<ffi_sdk::BoxedDocument> {
        &self.old_document
    }
}

impl SingleDocumentLiveQueryEvent {
    /// Return hash of the eventually contained document.
    pub fn hash(&self, doc: &Option<ffi_sdk::BoxedDocument>) -> Result<u64, DittoError> {
        let zero_or_one_doc: &[_] = doc.as_ref().map_or(&[], ::core::slice::from_ref);
        ffi_sdk::ditto_documents_hash(zero_or_one_doc.into()).ok()
    }

    /// Return hash of the mnemonic of the eventually contained document.
    pub fn hash_mnemonic(
        &self,
        doc: &Option<ffi_sdk::BoxedDocument>,
    ) -> Result<String, DittoError> {
        let zero_or_one_doc: &[_] = doc.as_ref().map_or(&[], ::core::slice::from_ref);
        let mnemonic_c_str =
            { ffi_sdk::ditto_documents_hash_mnemonic(zero_or_one_doc.into()).ok()? };
        Ok(mnemonic_c_str.into_string())
    }
}
